123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534 |
- using Common.system;
-
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Drawing;
- using System.IO;
- using System.Threading;
- using System.Windows;
- using System.Windows.Input;
- using System.Windows.Media.Imaging;
- using System.Windows.Threading;
-
- namespace XHWK.WKTool
- {
- /// <summary>
- /// 打印
- /// </summary>
- public partial class PrintWindow : Window
- {
- /// <summary>
- /// 下拉框数据源
- /// </summary>
- public DataTable data = new DataTable();
- private DataTable dtComponentsUniqueNo;
-
- private System.Timers.Timer times;
- public PrintWindow()
- {
- InitializeComponent();
- double proportion = 1036.0 / 1290.0;
- double Widths = (PrimaryScreen.ScaleWorkingAreaSize.Height) / proportion - (BorderThickness.Left + BorderThickness.Right);
- double Heights = PrimaryScreen.ScaleWorkingAreaSize.Height - (BorderThickness.Top + BorderThickness.Bottom);
- Width = Widths / 1.42;
- Height = Heights / 1.71;
- GridContent.Width = Width + 5;
- GridContent.Height = Height;
- ResizeMode = ResizeMode.NoResize;
- string defa = string.Empty;
- List<string> defaList = LatticeFileHelper.GetPrinterList(out defa);
- if (defaList.Count > 0)
- {
- data.Columns.Add("Value");
- data.Columns.Add("Key");
- for (int i = 0; i < defaList.Count; i++)
- {
- //创建一行
- DataRow row = data.NewRow();
- //将此行添加到table中
- data.Rows.Add(row);
- data.Rows[i]["Value"] = defaList[i];
- data.Rows[i]["Key"] = i.ToString();
- }
- dtComponentsUniqueNo = data.DefaultView.ToTable();
- cmbClass.ItemsSource = dtComponentsUniqueNo.DefaultView;
- cmbClass.SelectedIndex = 0;
- }
- }
-
- private double wit = 0;
- private double hei = 0;
- public void Initialize(string _imgPath, double _wit, double _hei)
- {
- wit = _wit;
- hei = _hei;
- if (!string.IsNullOrWhiteSpace(_imgPath))
- {
- imgPri.Source = new BitmapImage(new Uri(_imgPath));
- }
- else
- {
- imgPri.Source = null;
- }
- I = 0;
- tip_outer.Visibility = Visibility.Collapsed;
- }
- /// <summary>
- /// 打印
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void BtnPrint_Click(object sender, RoutedEventArgs e)
- {
- if (string.IsNullOrWhiteSpace(cmbClass.Text))
- {
- //MessageWindow.Show("打印机不能为空!");
- MessageWindow.Show("打印机不能为空!");
- return;
- }
- //APP.myloading.Show();
- btnClose.IsEnabled = false;
- btnPrint.IsEnabled = false;
- //APP.BackgroundWorkerHelper.RunWorkerAsync(InvokeServering, InvokeServerCompate);
- Thread myThread = new Thread(StartPrint);
- List<string> StrList = new List<string>();
-
- //string imgPath =
- //FileToolsCommon.CreateFile(imgPath);
- //string pdf = imgPath + "\\101.pdf";
- //PDF位置
- //string SourcePath = FileToolsCommon.GetFileAbsolutePath("/temp/101.pdf");
- //StrList.Add(SourcePath);
- int ipdf = 102;
- string imgPath = FileToolsCommon.GetFileAbsolutePath("temp/");
- string tempImgPath = imgPath;
- imgPath += "101.pdf";
- while (File.Exists(imgPath))
- {
- imgPath = tempImgPath + ipdf.ToString() + ".pdf";
- ipdf++;
- }
- StrList.Add(imgPath);
- //打印数量
- int PrinterNum = Convert.ToInt32(txbNumberOfCopies.Text);
- StrList.Add(PrinterNum.ToString());
- //打印机名称
- string PrinterName = cmbClass.Text;
- StrList.Add(PrinterName);
-
- num = 0;
- tip_outer.Visibility = Visibility.Visible;
- myThread.Start(StrList);
- times = new System.Timers.Timer(100);
- times.Elapsed += Times_ElapsedClick;
- times.Start();
- }
-
- private int num = 0;
- /// <summary>
- /// 计时器
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void Times_ElapsedClick(object sender, System.Timers.ElapsedEventArgs e)
- {
- Dispatcher.Invoke(() =>
- {
- pgbProcess.Value = num;
- lbProcess.Content = num.ToString() + "%";
- if (num < 99)
- {
- num++;
- times.Interval += (num / 2);
- }
- else
- {
- times.Stop();
- }
- });
- }
-
- /// <summary>
- /// 开始打印
- /// </summary>
- private void StartPrint(object obj)
- {
-
- try
- {
- iTextSharp.text.Document document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 0, 0, 0, 0);
- //iTextSharp.text.Rectangle page = new iTextSharp.text.Rectangle(iTextSharp.text.PageSize.A4., 250f);//cs
- ////设置纸张横向
- //document.SetPageSize(iTextSharp.text.PageSize.A4.Rotate());
-
- List<string> objStr1 = (List<string>)obj;
- string SourcePath1 = objStr1[0];
- iTextSharp.text.pdf.PdfWriter.GetInstance(document, new FileStream(SourcePath1, FileMode.Create, FileAccess.ReadWrite));
- document.Open();
- iTextSharp.text.Image image;
- for (int i = 0; i < APP.PageDrawList.Count; i++)
- {
- long ii = Timestamp();
- string directoryPath = AppDomain.CurrentDomain.BaseDirectory + "Temp\\";
- string filePathOutPut = Path.Combine(directoryPath, string.Format("print{0}{1}.jpg", ii, i));
- RectangleF rectangleFs = new RectangleF();
- MergerImg("", filePathOutPut, rectangleFs, out string errmsg);
- image = iTextSharp.text.Image.GetInstance(filePathOutPut);
- if (string.IsNullOrEmpty(APP.PageDrawList[i].PageImagePath))//没有图片
- {
-
- }
- else if (APP.PageDrawList[i].ImgDocumentation == true && !APP.PageDrawList[i].Type.Equals("ppt"))
- {
- image = iTextSharp.text.Image.GetInstance(APP.PageDrawList[i].PageImagePath);
- }
- else
- {
- RectangleF rectangleF = new RectangleF(0, 0, 0, 0);
- Dispatcher.Invoke(() =>
- {
- rectangleF = new RectangleF
- {
- Width = (float)APP.PageDrawList[i].ImageSizes.CenterX,
- Height = (float)APP.PageDrawList[i].ImageSizes.CenterY,
- X = (float)APP.PageDrawList[i].ImageLocation.X,
- Y = (float)APP.PageDrawList[i].ImageLocation.Y
- };
- });
-
-
-
- string msgs = string.Empty;
-
-
- bool isImg = MergerImg(APP.PageDrawList[i].PageImagePath, filePathOutPut, rectangleF, out msgs);
- if (isImg)
- {
- image = iTextSharp.text.Image.GetInstance(filePathOutPut);
- }
- else
- {
- image = iTextSharp.text.Image.GetInstance(APP.PageDrawList[i].PageImagePath);
- }
-
-
- }
- if (image.Height > iTextSharp.text.PageSize.A4.Height)
- {
- image.ScaleToFit(iTextSharp.text.PageSize.A4.Width, iTextSharp.text.PageSize.A4.Height);
- }
- else if (image.Width > iTextSharp.text.PageSize.A4.Width)
- {
- image.ScaleToFit(iTextSharp.text.PageSize.A4.Width, iTextSharp.text.PageSize.A4.Height);
- }
- image.Alignment = iTextSharp.text.Image.ALIGN_MIDDLE;
- document.NewPage();
- document.Add(image);
- //iTextSharp.text.Chunk c1 = new iTextSharp.text.Chunk("Hello World");
- //iTextSharp.text.Phrase p1 = new iTextSharp.text.Phrase();
- //p1.Leading = 150; //行间距
- //document.Add(p1);
- }
- Console.WriteLine("转换成功!");
- document.Close();
- }
- catch (Exception ex)
- {
-
-
-
- Dispatcher.Invoke(() =>
- {
- btnClose.IsEnabled = true;
- btnPrint.IsEnabled = true;
- times.Stop();
- tip_outer.Visibility = Visibility.Collapsed;
- MessageWindow.Show(ex.Message);
- return;
- });
- LogHelper.WriteErrLog("打印转换PDF失败,原因:" + ex.Message, ex);
- }
-
-
-
- List<string> objStr = (List<string>)obj;
- string SourcePath = objStr[0];
- int PrinterNum = int.Parse(objStr[1]);
- string PrinterName = objStr[2];
- //TPF文件位置
- string TPFPath = FileToolsCommon.GetFileAbsolutePath("temp/");
- FileToolsCommon.CreateDirectory(TPFPath);
- TPFPath += "101.TPF";
- //生成点阵文件
- LatticeFileHelper.GeneratingPDF(SourcePath, TPFPath, out int pr, out string msg, out string outPut/*, pyte*/);
- while (!FileToolsCommon.IsExistFile(TPFPath))
- {
- Thread.Sleep(100);
- }
- while (string.IsNullOrWhiteSpace(pr.ToString()))
- {
- Thread.Sleep(100);
- }
- Thread.Sleep(1000);
- //打印
- LatticeFileHelper.PrinterTPFFile(TPFPath, PrinterNum, PrinterName, out int printResult, out string standardError, out string standardOutput);
- if (printResult == 0)
- {
- Dispatcher.Invoke(() =>
- {
- btnClose.IsEnabled = true;
- btnPrint.IsEnabled = true;
- num = 99;
- times.Stop();
- pgbProcess.Value = 100;
- lbProcess.Content = "100%";
- MessageWindow.Show("打印成功!");
- tip_outer.Visibility = Visibility.Collapsed;
- });
- }
- else
- {
- Dispatcher.Invoke(() =>
- {
- btnClose.IsEnabled = true;
- btnPrint.IsEnabled = true;
- times.Stop();
- tip_outer.Visibility = Visibility.Collapsed;
- MessageWindow.Show(standardError);
- });
- }
- }
- /// <summary>
- /// 返回一个时间戳到毫秒
- /// </summary>
- /// <returns></returns>
- public static long Timestamp()
- {
- TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
- long timestr = Convert.ToInt64(ts.TotalMilliseconds);
- return timestr;
- }
- /// <summary>
- /// 生成图片
- /// </summary>
- /// <param name="_path">图片地址</param>
- /// <param name="_saveimg">保存位置</param>
- /// <param name="_rectangle">图片位置</param>
- /// <param name="errmsg">错误消息</param>
- /// <returns></returns>
- private bool MergerImg(string _path, string _saveimg, RectangleF _rectangle, out string errmsg)
- {
- errmsg = null;
- try
- {
- Bitmap bitmap = null;
-
- //创建要显示的图片对象,根据参数的个数设置宽度
- Bitmap backgroudImg = new Bitmap((int)/*gridM.ActualWidth*/wit, (int)/*gridM.ActualHeight*/hei);
- Graphics g = Graphics.FromImage(backgroudImg);
- //清除画布,背景设置为白色
- g.Clear(System.Drawing.Color.White);
-
- if (!string.IsNullOrWhiteSpace(_path))
- {
- bitmap = ImageHelper.ReadBitmapFile(_path);
- g.DrawImage(bitmap, _rectangle);
- }
-
-
- backgroudImg.Save(_saveimg);
-
- g.Dispose();
- backgroudImg.Dispose();
- if (bitmap != null)
- {
- bitmap.Dispose();
- }
-
- GC.Collect();
- return true;
- }
- catch (Exception ex)
- {
- errmsg = ex.Message;
- LogHelper.WriteErrLog("【截图合成】(MergerImg)图片合成失败:" + ex.Message, ex);
- return false;
- }
-
- }
- /// <summary>
- /// 打印-开始
- /// </summary>
- /// <returns></returns>
- private object InvokeServering()
- {
- Dispatcher.Invoke(() =>
- {
- I = 1;
- pgbProcess.Value = I * 100 / 100;
- lbProcess.Content = I.ToString() + "%";
- tip_outer.Visibility = Visibility.Visible;
- //I = 0;
- });
- Thread.Sleep(2000);
-
- string imgPath = FileToolsCommon.GetFileAbsolutePath("temp");
- FileToolsCommon.CreateFile(imgPath);
- string pdf = imgPath + "\\101.pdf";
- string tpf = imgPath + "\\102.TPF";
- string msg = string.Empty;
- string outPut = string.Empty;
- //if(rbnSquarePoint.IsChecked==false)
- //{
- // pyte = 1;
- //}
- LatticeFileHelper.GeneratingPDF(pdf, tpf, out int pr, out msg, out outPut/*, pyte*/);
- if (pr == 0)
- {
- //outPut = outPut.Replace("[", "").Replace("]", "").Replace("\"", "").Trim();
- //APP.OutPut = outPut.Split(',');
- //string []page= outPut.Split(',');
- //for(int i=0;i<page.Length;i++) //增加页码编号
- //{
- // APP.PageDrawList[i].PageCode = page[i];
- //}
-
- //string defa = string.Empty;
- //List<string> defaList = LatticeFileHelper.GetPrinterList(out defa);
-
- string standardError = string.Empty;
- string standardOutput = string.Empty;
- int frequency = 1;
- //Dispatcher.Invoke(() =>
- //{
- frequency = Convert.ToInt32(txbNumberOfCopies.Text);
- LatticeFileHelper.PrinterTPFFile(tpf, frequency, cmbClass.Text, out int printResult, out standardError, out standardOutput);
- if (printResult == 0)// 0为成功
- {
- new Thread(new ThreadStart(new Action(() =>
- {
- Dispatcher.Invoke(() =>
- {
- I = 100;
- pgbProcess.Value = I * 100 / 100;
- lbProcess.Content = I.ToString() + "%";
- APP.myloading.Hide();
- I = 0;
- });
- Thread.Sleep(3000);
- Dispatcher.Invoke(() =>
- {
- tip_outer.Visibility = Visibility.Collapsed;
- //tip_outer.Visibility = Visibility.Collapsed;
- MessageWindow.Show("打印成功");
- });
- }))).Start();
- }
- else
- {
- Dispatcher.Invoke(() =>
- {
- APP.myloading.Hide();
- MessageWindow.Show(standardError);
- });
- }
- //});
-
- }
- else
- {
- APP.myloading.Hide();
- MessageWindow.Show(msg);
- }
- return "";
- }
- /// <summary>
- /// 打印-结束
- /// </summary>
- /// <returns></returns>
- public void InvokeServerCompate(object obj)
- {
- try
- {
- GC.Collect();
- APP.myloading.Hide();
- }
- catch (Exception)
- {
- }
- }
-
- private int I = 0;
- private void Times_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
- {
- Dispatcher.Invoke(() =>
- {
- if (I <= 90)
- {
- try
- {
- pgbProcess.Value = I * 100 / 100;
- lbProcess.Content = I.ToString() + "%";
- I++;
- }
- catch (Exception ex)
- {
- LogHelper.WriteErrLog("【摄像】(Times_Elapsed)生成图片错误:" + ex.Message, ex);
- }
- }
- });
- }
- /// <summary>
- /// 窗口移动
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- DragMove();
- }
- /// <summary>
- /// 关闭
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void BtnClose_Click(object sender, RoutedEventArgs e)
- {
- Hide();
- }
- /// <summary>
- /// 减
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void BtnLess_Click(object sender, RoutedEventArgs e)
- {
- int num = Convert.ToInt32(txbNumberOfCopies.Text);
- if (num > 1)
- {
- num = num - 1;
- txbNumberOfCopies.Text = num.ToString();
- }
- }
- /// <summary>
- /// 新增
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void BtnAdd_Click(object sender, RoutedEventArgs e)
- {
- int num = Convert.ToInt32(txbNumberOfCopies.Text);
- if (num > 0)
- {
- num = num + 1;
- txbNumberOfCopies.Text = num.ToString();
- }
- }
-
- private void Window_MouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
- {
-
- }
- }
- }
|