星火微课系统客户端
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.

PrintWindowTStudy.xaml.cs 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  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 PrintWindowTStudy : 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 PrintWindowTStudy()
  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. #region 腾千里服务打印
  240. //if (APP.dAL_TmatrixCode == null)
  241. //{
  242. // APP.dAL_TmatrixCode = new DAL.DAL_TmatrixCode();
  243. //}
  244. //string ErrMessage;
  245. //if(APP.dAL_TmatrixCode.TQLUserLogin(out ErrMessage))
  246. //{
  247. // if(APP.dAL_TmatrixCode.TQLStartTmatrix(TPFPath, "101.TPF", out ErrMessage))
  248. // {
  249. // while(true)
  250. // {
  251. // //-1失败 0:未铺码 1:正在铺码 2:铺码完成,3:未查到。
  252. // int TmatrixType = APP.dAL_TmatrixCode.TQLSchedulequery(out ErrMessage);
  253. // if(TmatrixType==0)
  254. // {
  255. // //未铺码
  256. // Thread.Sleep(3000);
  257. // }
  258. // else if(TmatrixType==1)
  259. // {
  260. // //正在铺码
  261. // Thread.Sleep(1000);
  262. // }
  263. // else if (TmatrixType == 2)
  264. // {
  265. // //铺码完成
  266. // Dispatcher.Invoke(() =>
  267. // {
  268. // btnClose.IsEnabled = true;
  269. // btnPrint.IsEnabled = true;
  270. // num = 99;
  271. // times.Stop();
  272. // pgbProcess.Value = 100;
  273. // lbProcess.Content = "100%";
  274. // MessageWindow.Show("打印成功!");
  275. // tip_outer.Visibility = Visibility.Collapsed;
  276. // });
  277. // }
  278. // else if (TmatrixType == 3)
  279. // {
  280. // //未查到
  281. // Dispatcher.Invoke(() =>
  282. // {
  283. // btnClose.IsEnabled = true;
  284. // btnPrint.IsEnabled = true;
  285. // times.Stop();
  286. // tip_outer.Visibility = Visibility.Collapsed;
  287. // MessageWindow.Show("文件铺码失败," + ErrMessage);
  288. // });
  289. // break;
  290. // }
  291. // else //if (TmatrixType == -1)
  292. // {
  293. // Dispatcher.Invoke(() =>
  294. // {
  295. // btnClose.IsEnabled = true;
  296. // btnPrint.IsEnabled = true;
  297. // times.Stop();
  298. // tip_outer.Visibility = Visibility.Collapsed;
  299. // MessageWindow.Show("文件铺码失败," + ErrMessage);
  300. // });
  301. // break;
  302. // }
  303. // }
  304. // }
  305. // else
  306. // {
  307. // Dispatcher.Invoke(() =>
  308. // {
  309. // btnClose.IsEnabled = true;
  310. // btnPrint.IsEnabled = true;
  311. // times.Stop();
  312. // tip_outer.Visibility = Visibility.Collapsed;
  313. // MessageWindow.Show("文件铺码失败,"+ErrMessage);
  314. // });
  315. // }
  316. //}
  317. //else
  318. //{
  319. // Dispatcher.Invoke(() =>
  320. // {
  321. // btnClose.IsEnabled = true;
  322. // btnPrint.IsEnabled = true;
  323. // times.Stop();
  324. // tip_outer.Visibility = Visibility.Collapsed;
  325. // MessageWindow.Show("登录铺码服务器失败,"+ErrMessage);
  326. // });
  327. //}
  328. #endregion
  329. #region 拓思德生成点阵文件
  330. //生成点阵文件
  331. LatticeFileHelper.GeneratingPDF(SourcePath, TPFPath, out int pr, out string msg, out string outPut/*, pyte*/);
  332. while (!FileToolsCommon.IsExistFile(TPFPath))
  333. {
  334. Thread.Sleep(100);
  335. }
  336. while (string.IsNullOrWhiteSpace(pr.ToString()))
  337. {
  338. Thread.Sleep(100);
  339. }
  340. Thread.Sleep(1000);
  341. #endregion
  342. #region 拓思德打印
  343. //打印
  344. LatticeFileHelper.PrinterTPFFile(TPFPath, PrinterNum, PrinterName, out int printResult, out string standardError, out string standardOutput);
  345. if (printResult == 0)
  346. {
  347. Dispatcher.Invoke(() =>
  348. {
  349. btnClose.IsEnabled = true;
  350. btnPrint.IsEnabled = true;
  351. num = 99;
  352. times.Stop();
  353. pgbProcess.Value = 100;
  354. lbProcess.Content = "100%";
  355. MessageWindow.Show("打印成功!");
  356. tip_outer.Visibility = Visibility.Collapsed;
  357. });
  358. }
  359. else
  360. {
  361. Dispatcher.Invoke(() =>
  362. {
  363. btnClose.IsEnabled = true;
  364. btnPrint.IsEnabled = true;
  365. times.Stop();
  366. tip_outer.Visibility = Visibility.Collapsed;
  367. MessageWindow.Show(standardError);
  368. });
  369. }
  370. #endregion
  371. }
  372. /// <summary>
  373. /// 返回一个时间戳到毫秒
  374. /// </summary>
  375. /// <returns></returns>
  376. public static long Timestamp()
  377. {
  378. TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
  379. long timestr = Convert.ToInt64(ts.TotalMilliseconds);
  380. return timestr;
  381. }
  382. /// <summary>
  383. /// 生成图片
  384. /// </summary>
  385. /// <param name="_path">图片地址</param>
  386. /// <param name="_saveimg">保存位置</param>
  387. /// <param name="_rectangle">图片位置</param>
  388. /// <param name="errmsg">错误消息</param>
  389. /// <returns></returns>
  390. private bool MergerImg(string _path, string _saveimg, RectangleF _rectangle, out string errmsg)
  391. {
  392. errmsg = null;
  393. try
  394. {
  395. Bitmap bitmap = null;
  396. //创建要显示的图片对象,根据参数的个数设置宽度
  397. Bitmap backgroudImg = new Bitmap((int)/*gridM.ActualWidth*/wit, (int)/*gridM.ActualHeight*/hei);
  398. Graphics g = Graphics.FromImage(backgroudImg);
  399. //清除画布,背景设置为白色
  400. g.Clear(System.Drawing.Color.White);
  401. if (!string.IsNullOrWhiteSpace(_path))
  402. {
  403. bitmap = ImageHelper.ReadBitmapFile(_path);
  404. g.DrawImage(bitmap, _rectangle);
  405. }
  406. #region 添加箭头
  407. string Str = "↑";
  408. SolidBrush mybrush = new SolidBrush(Color.Black); //设置默认画刷颜色
  409. Font myfont = new Font("黑体", 14); //设置默认字体格式
  410. g.DrawString(Str, myfont, mybrush, new Rectangle((int)wit - 50, 20, 50, 50));
  411. #endregion
  412. backgroudImg.Save(_saveimg);
  413. g.Dispose();
  414. backgroudImg.Dispose();
  415. if (bitmap != null)
  416. {
  417. bitmap.Dispose();
  418. }
  419. GC.Collect();
  420. return true;
  421. }
  422. catch (Exception ex)
  423. {
  424. errmsg = ex.Message;
  425. LogHelper.WriteErrLog("【截图合成】(MergerImg)图片合成失败:" + ex.Message, ex);
  426. return false;
  427. }
  428. }
  429. /// <summary>
  430. /// 打印-开始
  431. /// </summary>
  432. /// <returns></returns>
  433. private object InvokeServering()
  434. {
  435. Dispatcher.Invoke(() =>
  436. {
  437. I = 1;
  438. pgbProcess.Value = I * 100 / 100;
  439. lbProcess.Content = I.ToString() + "%";
  440. tip_outer.Visibility = Visibility.Visible;
  441. //I = 0;
  442. });
  443. Thread.Sleep(2000);
  444. string imgPath = FileToolsCommon.GetFileAbsolutePath("temp");
  445. FileToolsCommon.CreateFile(imgPath);
  446. string pdf = imgPath + "\\101.pdf";
  447. string tpf = imgPath + "\\102.TPF";
  448. string msg = string.Empty;
  449. string outPut = string.Empty;
  450. //if(rbnSquarePoint.IsChecked==false)
  451. //{
  452. // pyte = 1;
  453. //}
  454. LatticeFileHelper.GeneratingPDF(pdf, tpf, out int pr, out msg, out outPut/*, pyte*/);
  455. if (pr == 0)
  456. {
  457. //outPut = outPut.Replace("[", "").Replace("]", "").Replace("\"", "").Trim();
  458. //APP.OutPut = outPut.Split(',');
  459. //string []page= outPut.Split(',');
  460. //for(int i=0;i<page.Length;i++) //增加页码编号
  461. //{
  462. // APP.PageDrawList[i].PageCode = page[i];
  463. //}
  464. //string defa = string.Empty;
  465. //List<string> defaList = LatticeFileHelper.GetPrinterList(out defa);
  466. string standardError = string.Empty;
  467. string standardOutput = string.Empty;
  468. int frequency = 1;
  469. //Dispatcher.Invoke(() =>
  470. //{
  471. frequency = Convert.ToInt32(txbNumberOfCopies.Text);
  472. LatticeFileHelper.PrinterTPFFile(tpf, frequency, cmbClass.Text, out int printResult, out standardError, out standardOutput);
  473. if (printResult == 0)// 0为成功
  474. {
  475. new Thread(new ThreadStart(new Action(() =>
  476. {
  477. Dispatcher.Invoke(() =>
  478. {
  479. I = 100;
  480. pgbProcess.Value = I * 100 / 100;
  481. lbProcess.Content = I.ToString() + "%";
  482. APP.myloading.Hide();
  483. I = 0;
  484. });
  485. Thread.Sleep(3000);
  486. Dispatcher.Invoke(() =>
  487. {
  488. tip_outer.Visibility = Visibility.Collapsed;
  489. //tip_outer.Visibility = Visibility.Collapsed;
  490. MessageWindow.Show("打印成功");
  491. });
  492. }))).Start();
  493. }
  494. else
  495. {
  496. Dispatcher.Invoke(() =>
  497. {
  498. APP.myloading.Hide();
  499. MessageWindow.Show(standardError);
  500. });
  501. }
  502. //});
  503. }
  504. else
  505. {
  506. APP.myloading.Hide();
  507. MessageWindow.Show(msg);
  508. }
  509. return "";
  510. }
  511. /// <summary>
  512. /// 打印-结束
  513. /// </summary>
  514. /// <returns></returns>
  515. public void InvokeServerCompate(object obj)
  516. {
  517. try
  518. {
  519. GC.Collect();
  520. APP.myloading.Hide();
  521. }
  522. catch (Exception)
  523. {
  524. }
  525. }
  526. private int I = 0;
  527. private void Times_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
  528. {
  529. Dispatcher.Invoke(() =>
  530. {
  531. if (I <= 90)
  532. {
  533. try
  534. {
  535. pgbProcess.Value = I * 100 / 100;
  536. lbProcess.Content = I.ToString() + "%";
  537. I++;
  538. }
  539. catch (Exception ex)
  540. {
  541. LogHelper.WriteErrLog("【摄像】(Times_Elapsed)生成图片错误:" + ex.Message, ex);
  542. }
  543. }
  544. });
  545. }
  546. /// <summary>
  547. /// 窗口移动
  548. /// </summary>
  549. /// <param name="sender"></param>
  550. /// <param name="e"></param>
  551. private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  552. {
  553. DragMove();
  554. }
  555. /// <summary>
  556. /// 关闭
  557. /// </summary>
  558. /// <param name="sender"></param>
  559. /// <param name="e"></param>
  560. private void BtnClose_Click(object sender, RoutedEventArgs e)
  561. {
  562. Hide();
  563. }
  564. /// <summary>
  565. /// 减
  566. /// </summary>
  567. /// <param name="sender"></param>
  568. /// <param name="e"></param>
  569. private void BtnLess_Click(object sender, RoutedEventArgs e)
  570. {
  571. int num = Convert.ToInt32(txbNumberOfCopies.Text);
  572. if (num > 1)
  573. {
  574. num = num - 1;
  575. txbNumberOfCopies.Text = num.ToString();
  576. }
  577. }
  578. /// <summary>
  579. /// 新增
  580. /// </summary>
  581. /// <param name="sender"></param>
  582. /// <param name="e"></param>
  583. private void BtnAdd_Click(object sender, RoutedEventArgs e)
  584. {
  585. int num = Convert.ToInt32(txbNumberOfCopies.Text);
  586. if (num > 0)
  587. {
  588. num = num + 1;
  589. txbNumberOfCopies.Text = num.ToString();
  590. }
  591. }
  592. }
  593. }