将 RTF 文件转换为 PDF 的 Java 库。
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14432768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java library to convert RTF files to PDF.
提问by partha
I have a java desktop service. It currently uses velocity templates to create formatted RTF files. There are quite a few of those templates and changing them (or moving from velocity) is not an option.
我有一个 Java 桌面服务。它目前使用速度模板来创建格式化的 RTF 文件。有很多这样的模板,改变它们(或从速度移动)不是一种选择。
I am looking for a quick and easy way to change RTF files to PDF, using java code. I am already checked the following avenues. 1. iText 2. OpenOffice 3. JodConverter
我正在寻找一种使用 java 代码将 RTF 文件更改为 PDF 的快速简便的方法。我已经检查了以下途径。1. iText 2. OpenOffice 3. JodConverter
However, all of them are PDF creators. I need something that will convert RTF to PDF. Please help. Thanks.
但是,他们都是 PDF 创建者。我需要一些将 RTF 转换为 PDF 的东西。请帮忙。谢谢。
回答by Neon
I have been googling the same question for a long time here is my solution: Using documents4j and microsoft office 2010 (newer version not tried) (not needed to run in background). This solution CONVERTS .rtf to .pdf
我一直在谷歌上搜索同样的问题很长一段时间,这是我的解决方案:使用文档4j和microsoft office 2010(未尝试过较新版本)(不需要在后台运行)。此解决方案将 .rtf 转换为 .pdf
POM file:
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>
Include ALL of them! In .rtf file you need some of them for fonts and stuff like that. Example of use in java:
包括所有这些!在 .rtf 文件中,您需要其中一些用于字体和类似的东西。在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();
}
}
Hope someone finds this usefull!
希望有人觉得这很有用!
回答by Pradeep Simha
JODConverterhelps you to convert RTF files to PDF using Java. Latest version can be found here.
JODConverter可帮助您使用 Java 将 RTF 文件转换为 PDF。最新版本可以在这里找到。
As per their website,
根据他们的网站,
JODConverter, the Java OpenDocument Converter, converts documents between different office formats. It leverages OpenOffice.org, which provides arguably the best import/export filters for OpenDocument and Microsoft Office formats available today.
JODConverter,Java OpenDocument 转换器,可在不同办公格式之间转换文档。它利用 OpenOffice.org,它为当今可用的 OpenDocument 和 Microsoft Office 格式提供了可以说是最好的导入/导出过滤器。