You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Apache POI 5+xdocreport将docx转PDF出现文字重叠问题

使用XDocReport将DOCX转换为PDF时出现文字重叠问题

使用XDocReport将DOCX文件转换为PDF时,输出的PDF文件存在文字重叠现象。

复现代码

@Test
void simpletestconversion() {
    try(InputStream in = new FileInputStream(docPath);
        OutputStream out = new FileOutputStream(pdfPath)) {

        XWPFDocument document = new XWPFDocument(in);
        PdfOptions pdfOptions = PdfOptions.create();
        // 使用自定义中文字体提供者
        pdfOptions.fontProvider(CHINESE_FONT_PROVIDER);

        PdfConverter.getInstance().convert(document, out, pdfOptions);
    } catch(Exception e) {
        e.printStackTrace();
    }
}

中文字体提供者定义

(使用NotoSansCJK-Regular字体文件)

private static final IFontProvider CHINESE_FONT_PROVIDER = (familyName, encoding, size, style, color) -> {
    try {
        BaseFont bf = BaseFont.createFont("NotoSansCJK-Regular.ttc" + ",0", BaseFont.IDENTITY_H,
                                          BaseFont.NOT_EMBEDDED);
        Font font = new Font(bf, size, style, color);
        if(familyName != null) {
            font.setFamily(familyName);
        }
        return font;
    } catch(DocumentException | IOException e) {
        log.error("Font error", e);
        return ITextFontRegistry.getRegistry().getFont(familyName, encoding, size, style, color);
    }
};

依赖配置(pom.xml)

...
 <apache-poi.version>5.2.3</apache-poi.version>
...
       <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>5.2.3</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-full</artifactId>
            <version>5.2.3</version>
        </dependency>

        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>fr.opensagres.poi.xwpf.converter.pdf</artifactId>
            <version>2.0.4</version>
        </dependency>

目前未找到相关解决方案,恳请各位提供帮助。


内容的提问来源于stack exchange,提问作者rul0

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 15:55:20