本文介绍如何使用spire.doc for java打印word文档到特定的物理打印机或虚拟打印机。
打印到物理打印机
import com.spire.doc.document;
import com.spire.ms.system.drawing.printing.printersettings;
public class printword {
public static void main(string[] args) {
//加载word文档
document document = new document();
document.loadfromfile("c:\\users\\administrator\\desktop\\docoumenttoprint.docx");
//创建printersettings对象
printersettings printersettings = new printersettings();
//指定物理打印机名称
printersettings.setprintername("\\\\192.168.1.104\\hp laserjet p1007");
//设置打印份数
printersettings.setcopies((short) 1);
//设置打印范围
printersettings.setfrompage(2);
printersettings.settopage(4);
//应用打印设置
document.getprintdocument().setprintersettings(printersettings);
//执行打印
document.getprintdocument().print();
}
}
打印到虚拟打印机
import com.spire.doc.document;
import com.spire.ms.system.drawing.printing.printersettings;
public class printword {
public static void main(string[] args) {
//加载word文档
document document = new document();
document.loadfromfile("c:\\users\\administrator\\desktop\\documenttoprint.docx");
//创建printersettings对象
printersettings printersettings = new printersettings();
//指定虚拟打印机
printersettings.setprintername("microsoft print to pdf");
//打印到文档
printersettings.setprinttofile(true);
//指定打印文档的保存路径和名称
printersettings.setprintfilename("output/printtopdf.pdf");
//应用打印设置
document.getprintdocument().setprintersettings(printersettings);
//执行打印
document.getprintdocument().print();
}
}