Java POI Word 转 PDF

pom依赖:


    org.apache.poi
    poi-ooxml
    3.17


    fr.opensagres.xdocreport
    fr.opensagres.poi.xwpf.converter.pdf-gae
    2.0.1

测试代码:

@Test
public void test() {
    FileInputStream fileInputStream = null;
    FileOutputStream fileOutputStream = null;
    
    try {
        fileInputStream = new FileInputStream("D:\\download\\xxx.docx");
        XWPFDocument xwpfDocument = new XWPFDocument(fileInputStream);
        PdfOptions pdfOptions = PdfOptions.create();
        fileOutputStream = new FileOutputStream("D:\\download\\xxx.pdf");
        PdfConverter.getInstance().convert(xwpfDocument,fileOutputStream,pdfOptions);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (null != fileInputStream) {
                fileInputStream.close();
            }
            if (null != fileOutputStream) {
                fileOutputStream.close();
            } 
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

你可能感兴趣的:(Java小工具,java)