使用PDF处理控件Aspose.PDF以编程方式打印PDF文档完整攻略

许多公司需要在分发在线订单进行交付时打印PDF。他们大规模处理项目,因此手动打印每个文档可能是一项艰巨的任务。在本文中将探讨与使用C#在.NET应用程序中打印PDF文件有关的用例。

许多公司在很大程度上减少了纸张的使用。但是,在某些情况下打印很重要。例如,系统可能包含PDF格式的在线订单的详细信息。他们需要在分发在线订单进行交付时打印PDF。他们大规模处理项目,因此手动打印每个文档可能是一项艰巨的任务。

Aspose.PDF除了许多其他文件操纵和转换功能外,还支持PDF文件的高效打印。让我们探讨以下与使用C#在.NET应用程序中打印PDF文件有关的用例:

  • 使用C#打印PDF文件
  • 使用C#打印多个PDF文件
  • 使用C#打印PDF的特定页面
  • 使用C#打印安全的PDF文件
  • 使用C#将PDF页面打印到不同的纸盒
  • 使用C#将不同的页面范围打印到不同的纸张来源
  • 使用C#打印PDF时检查打印作业状态

目前,.NET版Aspose.PDF升级到v20.7版,添加PDF到CSV转换支持,更改XImage.Name结果损坏的PDF文档,修复提取页面后,缺少内容的问题,感兴趣的朋友可点击下方按钮下载最新版。

使用C#打印PDF文件

可以使用C#或VB.net在.NET应用程序中自动打印PDF文件。您可以按照以下简单步骤打印PDF文件:

  • 创建一个PdfViewer类的对象
  • 加载输入的PDF文档
  • 打印PDF文件

下面的代码段显示了如何使用C#打印PDF文件:

//Create PdfViewer objectPdfViewer viewer = new PdfViewer();//Open input PDF fileviewer.BindPdf(dataDir + "Test.pdf");//Print PDF documentviewer.PrintDocument();//Close PDF fileviewer.Close();

使用C#打印多个PDF文件

如果由于业务性质需要打印多个文件,则Aspose.PDF for .NET API已支持该功能。使用上面的代码片段一个接一个地打印多个PDF文件可能会有点慢。因此,让我们将PDF打印再进一步迈出一步,以简化流程。在这里,我们将使用列表,同时将每个PDF文件的名称添加到该列表中。以下步骤说明了我们将如何打印多个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();}

使用C#打印PDF的特定页面

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(); } } }

使用C#打印安全的PDF文件

PDF文件可以用密码保护和保护。但是,密码可以有两种类型,即用户密码和所有者密码。使用用户密码保护的PDF文件需要密码才能打开和查看加密的PDF文件。另一方面,需要所有者密码来修改受保护和受密码保护的PDF文件的内容。以下步骤说明了安全PDF的打印:

  • 使用密码加载受保护的PDF
  • 创建PdfViewer对象
  • 打印安全的PDF文件

以下代码段显示了如何使用C#打印受保护的PDF文件:

//Load secure PDF document while specifying User or Owner passwordDocument document = new Document(dataDir + "Password.pdf" , "userORowner");//Create PdfViewer objectPdfViewer viewer = new PdfViewer();//Open input PDF fileviewer.BindPdf(document);//Print PDF documentviewer.PrintDocument();//Close PDF fileviewer.Close();

使用C#将PDF打印到打印机的特定纸盘

使用Aspose.PDF for .NET API将PDF打印到特定的纸盒。例如,您可能希望将包含大量照片的PDF打印到另一个纸盒,将文本PDF文件打印到另一个纸盒。请按照以下步骤设置用于打印PDF文件的出纸盘或纸槽:

  • 加载输入PDF文件
  • 设置打印属性
  • 指定PageSettings和PaperSource
  • 调用 PrintDocumentWithSettings方法

这里值得注意的是,您可以更改打印机的名称。在这里,我们将使用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 printingviewer.AutoResize = true; // Print the file with adjusted sizeviewer.AutoRotate = false; // Print the file with adjusted rotationviewer.PrintPageDialog = false; // Do not produce the page number dialog when printingviewer.RenderingOptions.UseNewImagingEngine = true;// Create objects for printer and page settings and PrintDocumentSystem.Drawing.Printing.PrinterSettings ps = new System.Drawing.Printing.PrinterSettings();System.Drawing.Printing.PageSettings pgs = new System.Drawing.Printing.PageSettings();// Set printer nameps.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 settingsviewer.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; }

使用C#将页面范围打印到不同的纸张来源

在某些情况下,您可能需要将一个PDF文档的不同页面打印到不同的纸盒或纸槽中。例如,封面的纸张来源不同,其他页面的纸张来源不同。您当然可以按照以下步骤在同一打印作业中将页面范围打印到不同的纸张来源:

  • 初始化PdfViewer类的对象
  • 使用PdfQueryPageSettings事件处理程序委托
  • 设置页面和打印机设置
  • 调用PrintDocumentWithSettings方法

以下代码段显示了如何执行以下步骤,以及如何使用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];}

使用C#打印PDF时检查打印作业状态

可以将PDF文件打印到其他打印机。例如,Microsoft Print to PDF,Microsoft XPS Document Writer或任何物理打印机。但是,大型PDF文档的打印可能会花费很长时间,或者由于某种原因打印可能会失败。因此,API提供了一项功能,可让您通过以下步骤检查打印作业的状态:

  • 加载输入PDF文件
  • 指定页面设置
  • 设置打印机名称
  • 使用PrintDocumentWithSettings打印PDF文档

下面的代码片段显示了如何使用C#检查打印作业状态或PDF的打印进度:

// Instantiate PdfViewer objectPdfViewer viewer = new PdfViewer();// Bind source PDF fileviewer.BindPdf(dataDir + "Sample Document with Bookmark.pdf");viewer.AutoResize = true;// Hide printing dialogviewer.PrintPageDialog = false;// Create Printer Settings objectSystem.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 fileps.PrintToFile = true;ps.FromPage = 1;ps.ToPage = 2;ps.PrintRange = System.Drawing.Printing.PrintRange.SomePages;// Specify the page size of printoutpgs.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 aboveviewer.PrintDocumentWithSettings(pgs, ps);// Check the print statusif (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文件的打印状态的代码:

使用PDF处理控件Aspose.PDF以编程方式打印PDF文档完整攻略

还想要更多吗可以点击阅读

【2020 · Aspose最新资源整合】查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询

标签:

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2020年6月7日
下一篇 2020年6月7日

相关推荐

发表回复

登录后才能评论