Java 中的 RTF 转 PDF

发布于 2024-09-17 05:05:56 字数 1193 浏览 3 评论 0原文

我们正在构建一个与其他系统部分交互的应用程序。我们正在从其他系统中提取一些数据,这些数据以 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 已创建,但字体大小错误、样式错误且编码错误。也许您遇到了类似的问题并且您解决了一些问题?也许有更好的解决方案?

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:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黑凤梨 2024-09-24 05:05:56

根据此帖子,Itext 正在放弃 RTF 。
我使用过的一个很好的解决方案是JODCoverter Library。它利用 OpenOffice,我过去能够将数千个 RTF 文档转换为 PDF。

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.

赴月观长安 2024-09-24 05:05:56

考虑使用真正的文字处理器来生成 PDF。一种可能是 OpenOffice,它具有解决此类问题的 API - http://api.openoffice.org/ - 我会根据你的情况进行调查。

稍后可以使用其他开源软件来保护 PDF。

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's can be protected later using other open source software.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文