我们正在构建一个与其他系统部分交互的应用程序。我们正在从其他系统中提取一些数据,这些数据作为 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 已创建,但字体大小错误、样式错误和编码错误。也许你有类似的问题并且你解决了一些问题?也许有更好的解决方案?
最佳答案
Itext 正在根据此 post 放弃 RTF . 我用过的一个很好的解决方案是 JODCoverter Library .它利用了 OpenOffice,我过去能够将数千个 RTF 文档转换为 PDF。
关于java - 在 Java 中将 RTF 转为 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3591239/