星火微课系统客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PrintWindow.xaml.cs 19KB

4 anni fa
4 anni fa
4 anni fa
4 anni fa
4 anni fa
4 anni fa
4 anni fa
4 anni fa
4 anni fa
4 anni fa
4 anni fa
4 anni fa
4 anni fa
4 anni fa
4 anni fa
4 anni fa
4 anni fa
4 anni fa
4 anni fa
4 anni fa
4 anni fa
4 anni fa
4 anni fa
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. using Common.system;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Threading;
  8. using System.Windows;
  9. using System.Windows.Input;
  10. using System.Windows.Media.Imaging;
  11. using System.Windows.Threading;
  12. namespace XHWK.WKTool
  13. {
  14. /// <summary>
  15. /// 打印
  16. /// </summary>
  17. public partial class PrintWindow : Window
  18. {
  19. /// <summary>
  20. /// 下拉框数据源
  21. /// </summary>
  22. public DataTable data = new DataTable();
  23. private DataTable dtComponentsUniqueNo;
  24. private System.Timers.Timer times;
  25. public PrintWindow()
  26. {
  27. InitializeComponent();
  28. double proportion = 1036.0 / 1290.0;
  29. double Widths = (PrimaryScreen.ScaleWorkingAreaSize.Height) / proportion - (BorderThickness.Left + BorderThickness.Right);
  30. double Heights = PrimaryScreen.ScaleWorkingAreaSize.Height - (BorderThickness.Top + BorderThickness.Bottom);
  31. Width = Widths / 1.42;
  32. Height = Heights / 1.71;
  33. GridContent.Width = Width + 5;
  34. GridContent.Height = Height;
  35. ResizeMode = ResizeMode.NoResize;
  36. string defa = string.Empty;
  37. List<string> defaList = LatticeFileHelper.GetPrinterList(out defa);
  38. if (defaList.Count > 0)
  39. {
  40. data.Columns.Add("Value");
  41. data.Columns.Add("Key");
  42. for (int i = 0; i < defaList.Count; i++)
  43. {
  44. //创建一行
  45. DataRow row = data.NewRow();
  46. //将此行添加到table中
  47. data.Rows.Add(row);
  48. data.Rows[i]["Value"] = defaList[i];
  49. data.Rows[i]["Key"] = i.ToString();
  50. }
  51. dtComponentsUniqueNo = data.DefaultView.ToTable();
  52. cmbClass.ItemsSource = dtComponentsUniqueNo.DefaultView;
  53. cmbClass.SelectedIndex = 0;
  54. }
  55. }
  56. private double wit = 0;
  57. private double hei = 0;
  58. public void Initialize(string _imgPath, double _wit, double _hei)
  59. {
  60. wit = _wit;
  61. hei = _hei;
  62. if (!string.IsNullOrWhiteSpace(_imgPath))
  63. {
  64. imgPri.Source = new BitmapImage(new Uri(_imgPath));
  65. }
  66. else
  67. {
  68. imgPri.Source = null;
  69. }
  70. I = 0;
  71. tip_outer.Visibility = Visibility.Collapsed;
  72. }
  73. /// <summary>
  74. /// 打印
  75. /// </summary>
  76. /// <param name="sender"></param>
  77. /// <param name="e"></param>
  78. private void BtnPrint_Click(object sender, RoutedEventArgs e)
  79. {
  80. if (string.IsNullOrWhiteSpace(cmbClass.Text))
  81. {
  82. //MessageWindow.Show("打印机不能为空!");
  83. MessageWindow.Show("请设置打印机!");
  84. return;
  85. }
  86. //APP.myloading.Show();
  87. btnClose.IsEnabled = false;
  88. btnPrint.IsEnabled = false;
  89. //APP.BackgroundWorkerHelper.RunWorkerAsync(InvokeServering, InvokeServerCompate);
  90. Thread myThread = new Thread(StartPrint);
  91. List<string> StrList = new List<string>();
  92. //string imgPath =
  93. //FileToolsCommon.CreateFile(imgPath);
  94. //string pdf = imgPath + "\\101.pdf";
  95. //PDF位置
  96. //string SourcePath = FileToolsCommon.GetFileAbsolutePath("/temp/101.pdf");
  97. //StrList.Add(SourcePath);
  98. int ipdf = 102;
  99. string imgPath = FileToolsCommon.GetFileAbsolutePath("temp/");
  100. string tempImgPath = imgPath;
  101. imgPath += "101.pdf";
  102. while (File.Exists(imgPath))
  103. {
  104. imgPath = tempImgPath + ipdf.ToString() + ".pdf";
  105. ipdf++;
  106. }
  107. StrList.Add(imgPath);
  108. //打印数量
  109. int PrinterNum = Convert.ToInt32(txbNumberOfCopies.Text);
  110. StrList.Add(PrinterNum.ToString());
  111. //打印机名称
  112. string PrinterName = cmbClass.Text;
  113. StrList.Add(PrinterName);
  114. num = 0;
  115. tip_outer.Visibility = Visibility.Visible;
  116. myThread.Start(StrList);
  117. times = new System.Timers.Timer(100);
  118. times.Elapsed += Times_ElapsedClick;
  119. times.Start();
  120. }
  121. private int num = 0;
  122. /// <summary>
  123. /// 计时器
  124. /// </summary>
  125. /// <param name="sender"></param>
  126. /// <param name="e"></param>
  127. private void Times_ElapsedClick(object sender, System.Timers.ElapsedEventArgs e)
  128. {
  129. Dispatcher.Invoke(() =>
  130. {
  131. pgbProcess.Value = num;
  132. lbProcess.Content = num.ToString() + "%";
  133. if (num < 99)
  134. {
  135. num++;
  136. times.Interval += (num / 2);
  137. }
  138. else
  139. {
  140. times.Stop();
  141. }
  142. });
  143. }
  144. /// <summary>
  145. /// 开始打印
  146. /// </summary>
  147. private void StartPrint(object obj)
  148. {
  149. try
  150. {
  151. iTextSharp.text.Document document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 0, 0, 0, 0);
  152. //iTextSharp.text.Rectangle page = new iTextSharp.text.Rectangle(iTextSharp.text.PageSize.A4., 250f);//cs
  153. ////设置纸张横向
  154. //document.SetPageSize(iTextSharp.text.PageSize.A4.Rotate());
  155. List<string> objStr1 = (List<string>)obj;
  156. string SourcePath1 = objStr1[0];
  157. iTextSharp.text.pdf.PdfWriter.GetInstance(document, new FileStream(SourcePath1, FileMode.Create, FileAccess.ReadWrite));
  158. document.Open();
  159. iTextSharp.text.Image image;
  160. for (int i = 0; i < APP.PageDrawList.Count; i++)
  161. {
  162. long ii = Timestamp();
  163. string directoryPath = AppDomain.CurrentDomain.BaseDirectory + "Temp\\";
  164. string filePathOutPut = Path.Combine(directoryPath, string.Format("print{0}{1}.jpg", ii, i));
  165. RectangleF rectangleFs = new RectangleF();
  166. MergerImg("", filePathOutPut, rectangleFs, out string errmsg);
  167. image = iTextSharp.text.Image.GetInstance(filePathOutPut);
  168. if (string.IsNullOrEmpty(APP.PageDrawList[i].PageImagePath))//没有图片
  169. {
  170. }
  171. else if (APP.PageDrawList[i].ImgDocumentation == true && !APP.PageDrawList[i].Type.Equals("ppt"))
  172. {
  173. image = iTextSharp.text.Image.GetInstance(APP.PageDrawList[i].PageImagePath);
  174. }
  175. else
  176. {
  177. RectangleF rectangleF = new RectangleF(0, 0, 0, 0);
  178. Dispatcher.Invoke(() =>
  179. {
  180. rectangleF = new RectangleF
  181. {
  182. Width = (float)APP.PageDrawList[i].ImageSizes.CenterX,
  183. Height = (float)APP.PageDrawList[i].ImageSizes.CenterY,
  184. X = (float)APP.PageDrawList[i].ImageLocation.X,
  185. Y = (float)APP.PageDrawList[i].ImageLocation.Y
  186. };
  187. });
  188. string msgs = string.Empty;
  189. bool isImg = MergerImg(APP.PageDrawList[i].PageImagePath, filePathOutPut, rectangleF, out msgs);
  190. if (isImg)
  191. {
  192. image = iTextSharp.text.Image.GetInstance(filePathOutPut);
  193. }
  194. else
  195. {
  196. image = iTextSharp.text.Image.GetInstance(APP.PageDrawList[i].PageImagePath);
  197. }
  198. }
  199. if (image.Height > iTextSharp.text.PageSize.A4.Height)
  200. {
  201. image.ScaleToFit(iTextSharp.text.PageSize.A4.Width, iTextSharp.text.PageSize.A4.Height);
  202. }
  203. else if (image.Width > iTextSharp.text.PageSize.A4.Width)
  204. {
  205. image.ScaleToFit(iTextSharp.text.PageSize.A4.Width, iTextSharp.text.PageSize.A4.Height);
  206. }
  207. image.Alignment = iTextSharp.text.Image.ALIGN_MIDDLE;
  208. document.NewPage();
  209. document.Add(image);
  210. //iTextSharp.text.Chunk c1 = new iTextSharp.text.Chunk("Hello World");
  211. //iTextSharp.text.Phrase p1 = new iTextSharp.text.Phrase();
  212. //p1.Leading = 150; //行间距
  213. //document.Add(p1);
  214. }
  215. Console.WriteLine("转换成功!");
  216. document.Close();
  217. }
  218. catch (Exception ex)
  219. {
  220. Dispatcher.Invoke(() =>
  221. {
  222. btnClose.IsEnabled = true;
  223. btnPrint.IsEnabled = true;
  224. times.Stop();
  225. tip_outer.Visibility = Visibility.Collapsed;
  226. MessageWindow.Show(ex.Message);
  227. return;
  228. });
  229. LogHelper.WriteErrLog("打印转换PDF失败,原因:" + ex.Message, ex);
  230. }
  231. List<string> objStr = (List<string>)obj;
  232. string SourcePath = objStr[0];
  233. int PrinterNum = int.Parse(objStr[1]);
  234. string PrinterName = objStr[2];
  235. //TPF文件位置
  236. string TPFPath = FileToolsCommon.GetFileAbsolutePath("temp/");
  237. FileToolsCommon.CreateDirectory(TPFPath);
  238. TPFPath += "101.TPF";
  239. //生成点阵文件
  240. LatticeFileHelper.GeneratingPDF(SourcePath, TPFPath, out int pr, out string msg, out string outPut/*, pyte*/);
  241. while (!FileToolsCommon.IsExistFile(TPFPath))
  242. {
  243. Thread.Sleep(100);
  244. }
  245. while (string.IsNullOrWhiteSpace(pr.ToString()))
  246. {
  247. Thread.Sleep(100);
  248. }
  249. Thread.Sleep(1000);
  250. //打印
  251. LatticeFileHelper.PrinterTPFFile(TPFPath, PrinterNum, PrinterName, out int printResult, out string standardError, out string standardOutput);
  252. if (printResult == 0)
  253. {
  254. Dispatcher.Invoke(() =>
  255. {
  256. btnClose.IsEnabled = true;
  257. btnPrint.IsEnabled = true;
  258. num = 99;
  259. times.Stop();
  260. pgbProcess.Value = 100;
  261. lbProcess.Content = "100%";
  262. MessageWindow.Show("打印成功!");
  263. tip_outer.Visibility = Visibility.Collapsed;
  264. });
  265. }
  266. else
  267. {
  268. Dispatcher.Invoke(() =>
  269. {
  270. btnClose.IsEnabled = true;
  271. btnPrint.IsEnabled = true;
  272. times.Stop();
  273. tip_outer.Visibility = Visibility.Collapsed;
  274. MessageWindow.Show(standardError);
  275. });
  276. }
  277. }
  278. /// <summary>
  279. /// 返回一个时间戳到毫秒
  280. /// </summary>
  281. /// <returns></returns>
  282. public static long Timestamp()
  283. {
  284. TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
  285. long timestr = Convert.ToInt64(ts.TotalMilliseconds);
  286. return timestr;
  287. }
  288. /// <summary>
  289. /// 生成图片
  290. /// </summary>
  291. /// <param name="_path">图片地址</param>
  292. /// <param name="_saveimg">保存位置</param>
  293. /// <param name="_rectangle">图片位置</param>
  294. /// <param name="errmsg">错误消息</param>
  295. /// <returns></returns>
  296. private bool MergerImg(string _path, string _saveimg, RectangleF _rectangle, out string errmsg)
  297. {
  298. errmsg = null;
  299. try
  300. {
  301. Bitmap bitmap = null;
  302. //创建要显示的图片对象,根据参数的个数设置宽度
  303. Bitmap backgroudImg = new Bitmap((int)/*gridM.ActualWidth*/wit, (int)/*gridM.ActualHeight*/hei);
  304. Graphics g = Graphics.FromImage(backgroudImg);
  305. //清除画布,背景设置为白色
  306. g.Clear(System.Drawing.Color.White);
  307. if (!string.IsNullOrWhiteSpace(_path))
  308. {
  309. bitmap = ImageHelper.ReadBitmapFile(_path);
  310. g.DrawImage(bitmap, _rectangle);
  311. }
  312. #region 添加箭头
  313. string Str = "↑";
  314. SolidBrush mybrush = new SolidBrush(Color.Black); //设置默认画刷颜色
  315. Font myfont = new Font("黑体", 14); //设置默认字体格式
  316. g.DrawString(Str, myfont, mybrush, new Rectangle((int)wit - 50, 20, 50, 50));
  317. #endregion
  318. backgroudImg.Save(_saveimg);
  319. g.Dispose();
  320. backgroudImg.Dispose();
  321. if (bitmap != null)
  322. {
  323. bitmap.Dispose();
  324. }
  325. GC.Collect();
  326. return true;
  327. }
  328. catch (Exception ex)
  329. {
  330. errmsg = ex.Message;
  331. LogHelper.WriteErrLog("【截图合成】(MergerImg)图片合成失败:" + ex.Message, ex);
  332. return false;
  333. }
  334. }
  335. /// <summary>
  336. /// 打印-开始
  337. /// </summary>
  338. /// <returns></returns>
  339. private object InvokeServering()
  340. {
  341. Dispatcher.Invoke(() =>
  342. {
  343. I = 1;
  344. pgbProcess.Value = I * 100 / 100;
  345. lbProcess.Content = I.ToString() + "%";
  346. tip_outer.Visibility = Visibility.Visible;
  347. //I = 0;
  348. });
  349. Thread.Sleep(2000);
  350. string imgPath = FileToolsCommon.GetFileAbsolutePath("temp");
  351. FileToolsCommon.CreateFile(imgPath);
  352. string pdf = imgPath + "\\101.pdf";
  353. string tpf = imgPath + "\\102.TPF";
  354. string msg = string.Empty;
  355. string outPut = string.Empty;
  356. //if(rbnSquarePoint.IsChecked==false)
  357. //{
  358. // pyte = 1;
  359. //}
  360. LatticeFileHelper.GeneratingPDF(pdf, tpf, out int pr, out msg, out outPut/*, pyte*/);
  361. if (pr == 0)
  362. {
  363. //outPut = outPut.Replace("[", "").Replace("]", "").Replace("\"", "").Trim();
  364. //APP.OutPut = outPut.Split(',');
  365. //string []page= outPut.Split(',');
  366. //for(int i=0;i<page.Length;i++) //增加页码编号
  367. //{
  368. // APP.PageDrawList[i].PageCode = page[i];
  369. //}
  370. //string defa = string.Empty;
  371. //List<string> defaList = LatticeFileHelper.GetPrinterList(out defa);
  372. string standardError = string.Empty;
  373. string standardOutput = string.Empty;
  374. int frequency = 1;
  375. //Dispatcher.Invoke(() =>
  376. //{
  377. frequency = Convert.ToInt32(txbNumberOfCopies.Text);
  378. LatticeFileHelper.PrinterTPFFile(tpf, frequency, cmbClass.Text, out int printResult, out standardError, out standardOutput);
  379. if (printResult == 0)// 0为成功
  380. {
  381. new Thread(new ThreadStart(new Action(() =>
  382. {
  383. Dispatcher.Invoke(() =>
  384. {
  385. I = 100;
  386. pgbProcess.Value = I * 100 / 100;
  387. lbProcess.Content = I.ToString() + "%";
  388. APP.myloading.Hide();
  389. I = 0;
  390. });
  391. Thread.Sleep(3000);
  392. Dispatcher.Invoke(() =>
  393. {
  394. tip_outer.Visibility = Visibility.Collapsed;
  395. //tip_outer.Visibility = Visibility.Collapsed;
  396. MessageWindow.Show("打印成功");
  397. });
  398. }))).Start();
  399. }
  400. else
  401. {
  402. Dispatcher.Invoke(() =>
  403. {
  404. APP.myloading.Hide();
  405. MessageWindow.Show(standardError);
  406. });
  407. }
  408. //});
  409. }
  410. else
  411. {
  412. APP.myloading.Hide();
  413. MessageWindow.Show(msg);
  414. }
  415. return "";
  416. }
  417. /// <summary>
  418. /// 打印-结束
  419. /// </summary>
  420. /// <returns></returns>
  421. public void InvokeServerCompate(object obj)
  422. {
  423. try
  424. {
  425. GC.Collect();
  426. APP.myloading.Hide();
  427. }
  428. catch (Exception)
  429. {
  430. }
  431. }
  432. private int I = 0;
  433. private void Times_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
  434. {
  435. Dispatcher.Invoke(() =>
  436. {
  437. if (I <= 90)
  438. {
  439. try
  440. {
  441. pgbProcess.Value = I * 100 / 100;
  442. lbProcess.Content = I.ToString() + "%";
  443. I++;
  444. }
  445. catch (Exception ex)
  446. {
  447. LogHelper.WriteErrLog("【摄像】(Times_Elapsed)生成图片错误:" + ex.Message, ex);
  448. }
  449. }
  450. });
  451. }
  452. /// <summary>
  453. /// 窗口移动
  454. /// </summary>
  455. /// <param name="sender"></param>
  456. /// <param name="e"></param>
  457. private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  458. {
  459. DragMove();
  460. }
  461. /// <summary>
  462. /// 关闭
  463. /// </summary>
  464. /// <param name="sender"></param>
  465. /// <param name="e"></param>
  466. private void BtnClose_Click(object sender, RoutedEventArgs e)
  467. {
  468. Hide();
  469. }
  470. /// <summary>
  471. /// 减
  472. /// </summary>
  473. /// <param name="sender"></param>
  474. /// <param name="e"></param>
  475. private void BtnLess_Click(object sender, RoutedEventArgs e)
  476. {
  477. int num = Convert.ToInt32(txbNumberOfCopies.Text);
  478. if (num > 1)
  479. {
  480. num = num - 1;
  481. txbNumberOfCopies.Text = num.ToString();
  482. }
  483. }
  484. /// <summary>
  485. /// 新增
  486. /// </summary>
  487. /// <param name="sender"></param>
  488. /// <param name="e"></param>
  489. private void BtnAdd_Click(object sender, RoutedEventArgs e)
  490. {
  491. int num = Convert.ToInt32(txbNumberOfCopies.Text);
  492. if (num > 0)
  493. {
  494. num = num + 1;
  495. txbNumberOfCopies.Text = num.ToString();
  496. }
  497. }
  498. private void Window_MouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
  499. {
  500. }
  501. }
  502. }