多格式转pdf——docx 转 pdf
1 public static String docxToPdfConverter (String docxFilePath, String pdfFilePath, String fontPath) throws Exception { 2 FileInputStream file = new FileInputStream(docxFilePath); 3 XWPFDocument docxDocument = new XWPFDocument(file); 4 // 创建Document对象 5 Document document = new Document(); 6 // 创建PdfWriter对象将文档写入文件 7 PdfWriter.getInstance(document, new FileOutputStream(pdfFilePath)); 8 // 打开文档 9 document.open(); 10 // 设置字体,用于支持中文显示 11 fontPath = fontPath + ",0"; 12 System.out.println(fontPath); 13 BaseFont font = BaseFont.createFont(fontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); 14 Font chineseFont = new Font(font, 12); 15 // 使用XWPFWordExtractor提取文本内容 16 XWPFWordExtractor extractor = new XWPFWordExtractor(docxDocument); 17 String text = extractor.getText(); 18 Paragraph paragraph = new Paragraph(text, chineseFont); 19 // 添加文本内容到文档 20 document.add(paragraph); 21 // 关闭文档和文件流 22 extractor.close(); 23 docxDocument.close(); 24 document.close(); 25 file.close(); 26 return pdfFilePath; 27 }
多格式转换相关:https://www.cnblogs.com/Rover20230226/p/17786602.html