翻译|使用教程|编辑:李显亮|2020-07-10 10:18:04.877|阅读 707 次
概述:许多公司需要在分发在线订单进行交付时打印PDF。他们大规模处理项目,因此手动打印每个文档可能是一项艰巨的任务。在本文中将探讨与使用C#在.NET应用程序中打印PDF文件有关的用例。
# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>
许多公司在很大程度上减少了纸张的使用。但是,在某些情况下打印很重要。例如,系统可能包含PDF格式的在线订单的详细信息。他们需要在分发在线订单进行交付时打印PDF。他们大规模处理项目,因此手动打印每个文档可能是一项艰巨的任务。
Aspose.PDF除了许多其他文件操纵和转换功能外,还支持PDF文件的高效打印。让我们探讨以下与使用C#在.NET应用程序中打印PDF文件有关的用例:
目前,.NET版Aspose.PDF升级到v20.7版,添加PDF到CSV转换支持,更改XImage.Name结果损坏的PDF文档,修复提取页面后,缺少内容的问题,感兴趣的朋友可点击下方按钮下载最新版。
可以使用C#或VB.net在.NET应用程序中自动打印PDF文件。您可以按照以下简单步骤打印PDF文件:
下面的代码段显示了如何使用C#打印PDF文件:
//Create PdfViewer object PdfViewer viewer = new PdfViewer(); //Open input PDF file viewer.BindPdf(dataDir + "Test.pdf"); //Print PDF document viewer.PrintDocument(); //Close PDF file viewer.Close();
如果由于业务性质需要打印多个文件,则Aspose.PDF for .NET API已支持该功能。使用上面的代码片段一个接一个地打印多个PDF文件可能会有点慢。因此,让我们将PDF打印再进一步迈出一步,以简化流程。在这里,我们将使用列表,同时将每个PDF文件的名称添加到该列表中。以下步骤说明了我们将如何打印多个PDF文件:
该代码段显示了如何使用C#打印多个PDF文件:
var files = new List(); files.Add(dataDir + "First.pdf"); files.Add(dataDir + "Second.pdf"); foreach (String file in files) { //Create PdfViewer object PdfViewer viewer = new PdfViewer(); //Open input PDF file viewer.BindPdf(file); //Print PDF document viewer.PrintDocument(); //Close PDF file viewer.Close(); }
API中提供了打印PDF文档特定页面的功能。我们将考虑一个示例,其中包括打印多个页面范围。您需要按照以下步骤中的说明指定起始和结束页码:
下面的代码段显示了如何使用C#打印文档的特定页面:
string inPdf = dataDir + "Test.pdf"; string output = dataDir; IListprintingJobs = new List(); PrintingJobSettings printingJob1 = new PrintingJobSettings(); printingJob1.FromPage = 2; printingJob1.ToPage = 3; printingJobs.Add(printingJob1); PrintingJobSettings printingJob2 = new PrintingJobSettings(); printingJob2.FromPage = 5; printingJob2.ToPage = 7; printingJobs.Add(printingJob2); { for (var printingJobIndex = 0; printingJobIndex < printingJobs.Count; printingJobIndex++) { System.Drawing.Printing.PageSettings pgs = new System.Drawing.Printing.PageSettings(); System.Drawing.Printing.PrinterSettings ps = new System.Drawing.Printing.PrinterSettings(); ps.PrinterName = "Microsoft Print to PDF"; ps.PrintRange = System.Drawing.Printing.PrintRange.SomePages; ps.FromPage = printingJobs[printingJobIndex].FromPage; ps.ToPage = printingJobs[printingJobIndex].ToPage; System.Console.WriteLine(ps.FromPage); System.Console.WriteLine(ps.ToPage); System.Console.WriteLine(printingJobIndex); using (var theViewer = new Aspose.Pdf.Facades.PdfViewer()) { // Document printing code goes here // Print document using printer and page settings theViewer.BindPdf(inPdf); theViewer.AutoResize = true; theViewer.AutoRotate = true; theViewer.PrintPageDialog = false; theViewer.PrintDocumentWithSettings(pgs, ps); theViewer.Close(); } } }
PDF文件可以用密码保护和保护。但是,密码可以有两种类型,即用户密码和所有者密码。使用用户密码保护的PDF文件需要密码才能打开和查看加密的PDF文件。另一方面,需要所有者密码来修改受保护和受密码保护的PDF文件的内容。以下步骤说明了安全PDF的打印:
以下代码段显示了如何使用C#打印受保护的PDF文件:
//Load secure PDF document while specifying User or Owner password Document document = new Document(dataDir + "Password.pdf" , "userORowner"); //Create PdfViewer object PdfViewer viewer = new PdfViewer(); //Open input PDF file viewer.BindPdf(document); //Print PDF document viewer.PrintDocument(); //Close PDF file viewer.Close();
使用Aspose.PDF for .NET API将PDF打印到特定的纸盒。例如,您可能希望将包含大量照片的PDF打印到另一个纸盒,将文本PDF文件打印到另一个纸盒。请按照以下步骤设置用于打印PDF文件的出纸盘或纸槽:
这里值得注意的是,您可以更改打印机的名称。在这里,我们将使用Microsoft Print to PDF作为示例。以下代码段遵循以下步骤,并说明如何使用C#将文档打印到特定的纸盒或打印机的纸槽中:
Document doc = new Document("Test.pdf"); PdfViewer viewer = new PdfViewer(); viewer.BindPdf(doc); viewer.PrinterJobName = System.IO.Path.GetFileName(doc.FileName); viewer.Resolution = 110; // Set attributes for printing viewer.AutoResize = true; // Print the file with adjusted size viewer.AutoRotate = false; // Print the file with adjusted rotation viewer.PrintPageDialog = false; // Do not produce the page number dialog when printing viewer.RenderingOptions.UseNewImagingEngine = true; // Create objects for printer and page settings and PrintDocument System.Drawing.Printing.PrinterSettings ps = new System.Drawing.Printing.PrinterSettings(); System.Drawing.Printing.PageSettings pgs = new System.Drawing.Printing.PageSettings(); // Set printer name ps.PrinterName = "Microsoft Print to PDF"; pgs.PaperSize = new System.Drawing.Printing.PaperSize(paperTypeName, paperWidth, paperHeight); pgs.Margins = new System.Drawing.Printing.Margins(margins.Left, margins.Right, margins.Top, margins.Bottom); pgs.PaperSource = GetPaperSource(printerName, trayName); // Print document using printer and page settings viewer.PrintDocumentWithSettings(pgs, ps); ////// Return the PaperSource object for the provided printer and tray name. ////////////public static System.Drawing.Printing.PaperSource GetPaperSource(string printerName, string trayName) { System.Drawing.Printing.PaperSource ps = null; System.Drawing.Printing.PrintDocument prtDoc = new System.Drawing.Printing.PrintDocument(); prtDoc.PrinterSettings.PrinterName = printerName; for (int i = 0; i < prtDoc.PrinterSettings.PaperSources.Count; i++) { if (prtDoc.PrinterSettings.PaperSources[i].SourceName.ToLower().Equals(trayName.ToLower())) { ps = prtDoc.PrinterSettings.PaperSources[i]; break; } } return ps; }
在某些情况下,您可能需要将一个PDF文档的不同页面打印到不同的纸盒或纸槽中。例如,封面的纸张来源不同,其他页面的纸张来源不同。您当然可以按照以下步骤在同一打印作业中将页面范围打印到不同的纸张来源:
以下代码段显示了如何执行以下步骤,以及如何使用C#将不同的页面范围打印到不同的纸张来源或纸盘:
Aspose.Pdf.Facades.PdfViewer pdfv = new Aspose.Pdf.Facades.PdfViewer(); pdfv.PdfQueryPageSettings += PdfvOnPdfQueryPageSettings; System.Drawing.Printing.PageSettings pgs = new System.Drawing.Printing.PageSettings(); System.Drawing.Printing.PrinterSettings prin = new System.Drawing.Printing.PrinterSettings(); pdfv.BindPdf(dataDir + "Print-PageRange.pdf"); prin.PrinterName = "HP LaserJet M9050 MFP PCL6"; prin.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("A4", 827, 1169); Aspose.Pdf.Facades.PdfPageEditor pageEditor = new Aspose.Pdf.Facades.PdfPageEditor(); pageEditor.BindPdf(dataDir + "input.pdf"); pgs.Margins = new System.Drawing.Printing.Margins(0, 0, 0, 0); pgs.PaperSize = prin.DefaultPageSettings.PaperSize; pdfv.PrintDocumentWithSettings(pgs, prin); pdfv.Close(); private static void PdfvOnPdfQueryPageSettings(object sender, System.Drawing.Printing.QueryPageSettingsEventArgs queryPageSettingsEventArgs, PdfPrintPageInfo currentPageInfo) { bool isOdd = currentPageInfo.PageNumber % 2 != 0; System.Drawing.Printing.PrinterSettings.PaperSourceCollection paperSources = queryPageSettingsEventArgs.PageSettings.PrinterSettings.PaperSources; if (isOdd) queryPageSettingsEventArgs.PageSettings.PaperSource = paperSources[0]; else queryPageSettingsEventArgs.PageSettings.PaperSource = paperSources[1]; }
可以将PDF文件打印到其他打印机。例如,Microsoft Print to PDF,Microsoft XPS Document Writer或任何物理打印机。但是,大型PDF文档的打印可能会花费很长时间,或者由于某种原因打印可能会失败。因此,API提供了一项功能,可让您通过以下步骤检查打印作业的状态:
下面的代码片段显示了如何使用C#检查打印作业状态或PDF的打印进度:
// Instantiate PdfViewer object PdfViewer viewer = new PdfViewer(); // Bind source PDF file viewer.BindPdf(dataDir + "Sample Document with Bookmark.pdf"); viewer.AutoResize = true; // Hide printing dialog viewer.PrintPageDialog = false; // Create Printer Settings object System.Drawing.Printing.PrinterSettings ps = new System.Drawing.Printing.PrinterSettings(); System.Drawing.Printing.PageSettings pgs = new System.Drawing.Printing.PageSettings(); System.Drawing.Printing.PrintDocument prtdoc = new System.Drawing.Printing.PrintDocument(); // Specify the printer anme //ps.PrinterName = "Microsoft XPS Document Writer"; ps.PrinterName = "Microsoft Print to PDF"; // Resultant Printout name //ps.PrintFileName = "ResultantPrintout.xps"; ps.PrintFileName = "ResultantPrintout.pdf"; // Print the output to file ps.PrintToFile = true; ps.FromPage = 1; ps.ToPage = 2; ps.PrintRange = System.Drawing.Printing.PrintRange.SomePages; // Specify the page size of printout pgs.PaperSize = new System.Drawing.Printing.PaperSize("A4", 827, 1169); ps.DefaultPageSettings.PaperSize = pgs.PaperSize; pgs.Margins = new System.Drawing.Printing.Margins(0, 0, 0, 0); // Print the document with settings specified above viewer.PrintDocumentWithSettings(pgs, ps); // Check the print status if (viewer.PrintStatus != null) { // An exception was thrown Exception ex = viewer.PrintStatus as Exception; if (ex != null) { // Get exception message } } else { // No errors were found. Printing job has completed successfully Console.WriteLine("printing completed without any issue.."); }
结果,此代码段在.NET应用程序的控制台上打印有关PDF文件的打印状态的代码:
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@pclwef.cn