使用C#打印PDF文件

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

  • 创建一个PdfViewer类的对象
  • 加载输入的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();

使用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 objectPdfViewer viewer = new PdfViewer();//Open input PDF fileviewer.BindPdf(file);//Print PDF documentviewer.PrintDocument();//Close PDF fileviewer.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 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();

使用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 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; }

使用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];elsequeryPageSettingsEventArgs.PageSettings.PaperSource = paperSources[1];

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

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

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

下面的代码片段显示了如何使用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 thrownException ex = viewer.PrintStatus as Exception;if (ex != null){// Get exception message}
}
else
{// No errors were found. Printing job has completed successfullyConsole.WriteLine("printing completed without any issue..");
}

利用Aspose.PDF以编程方式打印PDF文档相关推荐

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

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

  2. Word开发工具Aspose.Words功能演示:在C ++中以编程方式在Word文档中添加或删除页眉和页脚

    Word文档中的页眉和页脚用于格式化和显示重要信息,例如主题,章节,页码,Copywrite等.以编程方式使用Word文档时,可能需要添加或删除页眉和页脚.为此,本文将教您如何使用C ++在Word文 ...

  3. 如何使用C ++以编程方式在Word文档中使用注释?

    Microsoft Word使您能够向Word文档添加注释.在诸如建议改进文档或共享文本思想等情况下,注释可能会有所帮助.在某些情况下,需要以编程方式管理评论.为此,本文将教您如何使用C ++在Wor ...

  4. MSDN Visual系列:在WSSv3中编程方式激活单个文档库的审核功能

    原文:http://msdn2.microsoft.com/en-us/library/bb418730.aspx WSSv3有一个很强大基础结构,专门用来审核用户对页面,文档和列表项的访问.您可以在 ...

  5. Android 打印,搜索连接同一局域网下的所有网络打印机,打印照片,打印自定义文档。

    Android 搜索局域网下的所有网络打印机,打印照片,打印自定义文档. Android 连接局域网下的网络打印机打印图片,和自定义文档打印 github地址: https://github.com/ ...

  6. [实用][更新中]Java Apache POI 打印Word文档工具(含文本替换,动态表格功能)

    [实用][更新中]Java Apache POI 打印Word文档工具(含文本替换,动态表格功能) 基于Apache POI对Word进行操作 一.基于Apache POI封装的word文档工具V1. ...

  7. pdf编辑器如何缩放和平移文档

    很多人现在都比较熟悉PDF格式的文档,它是我们现在比较流行和常用的一种格式,想要快速有效的使用PDF格式文档,大家肯定都会利用一些软件的帮助,那么pdf编辑器如何缩放和平移文档?针对这个问题,小编就详 ...

  8. 利用Aspose.Word控件和Aspose.Cell控件,实现Word文档和Excel文档的模板化导出

    我们知道,一般都导出的Word文档或者Excel文档,基本上分为两类,一类是动态生成全部文档的内容方式,一种是基于固定模板化的内容输出,后者在很多场合用的比较多,这也是企业报表规范化的一个体现. 我的 ...

  9. 如何翻译英文PDF?如何翻译英文word文档?

    如何翻译英文PDF?如何翻译英文word文档? 无论是写论文的学生,还是查阅需求文档的程序猿,都会接触各种英文的文档.大部分人的英语水平,在面对满是专业词汇的文献时想必都是一败涂地.现在市面上的一些翻 ...

最新文章

  1. 人的一生,到底在追求甚么?...
  2. boost::python::converter::arg_to_python相关的测试程序
  3. 快速下载Spring官网下载dist.zip中所有jar,例如spring-5.2.10.RELEASE-dist.zip
  4. 欲瘦其包,必先探清其底细
  5. SAP 电商云 Spartacus UI 设置 Delivery Mode 的时序分析和一个竞争条件问题分享
  6. codeforces 919E Congruence Equation
  7. php 登录记住密码,php 记住密码自动登录
  8. 克隆卡设备_SD Clone for mac(SD卡克隆备份软件) v3.2
  9. cad画正弦曲线lisp_cadlisp基础教程.pdf
  10. Mac右键添加Google搜索
  11. 计算机电源可调电阻,电脑ATX电源改0V-30V可调电源,电流7A
  12. 苹果宣布前CEO史蒂夫·乔布斯逝世 世上再无乔布斯!
  13. h5活动是什么意思_H5活动页能给你带来什么?
  14. 【JY】旭日始旦 岁月如新
  15. Shell脚本发送邮件(CentOS+mailx+QQ邮箱)
  16. Chap.18 总结《CL: An Introduction》 (Vyvyan Evans)
  17. 从多个数中取出之和等于定值的组合
  18. css 设置容器高度等于宽度,设置容器的宽高一致。
  19. 路透社:特斯拉5月在华汽车订单较4月减少近一半
  20. 竞赛经验——挑战杯、互联网加、北斗杯、微软创新杯、计算机设计等比赛教训与经验

热门文章

  1. 【券商报告】信息服务行业AI产业链深度研究(2)AI+驾驶:蔚来,这不是汽车,这是一个移动的计算中心——附下载链接
  2. css线条渐变,线条两边向中间渐变, 中间向两边渐变
  3. windows 登陆 和 pGina
  4. 在XP的欢迎界面登录管理员帐号
  5. 2023年值得关注:邮件营销平台的新技术和新趋势
  6. 新手帮助,STM32 刷入Arduino的BootLoader。实现免串口下载
  7. Android 4.0 截屏(Screenshot)
  8. MediaTypeListWidget-insertItem 添加的label没有填充单元格
  9. 怎么判断噎到没噎到_吃东西噎住别慌 教你一分钟学会海姆立克急救法
  10. yate学习--yatesip.h--class YSIP_API SIPMessage : public RefObject