Java实现将word转化为PDF

这个工具类全面支持DOC, DOCX,OOXML, RTF HTML,OpenDocument,PDF, EPUB, XPS,SWF 相互转换

转化时需要的jar包和pom:

jar包地址如下:链接:https://pan.baidu.com/s/1w1aMvTYqMKRhrRtum88HbQ 提取码:y1qg

<!-- https://mvnrepository.com/artifact/com.aspose/aspose-words -->
    <dependency>
      <groupId>com.aspose</groupId>
      <artifactId>aspose-words</artifactId>
      <version>19.2</version>
      <scope>system</scope>
      <systemPath>${project.basedir}/lib/aspose-words-19.2-jdk16.jar</systemPath>
    </dependency>

1.word转PDF的工具类代码如下:

public class FileUtils {

    /**
     * Word 转换  Pdf
     * @param inPath
     * @param outPath
     */
    public static void doc2pdf(String inPath, String outPath) {
        System.out.println("===============Word 转换  Pdf=================");
	         /*if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生(15.8版本,已更改为19.2版本,故此不需要验证了)
	                 return;
	         }*/
        try {
            long old = System.currentTimeMillis();
            File file = new File(outPath); // 新建一个pdf文档
            FileOutputStream os = new FileOutputStream(file);
            /** 这里需要指定Linux服务器上从本地windows上传的字体库,否则生成乱码
             *  【/usr/share/fonts/windowsFonts/】位于Linux服务器上的windows字体,作为word转pdf用
             */
            //FontSettings.getDefaultInstance().setFontsFolder("/usr/share/fonts/windowsFonts/", true);
            Document doc = new Document(inPath); // Address是将要被转化的word文档
            System.out.println("doc"+doc);
            doc.save(os, com.aspose.words.SaveFormat.PDF);// 全面支持DOC, DOCX,
            // OOXML, RTF HTML,
            // OpenDocument,
            // PDF, EPUB, XPS,
            // SWF 相互转换
            long now = System.currentTimeMillis();
            // 将时间对象格式化为字符串
            Date date = new Date();
            SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss");
            String strDate = format.format(date);
            System.out.println("---PDF转化现在是--------"+strDate+"------------");
            os.close();

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("--------PDF转换出异常---------");
        }
    }
}

调用的样例:

public static void main( String[] args ) {
        String addr1 = "E:\\NYQX_BQD.docx";
        String addr2 = "E:\\NYQX_BQD.pdf";
        FileUtils.doc2pdf(addr1,addr2);

    }

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
以下是使用Java实现Word转换PDF的步骤: 1. 首先,需要使用Apache POI库来读取Word文档。可以使用以下Maven依赖项将其添加到项目中: ```xml <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> ``` 2. 接下来,需要使用iText库将Word文档转换PDF。可以使用以下Maven依赖项将其添加到项目中: ```xml <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.13</version> </dependency> ``` 3. 读取Word文档并将其转换PDF的代码如下所示: ```java import java.io.FileInputStream; import java.io.FileOutputStream; import org.apache.poi.xwpf.usermodel.XWPFDocument; import com.itextpdf.text.Document; import com.itextpdf.text.pdf.PdfWriter; public class WordToPdfConverter { public static void main(String[] args) { try { // 读取Word文档 XWPFDocument document = new XWPFDocument(new FileInputStream("input.docx")); // 创建PDF文档 Document pdf = new Document(); PdfWriter.getInstance(pdf, new FileOutputStream("output.pdf")); // 打开PDF文档 pdf.open(); // 将Word文档的内容写入PDF文档 document.write(pdf); // 关闭PDF文档和Word文档 pdf.close(); document.close(); System.out.println("Word文档已成功转换PDF。"); } catch (Exception e) { e.printStackTrace(); } } } ``` 4. 运行上述代码后,将在项目目录中生成一个名为“output.pdf”的PDF文件,其中包含从Word文档中提取的内容。
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值