Java将world转换成pdf文件简单实现

后台采用 : poi + documents4j


<!--poi依赖3.17-->
 <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi</artifactId>
                <version>${poi.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml</artifactId>
                <version>${poi.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml-schemas</artifactId>
                <version>${poi.version}</version>
            </dependency>


<!-- documents4j依赖 -->
 <dependency>
            <groupId>com.documents4j</groupId>
            <artifactId>documents4j-local</artifactId>
            <version>1.0.3</version>
        </dependency>
        <dependency>
            <groupId>com.documents4j</groupId>
            <artifactId>documents4j-transformer-msoffice-word</artifactId>
            <version>1.0.3</version>
        </dependency>

world转pdf代码

 public static void main(String[] args) {

        File inputWord = new File("E:\\SD\\FullStackProject\\CaseAI\\CaseAI\\service\\case\\A.docx");
        File outputFile = new File("E:\\SD\\FullStackProject\\CaseAI\\CaseAI\\service\\case\\pdf.pdf");
        try {
            InputStream docxInputStream = new FileInputStream(inputWord);
            OutputStream outputStream = new FileOutputStream(outputFile);
            IConverter converter = LocalConverter.builder().build();
            converter.convert(docxInputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute();
            outputStream.close();
            docxInputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

前端采用 pdf.js实现在线预览展示
前端实现参考博客

前端实现参考博客

你可能感兴趣的:(java,java,开发语言,后端)