private void InitConfig() { try { if (!File.Exists(SettingPath)) { throw new Exception("初始化数据失败:路径" + SettingPath + "不存在!"); } string configStr = File.ReadAllText(SettingPath); AppSetting = JsonConvert.DeserializeObject <SettingEntity>(configStr); if (!File.Exists(PrintConfigPath)) { throw new Exception("初始化数据失败:路径" + PrintConfigPath + "不存在!"); } configStr = File.ReadAllText(PrintConfigPath); PointSetting = JsonConvert.DeserializeObject <PrintPoint>(configStr); configStr = File.ReadAllText(ApplyConfigPath); ApplyPointSetting = JsonConvert.DeserializeObject <ApplyTableSetting>(configStr); } catch { throw; } }
private void CreatePDF(X18J52 x18J52, string hpzl, byte[] qrcode) { PdfHelper pdfHelper = new PdfHelper(); Document doc = pdfHelper.CreateDocumentA4(); MemoryStream memoryStream = new MemoryStream(); PdfWriter writer = PdfWriter.GetInstance(doc, memoryStream); try { doc.Open(); doc.NewPage(); BaseFont ArialFont = BaseFont.CreateFont("STZHONGS.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); iTextSharp.text.Font BoldFont = new iTextSharp.text.Font(ArialFont, 11, iTextSharp.text.Font.NORMAL, new BaseColor(System.Drawing.Color.Black)); //二维码 iTextSharp.text.Image img = pdfHelper.CreateImage(qrcode, 100, 100f); img.SetAbsolutePosition(AppHelper.PointSetting.X_QRCode, AppHelper.PointSetting.Y_QRCode); doc.Add(img); PrintPoint print = AppHelper.PointSetting; PdfContentByte pb = writer.DirectContent; pb.BeginText(); pb.SetFontAndSize(ArialFont, 9); pb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, DateTime.Now.ToString("yyyy"), print.X_Year, print.Y_Year, 0); pb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, DateTime.Now.ToString("MM"), print.X_Month, print.Y_Month, 0); pb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, DateTime.Now.ToString("dd"), print.X_Day, print.Y_Day, 0); pb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, x18J52.hphm, print.X_PlateNo, print.Y_PlateNo, 0); pb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, x18J52.syr, print.X_Owner, print.Y_Owner, 0); pb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, hpzl, print.X_PlateType, print.Y_PlateType, 0); pb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "", print.X_PlateColor, print.Y_PlateColor, 0); pb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, x18J52.cllx, print.X_CarType, print.Y_CarType, 0); pb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, x18J52.clpp1, print.X_CarBrand, print.Y_CarBrand, 0); pb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, x18J52.clxh, print.X_CarMold, print.Y_CarMold, 0); pb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, x18J52.ccdjrq, print.X_RegisterDate, print.Y_RegisterDate, 0); pb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, x18J52.ccrq, print.X_MakeDate, print.Y_MakeDate, 0); pb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, x18J52.zzl, print.X_Weight, print.Y_Weight, 0); pb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, x18J52.zbzl, print.X_KerbWeight, print.Y_KerbWeight, 0); pb.EndText(); doc.Close(); MemoryStream pdf = new MemoryStream(memoryStream.ToArray()); PDFFile pdfFile = PDFFile.Open(pdf); PrinterSettings settings = new PrinterSettings(); PrintDocument pd = new PrintDocument(); settings.PrinterName = AppHelper.AppSetting.PrinterName; settings.PrintToFile = false; PDFPrintSettings pdfPrintSettings = new PDFPrintSettings(settings); pdfPrintSettings.PageScaling = PageScaling.MultiplePagesPerSheetProportional; pdfPrintSettings.PrinterSettings.Copies = 1; pdfFile.Print(pdfPrintSettings); pdf.Close(); pdf.Dispose(); pdfFile.Dispose(); } catch (Exception ex) { MessageBox.Show("打印异常" + ex.Message); } finally { memoryStream.Dispose(); memoryStream.Close(); writer.Dispose(); doc.Dispose(); doc.Close(); } }