请提供使用 Documents4j 将 Word 文件简单转换为 PDF 格式的明确示例?

Pas*_*Rex 0 java pdf docx documents4j

我有一个简单的 Word 文件,我想使用documents4japi 将其转换为 PDF。已经搜索了几个小时,但还没有找到如何编写代码。我只需要一个基本的工作代码。

Nir*_*tel 5

在pom.xml中添加所需的依赖项

IE,

        <dependency>
            <groupId>com.documents4j</groupId>
            <artifactId>documents4j-api</artifactId>
            <version>0.2.1</version>
        </dependency>       

        <dependency>
            <groupId>com.documents4j</groupId>
            <artifactId>documents4j-util-conversion</artifactId>
            <version>0.2.1</version>
        </dependency>

        <dependency>
            <groupId>com.documents4j</groupId>
            <artifactId>documents4j-transformer</artifactId>
            <version>0.2.1</version>
        </dependency>

        <dependency>
            <groupId>com.documents4j</groupId>
            <artifactId>documents4j-util-all</artifactId>
            <version>0.2.1</version>
        </dependency>

        <dependency>
            <groupId>com.documents4j</groupId>
            <artifactId>documents4j-local</artifactId>
            <version>0.2.1</version>
        </dependency>

        <dependency>
            <groupId>com.documents4j</groupId>
            <artifactId>documents4j-local-demo</artifactId>
            <version>0.2.1</version>
        </dependency>
Run Code Online (Sandbox Code Playgroud)

并使用以下代码片段将文档转换为 PDF。在下面的示例中,我将 RTF 文件转换为 PDF。

        ByteArrayOutputStream bo = new ByteArrayOutputStream();

        InputStream in = new BufferedInputStream(new FileInputStream("C:\\PDF\\RichText1.rtf"));
        IConverter converter = LocalConverter.builder()
                                             .baseFolder(new File("C:\\PDF\\"))
                                             .workerPool(20, 25, 2, TimeUnit.SECONDS)
                                             .processTimeout(5, TimeUnit.SECONDS)
                                             .build();

        Future<Boolean> conversion = converter
                                        .convert(in).as(DocumentType.RTF)
                                        .to(bo).as(DocumentType.PDF)
                                        .prioritizeWith(1000) // optional
                                        .schedule();
Run Code Online (Sandbox Code Playgroud)

因此,您所需的转换后的文件将存储到此处的 ByteArrayOutputStream 对象 (bo) 中。