JAVA实现Word、rtf等转PDF

JAVA实现Word、rtf等转PDF

https://repository.aspose.com/repo/
下载
aspose-words-jdk16-14.6.0.0.jar

public static boolean getLicense() {  
        boolean result = false;  
        try {  
            InputStream is = BTest.class.getClassLoader().getResourceAsStream("license.xml"); // license.xml应放在..\WebRoot\WEB-INF\classes路径下  
            License aposeLic = new License();  
            aposeLic.setLicense(is);  
            result = true;  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return result;  
    }
public static void docToPdf(String inPath, String outPath) {  
        /*if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生  
            return;  
        }  */
        try {  
            File file = new File(outPath); // 新建一个空白pdf文档  
            FileOutputStream os = new FileOutputStream(file);  
            Document doc = new Document(inPath); // inPath是将要被转化的文档地址 
            doc.save(os, SaveFormat.PDF);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,// EPUB, XPS, SWF 相互转换  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }
  • 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、付费专栏及课程。

余额充值