一:C#代码直接打印pdf文件(打印质保书pdf文件)
引用:
代码注释很详细了。
private void btn_pdf_Click(object sender, RoutedEventArgs e) { string filePath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "Resource\\川AX5572 904080D005000.pdf"; printShow(filePath); } /// <summary> /// 打印的代码 /// </summary> /// <param name="url">要打印的PDF路径</param> private int printShow(string url) { int isOK = 0; PDFFile file = PDFFile.Open(url); PrinterSettings settings = new PrinterSettings(); System.Drawing.Printing.PrintDocument pd = new System.Drawing.Printing.PrintDocument(); settings.PrinterName = "NPI84FFAF (HP LaserJet MFP M436)"; // 打印机名称 settings.PrintToFile = false; //设置纸张大小(可以不设置,取默认设置)3.90 in, 8.65 in PaperSize ps = new PaperSize("test", 4, 9); ps.RawKind = 9; //如果是自定义纸张,就要大于118,(A4值为9,详细纸张类型与值的对照请看http://msdn.microsoft.com/zh-tw/library/system.drawing.printing.papersize.rawkind(v=vs.85).aspx) O2S.Components.PDFRender4NET.Printing.PDFPrintSettings pdfPrintSettings = new O2S.Components.PDFRender4NET.Printing.PDFPrintSettings(settings); pdfPrintSettings.PaperSize = ps; pdfPrintSettings.PageScaling = O2S.Components.PDFRender4NET.Printing.PageScaling.FitToPrinterMarginsProportional; pdfPrintSettings.PrinterSettings.Copies = 1; try { file.Print(pdfPrintSettings); isOK = 1; } catch (Exception) { isOK = -1; throw; } finally { file.Dispose(); } return isOK; }
二:先生成excel在调用打印(生成excel主要在于布局)
按钮事件代码:
private void btn_zbs_Click(object sender, RoutedEventArgs e) { // 准备好数据 List<DynamicModel> dm = new List<DynamicModel>() { new DynamicModel(){BatchNumber="319040027",RelMpa = "470",RMpa = "655",A="29.0",Agt = "16.0",RmReL = "1.40",RelRel = "1.18",DirectBendingOne="",ResultOne = "",DirectBendingTwo = "I", ResultTwo = "合格", Metallography = "合格", WeightDeviation="合格", SurfaceQuality="合格", SizeAndAppearance="合格", Packing="合格", }, new DynamicModel(){BatchNumber="319040027",RelMpa = "450",RMpa = "650",A="27.0",Agt = "15.4",RmReL = "1.45",RelRel = "1.13",DirectBendingOne="",ResultOne = "",DirectBendingTwo = "I", ResultTwo = "合格", Metallography = "合格", WeightDeviation="合格", SurfaceQuality="合格", SizeAndAppearance="合格", Packing="合格", }, new DynamicModel(){BatchNumber="319040027",RelMpa = "435",RMpa = "640",A="28.0",Agt = "15.7",RmReL = "1.47",RelRel = "1.09",DirectBendingOne="",ResultOne = "",DirectBendingTwo = "I", ResultTwo = "合格", Metallography = "合格", WeightDeviation="合格", SurfaceQuality="合格", SizeAndAppearance="合格", Packing="合格", }, new DynamicModel(){B