Docx4J将中文转换为pdf

时间:2018-09-27 09:19:44

标签: pdf ms-word docx4j

我有以下代码将docx文件转换为pdf文件,我的docx内容具有文本框和汉字。

String myFilePath = "testing.docx";

File docxFile = new File("testing.docx");
WordprocessingMLPackage wordprocessingMLPackage = WordprocessingMLPackage.load(docxFile);

Mapper identifierFontMapper = new IdentityPlusMapper();
wordprocessingMLPackage.setFontMapper(identifierFontMapper);

Mapper bestMatchingMapper = new BestMatchingMapper();
wordprocessingMLPackage.setFontMapper(bestMatchingMapper);

Docx4J.toPDF(wordprocessingMLPackage, new FileOutputStream(myFilePath + ".pdf"));

使用这些代码,我可以转换为pdf文件,但是问题是汉字变成了#####。

有什么办法解决这个问题?

here是我的document.xml

1 个答案:

答案 0 :(得分:1)

假设您的类路径上有docx4j-export-FO,以便使用XSL FO导出,您应该能够看到哪些字符缺少字形(打开org.docx4j.fonts的DEBUG日志记录)并进行映射合适的字体。

例如参见https://github.com/plutext/docx4j-export-FO/blob/master/src/samples/docx4j/org/docx4j/samples/ConvertOutPDFviaXSLFO.java#L144

编辑9月29日

我知道:

WARN org.docx4j.fonts.fop.util.FopConfigUtil .declareFonts line 123 - Document font Calibri is not mapped to a physical font!
WARN org.docx4j.fonts.fop.util.FopConfigUtil .declareFonts line 123 - Document font SimHei is not mapped to a physical font!
WARN org.docx4j.fonts.fop.util.FopConfigUtil .declareFonts line 123 - Document font Arial is not mapped to a physical font!
WARN org.docx4j.fonts.fop.util.FopConfigUtil .declareFonts line 123 - Document font Wingdings is not mapped to a physical font!
WARN org.docx4j.fonts.fop.util.FopConfigUtil .declareFonts line 123 - Document font 華康中黑體 is not mapped to a physical font!

WARN org.apache.fop.apps.FOUserAgent .processEvent line 94 - Font "Symbol,normal,700" not found. Substituting with "Symbol,normal,400".
WARN org.apache.fop.apps.FOUserAgent .processEvent line 94 - Font "ZapfDingbats,normal,700" not found. Substituting with "ZapfDingbats,normal,400".
WARN org.apache.fop.apps.FOUserAgent .processEvent line 94 - Font "Calibri,normal,700" not found. Substituting with "any,normal,700".
WARN org.apache.fop.apps.FOUserAgent .processEvent line 94 - Glyph "这" (0x8fd9) not available in font "Times-Bold".
WARN org.apache.fop.apps.FOUserAgent .processEvent line 94 - Glyph "些" (0x4e9b) not available in font "Times-Bold".
WARN org.apache.fop.apps.FOUserAgent .processEvent line 94 - Glyph "都" (0x90fd) not available in font "Times-Bold".
WARN org.apache.fop.apps.FOUserAgent .processEvent line 94 - Glyph "只" (0x53ea) not available in font "Times-Bold".
WARN org.apache.fop.apps.FOUserAgent .processEvent line 94 - Glyph "是" (0x662f) not available in font "Times-Bold".
WARN org.apache.fop.apps.FOUserAgent .processEvent line 94 - Glyph "测" (0x6d4b) not available in font "Times-Bold".
WARN org.apache.fop.apps.FOUserAgent .processEvent line 94 - Glyph "试" (0x8bd5) not available in font "Times-Bold".
WARN org.apache.fop.apps.FOUserAgent .processEvent line 94 - Glyph "而" (0x800c) not available in font "Times-Bold".

请注意,字形X在字体Y消息中不可用。因此,我需要类似的东西:

    Mapper fontMapper = new IdentityPlusMapper();
    wordMLPackage.setFontMapper(fontMapper);

    fontMapper.put("Times-Bold", PhysicalFonts.get(some Chinese font installed in my OS));