使用开源poi 实现 Word转Pdf

第一步:添加maven依赖
<dependencies>
    <dependency>
        <groupId>fr.opensagres.xdocreport</groupId>
        <artifactId>fr.opensagres.poi.xwpf.converter.pdf-gae</artifactId>
        <version>2.0.1</version>
    </dependency>
    <dependency>
        <groupId>com.deepoove</groupId>
        <artifactId>poi-tl</artifactId>
        <version>1.2.0</version>
    </dependency>
</dependencies>

第二步:代码
package org.example;

import fr.opensagres.poi.xwpf.converter.pdf.PdfConverter;
import fr.opensagres.poi.xwpf.converter.pdf.PdfOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;

public class Test {
    public static void main(String[] args) {
        try {

            //读取word文档
            XWPFDocument document = null;
            try (InputStream in = Files.newInputStream(Paths.get("D:\\test.docx"))) {
                document = new XWPFDocument(in);
            } catch (IOException e) {
                e.printStackTrace();
            }

            //将word转成pdf
            PdfOptions options = PdfOptions.create();
            try (OutputStream outPDF = Files.newOutputStream(Paths.get("D:\\test.pdf"))) {
                PdfConverter.getInstance().convert(document, outPDF, options);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

上一篇:你真的了解PCM吗?


下一篇:遍历当前打开画面个数代码