用 Java 将 RTF 转为 PDF
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3591239/
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
RTF to PDF in Java
提问by jjczopek
We are building an application which partially interacts with other system. We are pulling some data from the other system which is returned as RTF document. But we have to prevent users from editing this file, so we thought about converting it with iText into PDF. Code snippet:
我们正在构建一个与其他系统部分交互的应用程序。我们正在从其他系统中提取一些数据,这些数据作为 RTF 文档返回。但是我们要防止用户编辑这个文件,所以我们考虑用iText把它转成PDF。代码片段:
// moving the rtf data into input stream to be used in RTF parser
ByteArrayInputStream rtfInputStream = new ByteArrayInputStream(rtfStream.toByteArray());
// set headers
resp.setHeader("Cache-Control", "no-store");
resp.addHeader("Content-Type", "application/pdf");
resp.addHeader("Content-Disposition", "inline; filename=Karta.pdf");
resp.setStatus(HttpServletResponse.SC_OK);
// pdf output stream
ByteArrayOutputStream pdfStream = new ByteArrayOutputStream();
Document pdfDoc = new Document();
PdfWriter pdfWriter = PdfWriter.getInstance(pdfDoc, pdfStream);
pdfDoc.open();
RtfParser rtfParser = new RtfParser(null);
rtfParser.convertRtfDocument(rtfInputStream, pdfDoc);
pdfDoc.close();
pdfWriter.close();
resp.getOutputStream().write(pdfStream.toByteArray());
rtfInputStream.close();
pdfStream.close();
is.close();
Pdf is created but font sizes are wrong, styling is wrong and encoding is wrong. Maybe You had similar problems and You worked something out? Maybe there are better solutions?
pdf 已创建,但字体大小错误,样式错误且编码错误。也许你有类似的问题并且你解决了一些问题?也许有更好的解决方案?
回答by Tao
Itext is abandoning RTF according to this post. One good solution I have used is JODCoverter Library. It leverages OpenOffice and I was able to convert several thousand RTF documents to PDF in the past.
根据这篇文章,Itext 正在放弃 RTF 。我使用过的一个很好的解决方案是JODCoverter Library。它利用 OpenOffice,过去我能够将数千个 RTF 文档转换为 PDF。
回答by Thorbj?rn Ravn Andersen
Consider using a real word processor to generate the PDF. One posibility could be OpenOffice which has an API for this kind of problems - http://api.openoffice.org/- which I would look into in your situation.
考虑使用真正的文字处理器来生成 PDF。一种可能性可能是 OpenOffice,它有一个 API 来解决这类问题 - http://api.openoffice.org/- 我会在你的情况下进行调查。
PDF's can be protected later using other open source software.
以后可以使用其他开源软件保护 PDF。

