docx4j: docx转pdf - pdf中的docx内容未逐页显示

5
问题:使用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是否有控制它的设置?
DOCX: docx PDF: pdf 尝试了一下,但没有什么帮助: 用Java将docx文件转换为PDF

帮不了你,但我喜欢你的示例文本是传统的拉丁填充文本“Lorem ipsum”的英语翻译。 :-) (见 Cicero 的《关于善恶的境界》第 1.10.32 和 1.10.33 节) - Amedee Van Gasse
为什么有人给这个问题负面投票? - Prabhu Patel
我不知道,你提供了一个清晰的代码示例,你用截图展示了问题。人就是人? - Amedee Van Gasse
1个回答

7

使用以下依赖项升级您的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();
        } 
    }

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接