旧版word转pdf

1、转换方案

使用 aspose-words) 做为转换工具,为开源代码,MIT的License,所以有人已经破解,使用以下版本。

<dependency>  
    <groupId>com.luhuiguo</groupId>  
    <artifactId>aspose-words</artifactId>  
    <version>23.1</version>  
</dependency>

2、实现代码

import com.aspose.words.Document;  
import com.aspose.words.SaveFormat;  
import lombok.extern.slf4j.Slf4j;  
import org.apache.poi.xwpf.usermodel.XWPFDocument;  
import org.apache.poi.xwpf.usermodel.XWPFParagraph;  
  
import java.io.File;  
import java.io.FileInputStream;  
import java.io.FileOutputStream;  
import java.io.IOException;  
import java.util.HashSet;  
import java.util.List;  
import java.util.Set;  
import java.util.regex.Matcher;  
import java.util.regex.Pattern;  
  
@Slf4j  
public class WordUtils {  
    /**  
     * @Description:  
     * @Param:  
     * @param inputPath word文件路径  
     * @param keysToCheck 需要校验的键  
     * @return:  
     **/  
    public static void checkKeys(String inputPath, Set<String> keysToCheck) {  
        XWPFDocument document;  
        try (FileInputStream fis = new FileInputStream(inputPath)) {  
            document = new XWPFDocument(fis);  
        } catch (IOException e) {  
            throw new RuntimeException("读取模板文件失败: " + e.getMessage());  
        }  
        // 查找模板中的所有占位符  
        Set<String> placeholders = findPlaceholders(document);  
        // 校验模板中是否包含所有替换变量!  
        for (String key : keysToCheck) {  
            if (!placeholders.contains(key)) {  
                throw new RuntimeException("缺少模板占位符对应的替换内容: " + key);  
            }  
        }  
    }  
  
  
    /**  
     * @Description: word转pdf  
     * @Param:  
     * @param wordFilePath word文件路径  
     * @param pdfFilePath  pdf文件路径  
     * @return:  
    **/  
    public static void wordToPdf(String wordFilePath, String pdfFilePath) {  
        FileOutputStream fileOS = null;  
        try {  
            Document doc = new Document(wordFilePath);  
            fileOS = new FileOutputStream(new File(pdfFilePath));  
            // 保存转换的pdf文件  
            doc.save(fileOS, SaveFormat.PDF);  
        } catch (Exception e) {  
            log.error("转换Word文档为PDF失败:{}",e.getMessage());  
            throw new RuntimeException("转换Word文档为PDF失败: " + e.getMessage());  
        } finally {  
            try {  
                if (fileOS != null) {  
                    fileOS.close();  
                }  
            } catch (IOException e) {  
                log.error("关闭文件输出流时发生错误:{}",e.getMessage());  
            }  
        }  
    }
  
  
    /**  
     * @Description: 查找word中的所有占位符  
     * @param document  
     * @return java.util.Set<java.lang.String>  
    **/  
    private static Set<String> findPlaceholders(XWPFDocument document) {  
        Set<String> placeholders = new HashSet<>();  
        Pattern pattern = Pattern.compile("\\{\\{\\s*(@?\\w+)\\s*\\}\\}");  
        List<XWPFParagraph> paragraphs = document.getParagraphs();  
        for (XWPFParagraph paragraph : paragraphs) {  
            String paragraphText = paragraph.getText();  
            Matcher matcher = pattern.matcher(paragraphText);  
            while (matcher.find()) {  
                placeholders.add(matcher.group(1).trim());  
            }  
        }  
        return placeholders;  
    }  
  
  
}

3、执行生成

public static void wordToPdf_test() throws IOException {  
    String str = "keyWordsByPDF";  
    String  strByte = java.util.Base64.getEncoder().encodeToString(str.getBytes());  
    System.out.println("Base64编码成功:{}"+strByte);  
    BASE64Decoder decoder = new BASE64Decoder();  
    System.out.println("Base64解码成功:{}"+new String(java.util.Base64.getDecoder().decode(strByte)));  
    byte[] bytes = decoder.decodeBuffer(strByte);  
    System.out.println("BASE64Decoder解码成功:{}"+new String(bytes));  
  
    String inputPath = "D:\\tmp\\doc\\文件.doc";  
    for (int i = 0; i < 2; i++) {  
        String outputPdfPath = "D:\\tmp\\doc\\gen\\文件"+ i + ".pdf";  
        // 将Word文档转换为PDF  
        wordToPdf(inputPath, outputPdfPath);  
        System.out.println("文件名:" + outputPdfPath + " word转换pdf成功!");  
    }  
  
    System.out.println("Word文档转换为PDF成功!");  
}

参考资料
liuzy88破解说明
吾爱破解
24.3版本

本文由博客一文多发平台 OpenWrite 发布!

stic9527
1 声望0 粉丝