Spire.Pdf 组件支持多种 PDF 文档打印方式。本文适用于 Spire.PDF Hotfix Version 3.9.360 之前的版本或免费版本,如果你使用的是该版本之后的新版本,请参考这篇文章。
本文将介绍以下几种常见方式:
- 使用默认打印机打印PDF文档
- 使用虚拟打印机(Microsoft XPS Document Writer)打印PDF文档
- 指定打印机及PDF文档打印页码范围
- 静默打印PDF文档
- 双面打印PDF文档
使用默认打印机
C#
//加载PDF文档
PdfDocument doc = new PdfDocument();
doc.LoadFromFile("Test.pdf");
//使用默认打印机打印文档所有页面
doc.PrintDocument.Print();
VB.NET
'加载PDF文档
Dim doc As New PdfDocument()
doc.LoadFromFile("Test.pdf")
'使用默认打印机打印文档所有页面
doc.PrintDocument.Print()
使用虚拟打印机
虚拟打印机(Microsoft XPS Document Writer)
C#
//加载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();
VB.NET
'加载PDF文档
Dim doc As 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()
指定打印机及文档打印页码范围
C#
//加载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();
}
VB.NET
'加载PDF文档
Dim doc As New PdfDocument()
doc.LoadFromFile("Test.pdf")
'设置打印对话框属性
Dim dialogPrint As 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 Then
'指定打印机及打印页码范围
doc.PrintFromPage = dialogPrint.PrinterSettings.FromPage
doc.PrintToPage = dialogPrint.PrinterSettings.ToPage
doc.PrinterName = dialogPrint.PrinterSettings.PrinterName
'打印文档
Dim printDoc As PrintDocument = doc.PrintDocument
dialogPrint.Document = printDoc
printDoc.Print()
End If
静默打印
C#
//加载PDF文档
var doc = new PdfDocument();
doc.LoadFromFile("Test.pdf");
//静默打印PDF文档
PrintDocument printDoc = doc.PrintDocument;
printDoc.PrintController = new StandardPrintController();
printDoc.Print();
VB.NET
'加载PDF文档
Dim doc = New PdfDocument()
doc.LoadFromFile("Test.pdf")
'静默打印PDF文档
Dim printDoc As PrintDocument = doc.PrintDocument
printDoc.PrintController = New StandardPrintController()
printDoc.Print()
双面打印
C#
//加载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();
}
VB.NET
'加载PDF文档
Dim doc As New PdfDocument()
doc.LoadFromFile("Test.pdf")
'判断打印机是否支持双面打印
Dim isDuplex As Boolean = doc.PrintDocument.PrinterSettings.CanDuplex
If isDuplex Then
'如果支持则设置双面打印模式,可选:Default/Simplex/Horizontal/Vertical
doc.PrintDocument.PrinterSettings.Duplex = Duplex.[Default]
'打印文档
doc.PrintDocument.Print()
End If