Spire.PDF(点击下载)是一个专业的PDF组件,能够独立地创建、编写、编辑、操作和阅读PDF文件,支持 .NET、Java、WPF和Silverlight。Spire.PDF的PDF API拥有丰富的功能,如安全设置(包括数字签名)、PDF文本/附件/图片提取、PDF文件合并/拆分、元数据更新、章节和段落优化、图形/图像描绘和插入、表格创建和处理、数据导入等等。
本文将介绍以下几种常见方式:
-
使用默认打印机打印PDF文档
-
使用虚拟打印机(Microsoft XPS Document Writer)打印PDF文档
-
指定打印机及PDF文档打印页码范围
-
静默打印PDF文档
-
双面打印PDF文档
使用默认打印机
//加载PDF文档 PdfDocument doc = new PdfDocument(); doc.LoadFromFile("Test.pdf"); //使用默认打印机打印文档所有页面 doc.PrintDocument.Print();
使用虚拟打印机(Microsoft XPS Document Writer)
//加载PDF文档 PdfDocument doc = new PdfDocument(); doc.LoadFromFile("Test.pdf"); //选择Microsoft XPS Document Writer打印机 doc.PrintDocument.PrinterSettings.PrinterName = "Microsoft XPS Document Writer"; //打印PDF文档到XPS格式 doc.PrintDocument.PrinterSettings.PrintToFile = true; doc.PrintDocument.PrinterSettings.PrintFileName = "PrintToXps.xps"; doc.PrintDocument.Print();
指定打印机及文档打印页码范围
//加载PDF文档 PdfDocument doc = new PdfDocument(); doc.LoadFromFile("Test.pdf"); //设置打印对话框属性 PrintDialog dialogPrint = new PrintDialog(); dialogPrint.AllowPrintToFile = true; dialogPrint.AllowSomePages = true; dialogPrint.PrinterSettings.MinimumPage = 1; dialogPrint.PrinterSettings.MaximumPage = doc.Pages.Count; dialogPrint.PrinterSettings.FromPage = 1; dialogPrint.PrinterSettings.ToPage = doc.Pages.Count; if (dialogPrint.ShowDialog() == DialogResult.OK) { //指定打印机及打印页码范围 doc.PrintFromPage = dialogPrint.PrinterSettings.FromPage; doc.PrintToPage = dialogPrint.PrinterSettings.ToPage; doc.PrinterName = dialogPrint.PrinterSettings.PrinterName; //打印文档 PrintDocument printDoc = doc.PrintDocument; dialogPrint.Document = printDoc; printDoc.Print(); }
静默打印
//加载PDF文档 var doc = new PdfDocument(); doc.LoadFromFile("Test.pdf"); //静默打印PDF文档 PrintDocument printDoc = doc.PrintDocument; printDoc.PrintController = new StandardPrintController(); printDoc.Print();
双面打印
//加载PDF文档 PdfDocument doc = new PdfDocument(); doc.LoadFromFile("Test.pdf"); //判断打印机是否支持双面打印 bool isDuplex = doc.PrintDocument.PrinterSettings.CanDuplex; if (isDuplex) { //如果支持则设置双面打印模式,可选:Default/Simplex/Horizontal/Vertical doc.PrintDocument.PrinterSettings.Duplex = Duplex.Default; //打印文档 doc.PrintDocument.Print(); }
来源:oschina
链接:https://my.oschina.net/u/4087915/blog/3108042