星火微课系统客户端
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

PrintWindow.xaml.cs 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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. backgroudImg.Save(_saveimg);
  313. g.Dispose();
  314. backgroudImg.Dispose();
  315. if (bitmap != null)
  316. {
  317. bitmap.Dispose();
  318. }
  319. GC.Collect();
  320. return true;
  321. }
  322. catch (Exception ex)
  323. {
  324. errmsg = ex.Message;
  325. LogHelper.WriteErrLog("【截图合成】(MergerImg)图片合成失败:" + ex.Message, ex);
  326. return false;
  327. }
  328. }
  329. /// <summary>
  330. /// 打印-开始
  331. /// </summary>
  332. /// <returns></returns>
  333. private object InvokeServering()
  334. {
  335. Dispatcher.Invoke(() =>
  336. {
  337. I = 1;
  338. pgbProcess.Value = I * 100 / 100;
  339. lbProcess.Content = I.ToString() + "%";
  340. tip_outer.Visibility = Visibility.Visible;
  341. //I = 0;
  342. });
  343. Thread.Sleep(2000);
  344. string imgPath = FileToolsCommon.GetFileAbsolutePath("temp");
  345. FileToolsCommon.CreateFile(imgPath);
  346. string pdf = imgPath + "\\101.pdf";
  347. string tpf = imgPath + "\\102.TPF";
  348. string msg = string.Empty;
  349. string outPut = string.Empty;
  350. //if(rbnSquarePoint.IsChecked==false)
  351. //{
  352. // pyte = 1;
  353. //}
  354. LatticeFileHelper.GeneratingPDF(pdf, tpf, out int pr, out msg, out outPut/*, pyte*/);
  355. if (pr == 0)
  356. {
  357. //outPut = outPut.Replace("[", "").Replace("]", "").Replace("\"", "").Trim();
  358. //APP.OutPut = outPut.Split(',');
  359. //string []page= outPut.Split(',');
  360. //for(int i=0;i<page.Length;i++) //增加页码编号
  361. //{
  362. // APP.PageDrawList[i].PageCode = page[i];
  363. //}
  364. //string defa = string.Empty;
  365. //List<string> defaList = LatticeFileHelper.GetPrinterList(out defa);
  366. string standardError = string.Empty;
  367. string standardOutput = string.Empty;
  368. int frequency = 1;
  369. //Dispatcher.Invoke(() =>
  370. //{
  371. frequency = Convert.ToInt32(txbNumberOfCopies.Text);
  372. LatticeFileHelper.PrinterTPFFile(tpf, frequency, cmbClass.Text, out int printResult, out standardError, out standardOutput);
  373. if (printResult == 0)// 0为成功
  374. {
  375. new Thread(new ThreadStart(new Action(() =>
  376. {
  377. Dispatcher.Invoke(() =>
  378. {
  379. I = 100;
  380. pgbProcess.Value = I * 100 / 100;
  381. lbProcess.Content = I.ToString() + "%";
  382. APP.myloading.Hide();
  383. I = 0;
  384. });
  385. Thread.Sleep(3000);
  386. Dispatcher.Invoke(() =>
  387. {
  388. tip_outer.Visibility = Visibility.Collapsed;
  389. //tip_outer.Visibility = Visibility.Collapsed;
  390. MessageWindow.Show("打印成功");
  391. });
  392. }))).Start();
  393. }
  394. else
  395. {
  396. Dispatcher.Invoke(() =>
  397. {
  398. APP.myloading.Hide();
  399. MessageWindow.Show(standardError);
  400. });
  401. }
  402. //});
  403. }
  404. else
  405. {
  406. APP.myloading.Hide();
  407. MessageWindow.Show(msg);
  408. }
  409. return "";
  410. }
  411. /// <summary>
  412. /// 打印-结束
  413. /// </summary>
  414. /// <returns></returns>
  415. public void InvokeServerCompate(object obj)
  416. {
  417. try
  418. {
  419. GC.Collect();
  420. APP.myloading.Hide();
  421. }
  422. catch (Exception)
  423. {
  424. }
  425. }
  426. private int I = 0;
  427. private void Times_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
  428. {
  429. Dispatcher.Invoke(() =>
  430. {
  431. if (I <= 90)
  432. {
  433. try
  434. {
  435. pgbProcess.Value = I * 100 / 100;
  436. lbProcess.Content = I.ToString() + "%";
  437. I++;
  438. }
  439. catch (Exception ex)
  440. {
  441. LogHelper.WriteErrLog("【摄像】(Times_Elapsed)生成图片错误:" + ex.Message, ex);
  442. }
  443. }
  444. });
  445. }
  446. /// <summary>
  447. /// 窗口移动
  448. /// </summary>
  449. /// <param name="sender"></param>
  450. /// <param name="e"></param>
  451. private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  452. {
  453. DragMove();
  454. }
  455. /// <summary>
  456. /// 关闭
  457. /// </summary>
  458. /// <param name="sender"></param>
  459. /// <param name="e"></param>
  460. private void BtnClose_Click(object sender, RoutedEventArgs e)
  461. {
  462. Hide();
  463. }
  464. /// <summary>
  465. /// 减
  466. /// </summary>
  467. /// <param name="sender"></param>
  468. /// <param name="e"></param>
  469. private void BtnLess_Click(object sender, RoutedEventArgs e)
  470. {
  471. int num = Convert.ToInt32(txbNumberOfCopies.Text);
  472. if (num > 1)
  473. {
  474. num = num - 1;
  475. txbNumberOfCopies.Text = num.ToString();
  476. }
  477. }
  478. /// <summary>
  479. /// 新增
  480. /// </summary>
  481. /// <param name="sender"></param>
  482. /// <param name="e"></param>
  483. private void BtnAdd_Click(object sender, RoutedEventArgs e)
  484. {
  485. int num = Convert.ToInt32(txbNumberOfCopies.Text);
  486. if (num > 0)
  487. {
  488. num = num + 1;
  489. txbNumberOfCopies.Text = num.ToString();
  490. }
  491. }
  492. private void Window_MouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
  493. {
  494. }
  495. }
  496. }