java rtf,使用Java将RTF转换为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?

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Apache POI和iText库来实现Java中的RTF转换成Word。 首先需要使用iText库将RTF文件转换成DOCX文件,然后使用Apache POI库读取DOCX文件并保存为Word格式。 以下是实现步骤: 1. 添加iText和POI依赖库: ```xml <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.13</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> ``` 2. 使用iText将RTF转换成DOCX: ```java import com.itextpdf.text.Document; import com.itextpdf.text.rtf.RtfParser; import com.itextpdf.text.rtf.parser.RtfDestination; import com.itextpdf.text.rtf.parser.RtfListener; import com.itextpdf.text.rtf.parser.RtfParserUtils; import com.itextpdf.text.rtf.parser.RtfSource; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; public class RtfToDocxConverter { public static void convert(String rtfFilePath, String docxFilePath) throws Exception { FileInputStream input = new FileInputStream(new File(rtfFilePath)); FileOutputStream output = new FileOutputStream(new File(docxFilePath)); Document document = new Document(); RtfParser parser = new RtfParser(document); parser.convertRtfDocument(input, output); document.close(); input.close(); output.close(); } } ``` 3. 使用POI读取DOCX并保存为Word格式: ```java import org.apache.poi.xwpf.usermodel.XWPFDocument; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; public class DocxToWordConverter { public static void convert(String docxFilePath, String wordFilePath) throws Exception { FileInputStream input = new FileInputStream(new File(docxFilePath)); XWPFDocument document = new XWPFDocument(input); FileOutputStream output = new FileOutputStream(new File(wordFilePath)); document.write(output); document.close(); output.close(); } } ``` 最后,调用以上两个方法即可完成RTF转换成Word的操作。 ```java String rtfFilePath = "test.rtf"; String docxFilePath = "test.docx"; String wordFilePath = "test.doc"; RtfToDocxConverter.convert(rtfFilePath, docxFilePath); DocxToWordConverter.convert(docxFilePath, wordFilePath); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值