Java 使用docx4j将Word文档转换为PDF
我正在尝试将docx文件转换为pdf,docx是一个带有标题、文本和图片的模板,已通过docx4j编辑。这是一段用于转换的代码Java 使用docx4j将Word文档转换为PDF,java,pdf,ms-word,docx,docx4j,Java,Pdf,Ms Word,Docx,Docx4j,我正在尝试将docx文件转换为pdf,docx是一个带有标题、文本和图片的模板,已通过docx4j编辑。这是一段用于转换的代码 private static void wordClientToPDF() throws FileNotFoundException, IOException { try { // 1) Load DOCX into WordprocessingMLPackage InputStream is = new FileInputSt
private static void wordClientToPDF() throws FileNotFoundException, IOException
{
try {
// 1) Load DOCX into WordprocessingMLPackage
InputStream is = new FileInputStream(new File(WORD_FILE_PATH_CLIENT));
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(is);
// 2) Prepare Pdf settings
PdfSettings pdfSettings = new PdfSettings();
// 3) Convert WordprocessingMLPackage to Pdf
OutputStream out = new FileOutputStream(new File(PDF_FILE_PATH_CLIENT));
PdfConversion converter = new org.docx4j.convert.out.pdf.viaXSLFO.Conversion(wordMLPackage);
converter.output(out, pdfSettings);
} catch (Throwable e) {
//YURY: print stack trace in message
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
sw.toString(); // stack trace as a string
javax.swing.JOptionPane.showMessageDialog(panel, "PDF conversion error: " + sw.toString());
}
}
但是,我收到了以下错误
非常感谢您的帮助。您的docx4j似乎在Microsoft FontsRegistry中查找正确的表单时遇到问题。。。也许你可以尝试使用一种非常常见的字体,比如Arial或TimesNewRoman?所以基本上在我的docx文件中更改字体,我理解正确吗?将字体更改为Times New Roman,但仍然会遇到相同的错误。嗯,看起来你是在Mac上运行的。需要在第438行的PhysicalFonts中添加Null处理。但是更好的MicrosoftFonts.xml应该被修改,以便每个条目都包含@mac。看,现在我自己不能修补了,是吗?