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; } }
public void PrintMsg() { 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, 17, iTextSharp.text.Font.BOLD, new BaseColor(System.Drawing.Color.Black)); ApplyTableSetting print = AppHelper.ApplyPointSetting; PdfContentByte pb = writer.DirectContent; pb.BeginText(); pb.SetFontAndSize(ArialFont, 15); 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, _carInfo.Owner, print.X_Owner, print.Y_Owner, 0); pb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "100021", print.X_Post, print.Y_Post, 0); pb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, _carInfo?.Addr ?? "", print.X_Addr, print.X_Addr, 0); pb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, _carInfo?.MobilePhone ?? "", print.X_MobilePhone, print.Y_MobilePhone, 0); pb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, _carInfo.PlateNo, print.X_PlateNo, print.Y_PlateNo, 0); pb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, _carInfo.PlateType, print.X_PlateType, print.Y_PlateType, 0); pb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, _ticketNo, print.X_Ticket, print.Y_Ticket, 0); if (_carInfo.PlateNo.Contains("京")) { pb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "√", print.X_Apply, print.Y_Apply, 0); } else { pb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "√", print.X_Apply1, print.Y_Apply1, 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) { LogHelper.Error("打印异常:" + ex.Message); } finally { memoryStream.Dispose(); memoryStream.Close(); writer.Dispose(); doc.Dispose(); doc.Close(); } }