如何通过Java打印Word文档
这篇文章主要介绍了如何通过Java打印Word文档,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
Java 打印Word文档
本文介绍如何在Java程序中通过物理打印机和虚拟打印机来打印Word文档的方法。文中使用了类库Spire.Doc for Java,可通过
官网下载jar文件并导入程序或者直接通过maven仓库安装导入。
【示例1】通过物理打印机打印
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();
}
}
【示例2】通过虚拟打印机打印
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();
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
IntelliJ IDEA编译项目报错 "xxx包不存在" 或 "找不到符号"
这篇文章主要介绍了IntelliJ IDEA编译项目报错 "xxx包不存在" 或 "找不到符号" ,文中通过图文介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2019-08-08
IntelliJ IDEA2023中运行Spring Boot找不到VM options进
这篇文章主要介绍了IntelliJ IDEA2023中运行Spring Boot找不到VM options进行端口的修改的问题解决,本文通过图文并茂的形式给大家介绍的非常详细,需要的朋友可以参考下2023-11-11
Spring Boot 中的 @PutMapping 注解原理及使用小结
在本文中,我们介绍了 Spring Boot 中的 @PutMapping 注解,它可以将 HTTP PUT 请求映射到指定的处理方法上,我们还介绍了 @PutMapping 注解的原理以及如何在 Spring Boot 中使用它,感兴趣的朋友跟随小编一起看看吧2023-12-12
MybatisPlus LambdaQueryWrapper使用int默认值的坑及解决
这篇文章主要介绍了MybatisPlus LambdaQueryWrapper使用int默认值的坑及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教。2022-01-01


最新评论