首页 > 解决方案 > docx4j:docx 到 pdf 的转换 - docx 内容没有逐页显示为 pdf

问题描述

问题:使用 DOCX4J 将 docx 转换为 pdf。问题是 docx 的内容没有逐页转换为 pdf 文档。第 2 页的几行出现在 pdf 的第 1 页中。

pom.xml:

<dependency>
    <groupId>org.docx4j</groupId>
    <artifactId>docx4j</artifactId>
    <version>6.1.2</version>
</dependency>
<dependency>
    <groupId>org.docx4j</groupId>
    <artifactId>docx4j-export-fo</artifactId>
    <version>6.1.0</version>
</dependency>
<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.1</version>
</dependency>
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.4.3</version>
</dependency>

代码:

private static void convertToPDFDocx4j() throws Exception {

    InputStream is = new FileInputStream(new File(inputfilepath));
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
            .load(is);
    List sections = wordMLPackage.getDocumentModel().getSections();
    for (int i = 0; i < sections.size(); i++) {
        wordMLPackage.getDocumentModel().getSections().get(i)
                .getPageDimensions();
    }
    Mapper fontMapper = new IdentityPlusMapper();
    PhysicalFont font = PhysicalFonts.getPhysicalFonts().get(
            "Comic Sans MS");//set your desired font
    fontMapper.getFontMappings().put("Algerian", font);
    wordMLPackage.setFontMapper(fontMapper);
    PdfSettings pdfSettings = new PdfSettings();
    org.docx4j.convert.out.pdf.PdfConversion conversion = new org.docx4j.convert.out.pdf.viaXSLFO.Conversion(
            wordMLPackage);

    OutputStream out = new FileOutputStream(new File(outputfilepath));
    conversion.output(out, pdfSettings);
    System.out.println("DONE!!");

}

想知道 docx4j 是否有控制它的设置?

文档: 文档

PDF: pdf

试过了,但没有多大帮助 使用 Java 将 docx 文件转换为 PDF

标签: javapdfdocxdocx4j

解决方案


使用以下依赖项将您的 Docx4j 版本从 6.X 升级到 8.X 以解决此问题。

<dependency>
    <groupId>org.docx4j</groupId>
    <artifactId>docx4j-JAXB-Internal</artifactId>
    <version>8.0.0</version>
</dependency>
<dependency>
    <groupId>org.docx4j</groupId>
    <artifactId>docx4j-JAXB-ReferenceImpl</artifactId>
    <version>8.0.0</version>
</dependency>
<dependency>
    <groupId>org.docx4j</groupId>
    <artifactId>docx4j-JAXB-MOXy</artifactId>
    <version>8.0.0</version>
</dependency>
<dependency>
    <groupId>org.docx4j</groupId>
    <artifactId>docx4j-export-fo</artifactId>
    <version>8.0.0</version>
</dependency>

使用以下代码进行 docx 到 pdf 的转换。

import org.docx4j.Docx4J;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

public class DocToPDF {

    public static void main(String[] args) {
        
        try {
            InputStream templateInputStream = new FileInputStream("D:\\\\Workspace\\\\New\\\\Sample.docx");
            WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(templateInputStream);
            MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();

            String outputfilepath = "D:\\\\Workspace\\\\New\\\\Sample.pdf";
            FileOutputStream os = new FileOutputStream(outputfilepath);
            Docx4J.toPDF(wordMLPackage,os);
            os.flush();
            os.close();
        } catch (Throwable e) {

            e.printStackTrace();
        } 
    }

}

推荐阅读