docx4j: docx to pdf conversion - docx content not appearing page by page to pdf
Asked Answered
P

1

6

Issue: Converting docx to pdf using DOCX4J. Issues is content of docx not getting converted page by page to pdf documents. Few lines are get of page 2 is appearing in page 1 of pdf.

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>

Code:

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!!");

}

Wondering if docx4j has the setting to control it?

DOCX: docx

PDF: pdf

Tried it, but not much help Convert docx file into PDF with Java

Philosophism answered 14/5, 2020 at 18:53 Comment(3)
Can't help you, but I love how your sample text is the English translation of the traditional Latin filler text "Lorem ipsum". :-) (sections 1.10.32 and 1.10.33 of Cicero's De finibus bonorum et malorum)Papery
Why someone gave the negative vote for this question .Philosophism
I don't know, you provided a clear code example, you show the problem with screenshots. People are people?Papery
S
8

Upgrade your Docx4j version from 6.X to 8.X using the below dependencies to resolve this issue.

<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>

Use the below code for docx to pdf converstion.

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();
        } 
    }

}
Slipperwort answered 22/12, 2020 at 9:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.