Search code examples
javapdfdocumentdocx4j

How can I add pTab elements to docx4j while converting document to pdf


I'm getting some error while converting document to pdf using docx4j library in Java. Sadly, my error is this

NOT IMPLEMENTED support for w:pict without v:imagedata

and it's showing up on the converted pdf instead of displaying the error in my java terminal.

I have gone through some article and questions,thus found this converting docx to pdf . However, I am uncertain how to use this in my code or convert it. This is my code :

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

import org.docx4j.convert.out.pdf.viaXSLFO.PdfSettings;
import org.docx4j.fonts.PhysicalFont;
import org.docx4j.fonts.PhysicalFonts;
import org.docx4j.model.structure.SectionWrapper;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;

public class docTopdf {
    public static void main(String[] args) {
        try {
            InputStream is = new FileInputStream(
                    new File(
                            "test.docx"));
            WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
                    .load(is);

            List<SectionWrapper> sections = wordMLPackage.getDocumentModel().getSections();
            for (int i = 0; i < sections.size(); i++) {

                wordMLPackage.getDocumentModel().getSections().get(i)
                        .getPageDimensions();
            }
            PhysicalFonts.discoverPhysicalFonts();
            @Deprecated
            Map<String, PhysicalFont> physicalFonts = PhysicalFonts.getPhysicalFonts();

            // 2) Prepare Pdf settings
            @Deprecated
                    PdfSettings pdfSettings = new PdfSettings();

            // 3) Convert WordprocessingMLPackage to Pdf
            @Deprecated
            org.docx4j.convert.out.pdf.PdfConversion conversion = new org.docx4j.convert.out.pdf.viaXSLFO.Conversion(
                    wordMLPackage);
            @Deprecated
            OutputStream out = new FileOutputStream(
                    new File(
                            "test.pdf"));
            conversion.output(out, pdfSettings);
        } catch (Throwable e) {

            e.printStackTrace();
        }
    }
}

And my pom.xml

   <dependency>
            <groupId>org.docx4j</groupId>
            <artifactId>docx4j</artifactId>
            <version>3.2.1</version>
        </dependency>

any help would be appreciated as I am noob to this conversion. Thanks in advance


Solution

  • Creating a PDF via XSL FO doesn't support w:pict without v:imagedata (ie a graphic which isn't a simple image).

    Whilst you could suppress the message by configuring logging appropriately, your PDF output would be lossy.

    Your options are to correct the input docx (ie use an image instead of whatever you currently have), or to use a PDF converter with appropriate support. For one option, see https://www.docx4java.org/blog/2020/03/documents4j-for-pdf-output/