请问使用documents4j从Word文件到PDF格式的简单转换的明确示例?

时间:2015-10-20 12:22:41

标签: java pdf docx documents4j

我有一个简单的Word文件,我想使用documents4j api将其转换为PDF。一直在寻找几个小时,但还没有找到如何编写代码。我只需要一个基本的工作代码。

2 个答案:

答案 0 :(得分:4)

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

即,

        <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>

并使用以下代码段将文档转换为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();

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

答案 1 :(得分:0)

本文中提供的示例对我不起作用。 这个做了:

POM文件:

<dependencies>
    <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>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.8.0-beta2</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>1.8.0-beta2</version>
    </dependency>
    <dependency>
        <groupId>com.documents4j</groupId>
        <artifactId>documents4j-util-standalone</artifactId>
        <version>1.0.3</version>
    </dependency>
    <dependency>
        <groupId>com.documents4j</groupId>
        <artifactId>documents4j-transformer-msoffice-word</artifactId>
        <version>1.0.3</version>
    </dependency>
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>23.0</version>
    </dependency>
</dependencies>

和java文件:

import com.documents4j.api.DocumentType;
import com.documents4j.api.IConverter;
import com.documents4j.job.LocalConverter;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

/**
 *
 * @author Neon/Jan Madle
 */
public class NewMain {

    /**
     * @param args the command line arguments
     * @throws java.io.FileNotFoundException
     */
    public static void main(String[] args) throws FileNotFoundException, IOException, InterruptedException, ExecutionException {
    ByteArrayOutputStream bo = new ByteArrayOutputStream();

    InputStream in = new BufferedInputStream(new FileInputStream(System.getProperty("user.dir") + File.separator +"out.rtf"));
    IConverter converter = LocalConverter.builder()
            .baseFolder(new File(System.getProperty("user.dir") + File.separator +"test"))
            .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();
    conversion.get();
    try (OutputStream outputStream = new FileOutputStream("out.pdf")) {
        bo.writeTo(outputStream);
    }
    in.close();
    bo.close();
}
}