如何通过Java打印Word文档
这篇文章主要介绍了如何通过Java打印Word文档,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
Java打印Word文档
本文介绍如何在Java程序中通过物理打印机和虚拟打印机来打印Word文档的方法。文中使用了类库Spire.DocforJava,可通过
官网下载jar文件并导入程序或者直接通过maven仓库安装导入。
【示例1】通过物理打印机打印
importcom.spire.doc.Document;
importcom.spire.ms.System.Drawing.Printing.PrinterSettings;
publicclassPrintWord{
publicstaticvoidmain(String[]args){
//加载Word文档
Documentdocument=newDocument();
document.loadFromFile("C:\\Users\\Administrator\\Desktop\\DocoumentToPrint.docx");
//创建PrinterSettings对象
PrinterSettingsprinterSettings=newPrinterSettings();
//指定物理打印机名称
printerSettings.setPrinterName("\\\\192.168.1.104\\HPLaserJetP1007");
//设置打印份数
printerSettings.setCopies((short)1);
//设置打印范围
printerSettings.setFromPage(2);
printerSettings.setToPage(4);
//应用打印设置
document.getPrintDocument().setPrinterSettings(printerSettings);
//执行打印
document.getPrintDocument().print();
}
}
【示例2】通过虚拟打印机打印
importcom.spire.doc.Document;
importcom.spire.ms.System.Drawing.Printing.PrinterSettings;
publicclassPrintWord{
publicstaticvoidmain(String[]args){
//加载Word文档
Documentdocument=newDocument();
document.loadFromFile("C:\\Users\\Administrator\\Desktop\\DocumentToPrint.docx");
//创建PrinterSettings对象
PrinterSettingsprinterSettings=newPrinterSettings();
//指定虚拟打印机
printerSettings.setPrinterName("MicrosoftPrinttoPDF");
//打印到文档
printerSettings.setPrintToFile(true);
//指定打印文档的保存路径和名称
printerSettings.setPrintFileName("output/PrintToPDF.pdf");
//应用打印设置
document.getPrintDocument().setPrinterSettings(printerSettings);
//执行打印
document.getPrintDocument().print();
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。