星火微课系统客户端
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

PrintWindow.xaml.cs 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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. this.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. double wit = 0;
  57. 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. //打印数量
  99. int PrinterNum = Convert.ToInt32(txbNumberOfCopies.Text);
  100. StrList.Add(PrinterNum.ToString());
  101. //打印机名称
  102. string PrinterName = cmbClass.Text;
  103. StrList.Add(PrinterName);
  104. num = 0;
  105. tip_outer.Visibility = Visibility.Visible;
  106. myThread.Start(StrList);
  107. times = new System.Timers.Timer(100);
  108. times.Elapsed += Times_ElapsedClick;
  109. times.Start();
  110. }
  111. int num = 0;
  112. /// <summary>
  113. /// 计时器
  114. /// </summary>
  115. /// <param name="sender"></param>
  116. /// <param name="e"></param>
  117. private void Times_ElapsedClick(object sender, System.Timers.ElapsedEventArgs e)
  118. {
  119. Dispatcher.Invoke(() =>
  120. {
  121. pgbProcess.Value = num;
  122. lbProcess.Content = num.ToString()+"%";
  123. if (num < 99)
  124. {
  125. num++;
  126. times.Interval += (num/2);
  127. }
  128. else
  129. {
  130. times.Stop();
  131. }
  132. });
  133. }
  134. /// <summary>
  135. /// 开始打印
  136. /// </summary>
  137. void StartPrint(object obj)
  138. {
  139. try
  140. {
  141. iTextSharp.text.Document document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 0, 0, 0, 0);
  142. //iTextSharp.text.Rectangle page = new iTextSharp.text.Rectangle(iTextSharp.text.PageSize.A4., 250f);//cs
  143. ////设置纸张横向
  144. //document.SetPageSize(iTextSharp.text.PageSize.A4.Rotate());
  145. int ipdf = 102;
  146. string imgPath = FileToolsCommon.GetFileAbsolutePath("temp/");
  147. string tempImgPath = imgPath;
  148. FileToolsCommon.CreateDirectory(imgPath);
  149. imgPath += "101.pdf";
  150. while(File.Exists(imgPath))
  151. {
  152. imgPath = tempImgPath + ipdf.ToString() + ".pdf";
  153. ipdf++;
  154. }
  155. iTextSharp.text.pdf.PdfWriter.GetInstance(document, new FileStream(imgPath, FileMode.Create, FileAccess.ReadWrite));
  156. document.Open();
  157. iTextSharp.text.Image image;
  158. for (int i = 0; i < APP.PageDrawList.Count; i++)
  159. {
  160. long ii = Timestamp();
  161. string directoryPath = AppDomain.CurrentDomain.BaseDirectory + "Temp\\";
  162. string filePathOutPut = Path.Combine(directoryPath, string.Format("print{0}{1}.jpg", ii, i));
  163. RectangleF rectangleFs = new RectangleF();
  164. MergerImg("", filePathOutPut, rectangleFs, out string errmsg);
  165. image = iTextSharp.text.Image.GetInstance(filePathOutPut);
  166. if (string.IsNullOrEmpty(APP.PageDrawList[i].PageImagePath))//没有图片
  167. {
  168. }
  169. else if (APP.PageDrawList[i].ImgDocumentation == true && !APP.PageDrawList[i].Type.Equals("ppt"))
  170. {
  171. image = iTextSharp.text.Image.GetInstance(APP.PageDrawList[i].PageImagePath);
  172. }
  173. else
  174. {
  175. RectangleF rectangleF = new RectangleF(0,0,0,0) ;
  176. Dispatcher.Invoke(() =>
  177. {
  178. rectangleF = new RectangleF
  179. {
  180. Width = (float)APP.PageDrawList[i].ImageSizes.CenterX,
  181. Height = (float)APP.PageDrawList[i].ImageSizes.CenterY,
  182. X = (float)APP.PageDrawList[i].ImageLocation.X,
  183. Y = (float)APP.PageDrawList[i].ImageLocation.Y
  184. };
  185. });
  186. string msgs = string.Empty;
  187. bool isImg = MergerImg(APP.PageDrawList[i].PageImagePath, filePathOutPut, rectangleF, out msgs);
  188. if (isImg)
  189. {
  190. image = iTextSharp.text.Image.GetInstance(filePathOutPut);
  191. }
  192. else
  193. {
  194. image = iTextSharp.text.Image.GetInstance(APP.PageDrawList[i].PageImagePath);
  195. }
  196. }
  197. if (image.Height > iTextSharp.text.PageSize.A4.Height)
  198. {
  199. image.ScaleToFit(iTextSharp.text.PageSize.A4.Width, iTextSharp.text.PageSize.A4.Height);
  200. }
  201. else if (image.Width > iTextSharp.text.PageSize.A4.Width)
  202. {
  203. image.ScaleToFit(iTextSharp.text.PageSize.A4.Width, iTextSharp.text.PageSize.A4.Height);
  204. }
  205. image.Alignment = iTextSharp.text.Image.ALIGN_MIDDLE;
  206. document.NewPage();
  207. document.Add(image);
  208. //iTextSharp.text.Chunk c1 = new iTextSharp.text.Chunk("Hello World");
  209. //iTextSharp.text.Phrase p1 = new iTextSharp.text.Phrase();
  210. //p1.Leading = 150; //行间距
  211. //document.Add(p1);
  212. }
  213. Console.WriteLine("转换成功!");
  214. document.Close();
  215. }
  216. catch (Exception ex)
  217. {
  218. Dispatcher.Invoke(() =>
  219. {
  220. btnClose.IsEnabled = true;
  221. btnPrint.IsEnabled = true;
  222. times.Stop();
  223. tip_outer.Visibility = Visibility.Collapsed;
  224. MessageWindow.Show(ex.Message);
  225. return;
  226. });
  227. LogHelper.WriteErrLog("打印转换PDF失败,原因:" + ex.Message,ex);
  228. }
  229. List<string> objStr = (List<string>)obj;
  230. string SourcePath= objStr[0];
  231. int PrinterNum=int.Parse(objStr[1]);
  232. string PrinterName= objStr[2];
  233. //TPF文件位置
  234. string TPFPath = FileToolsCommon.GetFileAbsolutePath("temp/");
  235. FileToolsCommon.CreateDirectory(TPFPath);
  236. TPFPath += "101.TPF";
  237. //生成点阵文件
  238. LatticeFileHelper.GeneratingPDF(SourcePath, TPFPath, out int pr, out string msg, out string outPut/*, pyte*/);
  239. while(!FileToolsCommon.IsExistFile(TPFPath))
  240. {
  241. Thread.Sleep(100);
  242. }
  243. while(string.IsNullOrWhiteSpace(pr.ToString()))
  244. {
  245. Thread.Sleep(100);
  246. }
  247. Thread.Sleep(1000);
  248. //打印
  249. LatticeFileHelper.PrinterTPFFile(TPFPath, PrinterNum, PrinterName, out int printResult, out string standardError, out string standardOutput);
  250. if (printResult == 0)
  251. {
  252. Dispatcher.Invoke(() =>
  253. {
  254. btnClose.IsEnabled = true;
  255. btnPrint.IsEnabled = true;
  256. num = 99;
  257. times.Stop();
  258. pgbProcess.Value = 100;
  259. lbProcess.Content = "100%";
  260. MessageWindow.Show("打印成功!");
  261. tip_outer.Visibility = Visibility.Collapsed;
  262. });
  263. }
  264. else
  265. {
  266. Dispatcher.Invoke(() =>
  267. {
  268. btnClose.IsEnabled = true;
  269. btnPrint.IsEnabled = true;
  270. times.Stop();
  271. tip_outer.Visibility = Visibility.Collapsed;
  272. MessageWindow.Show(standardError);
  273. });
  274. }
  275. }
  276. /// <summary>
  277. /// 返回一个时间戳到毫秒
  278. /// </summary>
  279. /// <returns></returns>
  280. public static long Timestamp()
  281. {
  282. TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
  283. long timestr = Convert.ToInt64(ts.TotalMilliseconds);
  284. return timestr;
  285. }
  286. /// <summary>
  287. /// 生成图片
  288. /// </summary>
  289. /// <param name="_path">图片地址</param>
  290. /// <param name="_saveimg">保存位置</param>
  291. /// <param name="_rectangle">图片位置</param>
  292. /// <param name="errmsg">错误消息</param>
  293. /// <returns></returns>
  294. private bool MergerImg(string _path, string _saveimg, RectangleF _rectangle, out string errmsg)
  295. {
  296. errmsg = null;
  297. try
  298. {
  299. Bitmap bitmap = null;
  300. //创建要显示的图片对象,根据参数的个数设置宽度
  301. Bitmap backgroudImg = new Bitmap((int)/*gridM.ActualWidth*/wit, (int)/*gridM.ActualHeight*/hei);
  302. Graphics g = Graphics.FromImage(backgroudImg);
  303. //清除画布,背景设置为白色
  304. g.Clear(System.Drawing.Color.White);
  305. if (!string.IsNullOrWhiteSpace(_path))
  306. {
  307. bitmap = ImageHelper.ReadBitmapFile(_path);
  308. g.DrawImage(bitmap, _rectangle);
  309. }
  310. backgroudImg.Save(_saveimg);
  311. g.Dispose();
  312. backgroudImg.Dispose();
  313. if (bitmap != null)
  314. {
  315. bitmap.Dispose();
  316. }
  317. GC.Collect();
  318. return true;
  319. }
  320. catch (Exception ex)
  321. {
  322. errmsg = ex.Message;
  323. LogHelper.WriteErrLog("【截图合成】(MergerImg)图片合成失败:" + ex.Message, ex);
  324. return false;
  325. }
  326. }
  327. /// <summary>
  328. /// 打印-开始
  329. /// </summary>
  330. /// <returns></returns>
  331. private object InvokeServering()
  332. {
  333. Dispatcher.Invoke(() =>
  334. {
  335. I = 1;
  336. pgbProcess.Value = I * 100 / 100;
  337. lbProcess.Content = I.ToString() + "%";
  338. tip_outer.Visibility = Visibility.Visible;
  339. //I = 0;
  340. });
  341. Thread.Sleep(2000);
  342. string imgPath = FileToolsCommon.GetFileAbsolutePath("temp");
  343. FileToolsCommon.CreateFile(imgPath);
  344. string pdf = imgPath + "\\101.pdf";
  345. string tpf = imgPath + "\\102.TPF";
  346. string msg = string.Empty;
  347. string outPut = string.Empty;
  348. //if(rbnSquarePoint.IsChecked==false)
  349. //{
  350. // pyte = 1;
  351. //}
  352. LatticeFileHelper.GeneratingPDF(pdf, tpf, out int pr, out msg, out outPut/*, pyte*/);
  353. if (pr == 0)
  354. {
  355. //outPut = outPut.Replace("[", "").Replace("]", "").Replace("\"", "").Trim();
  356. //APP.OutPut = outPut.Split(',');
  357. //string []page= outPut.Split(',');
  358. //for(int i=0;i<page.Length;i++) //增加页码编号
  359. //{
  360. // APP.PageDrawList[i].PageCode = page[i];
  361. //}
  362. //string defa = string.Empty;
  363. //List<string> defaList = LatticeFileHelper.GetPrinterList(out defa);
  364. string standardError = string.Empty;
  365. string standardOutput = string.Empty;
  366. int frequency = 1;
  367. //Dispatcher.Invoke(() =>
  368. //{
  369. frequency = Convert.ToInt32(txbNumberOfCopies.Text);
  370. LatticeFileHelper.PrinterTPFFile(tpf, frequency, cmbClass.Text, out int printResult, out standardError, out standardOutput);
  371. if (printResult == 0)// 0为成功
  372. {
  373. new Thread(new ThreadStart(new Action(() =>
  374. {
  375. Dispatcher.Invoke(() =>
  376. {
  377. I = 100;
  378. pgbProcess.Value = I * 100 / 100;
  379. lbProcess.Content = I.ToString() + "%";
  380. APP.myloading.Hide();
  381. I = 0;
  382. });
  383. Thread.Sleep(3000);
  384. Dispatcher.Invoke(() =>
  385. {
  386. tip_outer.Visibility = Visibility.Collapsed;
  387. //tip_outer.Visibility = Visibility.Collapsed;
  388. MessageWindow.Show("打印成功");
  389. });
  390. }))).Start();
  391. }
  392. else
  393. {
  394. Dispatcher.Invoke(() =>
  395. {
  396. APP.myloading.Hide();
  397. MessageWindow.Show(standardError);
  398. });
  399. }
  400. //});
  401. }
  402. else
  403. {
  404. APP.myloading.Hide();
  405. MessageWindow.Show(msg);
  406. }
  407. return "";
  408. }
  409. /// <summary>
  410. /// 打印-结束
  411. /// </summary>
  412. /// <returns></returns>
  413. public void InvokeServerCompate(object obj)
  414. {
  415. try
  416. {
  417. GC.Collect();
  418. APP.myloading.Hide();
  419. }
  420. catch (Exception)
  421. {
  422. }
  423. }
  424. private int I = 0;
  425. private void Times_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
  426. {
  427. Dispatcher.Invoke(() =>
  428. {
  429. if (I <= 90)
  430. {
  431. try
  432. {
  433. pgbProcess.Value = I * 100 / 100;
  434. lbProcess.Content = I.ToString() + "%";
  435. I++;
  436. }
  437. catch (Exception ex)
  438. {
  439. LogHelper.WriteErrLog("【摄像】(Times_Elapsed)生成图片错误:" + ex.Message, ex);
  440. }
  441. }
  442. });
  443. }
  444. /// <summary>
  445. /// 窗口移动
  446. /// </summary>
  447. /// <param name="sender"></param>
  448. /// <param name="e"></param>
  449. private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  450. {
  451. DragMove();
  452. }
  453. /// <summary>
  454. /// 关闭
  455. /// </summary>
  456. /// <param name="sender"></param>
  457. /// <param name="e"></param>
  458. private void BtnClose_Click(object sender, RoutedEventArgs e)
  459. {
  460. Hide();
  461. }
  462. /// <summary>
  463. /// 减
  464. /// </summary>
  465. /// <param name="sender"></param>
  466. /// <param name="e"></param>
  467. private void BtnLess_Click(object sender, RoutedEventArgs e)
  468. {
  469. int num = Convert.ToInt32(txbNumberOfCopies.Text);
  470. if (num > 1)
  471. {
  472. num = num - 1;
  473. txbNumberOfCopies.Text = num.ToString();
  474. }
  475. }
  476. /// <summary>
  477. /// 新增
  478. /// </summary>
  479. /// <param name="sender"></param>
  480. /// <param name="e"></param>
  481. private void BtnAdd_Click(object sender, RoutedEventArgs e)
  482. {
  483. int num = Convert.ToInt32(txbNumberOfCopies.Text);
  484. if (num > 0)
  485. {
  486. num = num + 1;
  487. txbNumberOfCopies.Text = num.ToString();
  488. }
  489. }
  490. private void Window_MouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
  491. {
  492. }
  493. }
  494. }