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; }
标签:String,BaseFont,格式,fontPath,doc,new,close,pdf,document From: https://www.cnblogs.com/Rover20230226/p/17786565.html