多格式转pdf——doc 转 pdf

发布时间 2023-10-25 10:48:13作者: Rover20230226
    public static String docToPdfConverter (String docFilePath, String pdfFilePath, String fontPath) throws Exception {
        FileInputStream file = new FileInputStream(docFilePath);
        HWPFDocument docDocument = new HWPFDocument(file);
        // 使用WordExtractor提取文本内容
        WordExtractor extractor = new WordExtractor(docDocument);
        String text = extractor.getText();
        // 创建Document对象
        Document document = new Document();
        // 创建PdfWriter对象将文档写入文件
        PdfWriter.getInstance(document, new FileOutputStream(pdfFilePath));
        // 打开文档
        document.open();
        // 设置字体,用于支持中文显示
        fontPath = fontPath + ",0";
        BaseFont font = BaseFont.createFont(fontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        Font chineseFont = new Font(font, 12);
        Paragraph paragraph = new Paragraph(text, chineseFont);
        // 添加文本内容到文档
        document.add(paragraph);
        // 关闭文档和文件流
        extractor.close();
        document.close();
        docDocument.close();
        file.close();

        return pdfFilePath;
    }

 多格式转换相关:https://www.cnblogs.com/Rover20230226/p/17786602.html