關於PDF打印的問題,前面有篇文章(點這里查看)也敘述過,今天來談談另外一種方法
其實方法很簡單,因為需要把多個PDF文檔按順序連續打印,為此我們為什么不把要打印的pdf文檔按順序合並成一個PDF打印呢?如此就簡單多了哦。
這里文章寫出來並不是為了炫耀什么,只是覺得發現些好東西就分享出來而已,同時也做個記錄,方便以后查找
開始正文
1、為了方便,打印方法就不另尋他路了,和前面一致,具體如下:

Process proc = new Process(); proc.StartInfo.CreateNoWindow = false; proc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; proc.StartInfo.UseShellExecute = true; proc.StartInfo.FileName = itemPath;//打印文件路徑(本地完整路徑包括文件名和后綴名) proc.StartInfo.Verb = "print"; proc.Start(); proc.Close();
2、就是重點了,合並PDF文檔,方法很簡單,網上一搜一大把,因為我的需求需要把jpg圖片和pdf一起打印,因此合並方法中包含圖片
使用此方法需要第三方控件iTextSharp.dll(點擊這里下載)

/// <summary> /// 把多個PDF文件和JPG/PNG圖合並成一個PDF文檔 /// </summary> /// <param name="fileList">需要合並文件的完整路徑列表</param> /// <param name="outMergeFile">輸出文件完整路徑</param> public static void MergePDFFile(List<string> fileList, string outMergeFile) { PdfReader reader; Document document = new Document(); PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(outMergeFile, FileMode.Create)); document.Open(); PdfContentByte cb = writer.DirectContent; PdfImportedPage newPage; foreach (var itemFile in fileList) { if (!File.Exists(itemFile)) { string fileName = Path.GetFileName(itemFile); LogMessageWrite.WriteMessage(string.Format("文件打印合並__{0} 文件不存在", fileName)); continue; } FileInfo fInfo = new FileInfo(itemFile); if (fInfo.Length < 1) { string fileName = Path.GetFileName(itemFile); LogMessageWrite.WriteMessage(string.Format("文件打印合並__文件內容為空,無法打印,{0}", fileName)); return; } var ext = Path.GetExtension(itemFile).ToLower(); if (".pdf".Equals(ext)) { reader = new PdfReader(itemFile); int iPageNum = reader.NumberOfPages; for (int j = 1; j <= iPageNum; j++) { document.NewPage(); newPage = writer.GetImportedPage(reader, j); cb.AddTemplate(newPage, 0, 0); } } else if (".jpg".Equals(ext) || ".jpge".Equals(ext) || ".png".Equals(ext)) { FileStream rf = new FileStream(itemFile, FileMode.Open, FileAccess.Read); int size = (int)rf.Length; byte[] imext = new byte[size]; rf.Read(imext, 0, size); rf.Close(); Image img = Image.GetInstance(imext); //調整圖片大小,使之適合A4 var imgHeight = img.Height; var imgWidth = img.Width; if (img.Height > iTextSharp.text.PageSize.A4.Height) { imgHeight = iTextSharp.text.PageSize.A4.Height; } if (img.Width > iTextSharp.text.PageSize.A4.Width) { imgWidth = iTextSharp.text.PageSize.A4.Width; } img.ScaleToFit(imgWidth, imgHeight); //調整圖片位置,使之居中 img.Alignment = iTextSharp.text.Image.ALIGN_MIDDLE; document.NewPage(); document.Add(img); } } document.Close(); }
3、打印合並后的文件

try { var mergeFilePath = string.Format("{0}mergepdf.pdf", tempDownDir); PDFPrintHelper.MergePDFFile(pdfList, mergeFilePath); Process proc = new Process(); proc.StartInfo.CreateNoWindow = false; proc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; proc.StartInfo.UseShellExecute = true; proc.StartInfo.FileName = mergeFilePath;//打印文件路徑(本地完整路徑包括文件名和后綴名) proc.StartInfo.Verb = "print"; proc.Start(); proc.Close(); } catch (Exception ex) { LogMessageWrite.WriteMessage(ex.Message); }
至此 大功告成