使用spire.doc,程序员可以通过调用打印对话框(printdialog)来进行打印设置,或者通过静默打印方式直接打印word文档。
通过打印对话框打印
c#
//初始化document实例
document doc = new document();
//加载一个word文档
doc.loadfromfile("sample.docx");
//初始化printdialog实例
printdialog dialog = new printdialog();
//设置打印对话框属性
dialog.allowprinttofile = true;
dialog.allowcurrentpage = true;
dialog.allowsomepages = true;
//设置文档打印对话框
doc.printdialog = dialog;
//显示打印对话框并点击确定执行打印
printdocument printdoc = doc.printdocument;
if (dialog.showdialog() == dialogresult.ok)
{
printdoc.print();
}
vb.net
'初始化document实例
dim doc as document = new document
'加载一个word文档
doc.loadfromfile("sample.docx")
'初始化printdialog实例
dim dialog as printdialog = new printdialog
'设置打印对话框属性
dialog.allowprinttofile = true
dialog.allowcurrentpage = true
dialog.allowsomepages = true
'设置文档打印对话框
doc.printdialog = dialog
'显示打印对话框并点击确定执行打印
dim printdoc as printdocument = doc.printdocument
if (dialog.showdialog = dialogresult.ok) then
printdoc.print
end if
静默打印
该打印方式不会显示打印对话框及打印进程,需要电脑提前设置好默认打印机。
c#
//初始化document实例
document doc = new document();
//加载一个word文档
doc.loadfromfile("sample.docx");
//获取printdocument对象
printdocument printdoc = doc.printdocument;
//设置printcontroller属性为standardprintcontroller,用于隐藏打印进程
printdoc.printcontroller = new standardprintcontroller();
//打印文档
printdoc.print();
vb.net
'初始化document实例
dim doc as document = new document
'加载一个word文档
doc.loadfromfile("sample.docx")
'获取printdocument对象
dim printdoc as printdocument = doc.printdocument
'设置printcontroller属性为standardprintcontroller,用于隐藏打印进程
printdoc.printcontroller = new standardprintcontroller
打印文档
printdoc.print