Page 1 of 1

Using Template

PostPosted: Thu Feb 04, 2016 9:26 pm
by SMoltok
Hi,

I'm trying to use template with docx4j but my output file seems to be corrupt and then the PDF file is created with many empty spaces...

I'm using Word from Mac is it the issue ?

Thanks for answering !

Re: Using Template

PostPosted: Thu Feb 04, 2016 9:40 pm
by SMoltok
I've tested to create my PDF directly from the original template docx without replacing the variables, and there is too many space....

Code :
Code: Select all
public static void convert(String inputfilepath) {
    try {
        //String inputfilepath = System.getProperty("user.dir") + "/simpleDoc.docx";

        WordprocessingMLPackage wordMLPackage;
        if (inputfilepath==null) {
            System.out.println("No imput path passed, creating dummy document");
             wordMLPackage = WordprocessingMLPackage.createPackage();
            // SampleDocument.createContent(wordMLPackage.getMainDocumentPart());
        } else {
            System.out.println("Loading file from " + inputfilepath);
            wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));
        }

        Mapper fontMapper = new IdentityPlusMapper();

        PhysicalFont font = PhysicalFonts.get("Comic Sans MS");

        //fontMapper.put("Algerian", font);

       wordMLPackage.setFontMapper(fontMapper);
         
        FOSettings foSettings = Docx4J.createFOSettings();
        foSettings.setWmlPackage(wordMLPackage);
         
        String outputfilepath;
        if (inputfilepath==null) {
            outputfilepath = System.getProperty("user.dir") + "/simpleDoc.pdf";
        } else {
            outputfilepath = inputfilepath + ".pdf";
        }
         
        OutputStream os = new java.io.FileOutputStream(outputfilepath);
         
        Docx4J.toFO(foSettings, os, Docx4J.FLAG_NONE);
         
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Re: Using Template

PostPosted: Thu Feb 04, 2016 10:09 pm
by SMoltok
Well,

There was a problem with a not Class Found Error.

The missing one : org.apache.xmlgraphics.java2d.ps.TextHandler

I was using the following dependency :
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>xmlgraphics-commons</artifactId>
<version>1.5</version>

but with the 1.1 version, it rules....

Is there something wrong ??

Re: Using Template

PostPosted: Sat Feb 06, 2016 8:01 am
by jason
Don't use xmlgraphics-commons 1.1; things will break.