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