Page 1 of 1

setting page size for PDF export

PostPosted: Thu May 08, 2014 7:55 pm
by anju
Hi,
I've different docx files (created in Word and filled with XdocReport). Two of these files must be generated as A5 PDFs (so, that the printer automaticaly will choose the correct tray). A5 isn't yet supported in docx4J, so I wrote following code:

Code: Select all
void setPageSize(WordprocessingMLPackage wordMLPackage, String sz, boolean landscape){
       
   Body body = wordMLPackage.getMainDocumentPart().getJaxbElement().getBody();
       PgSz          pgsz = body.getSectPr().getPgSz();
       
       if (sz.equals(A5)) {
         pgsz.setCode(BigInteger.valueOf(10)); // ??? A3 is 8  - A4 is 9 - may be 10 for A5
         if (landscape) {
            pgsz.setOrient(STPageOrientation.LANDSCAPE);
            pgsz.setW(BigInteger.valueOf(11907));
            pgsz.setH(BigInteger.valueOf(8419));
         } else {
            pgsz.setW(BigInteger.valueOf(8419));
            pgsz.setH(BigInteger.valueOf(11907));
         }
            }
    }


And the calling method:


Code: Select all
public static byte[] convertDOCXToPDF(byte[] docdata)
    {
        byte[] result = null;

        ByteArrayOutputStream out = new ByteArrayOutputStream();

        try
        {
            WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new ByteArrayInputStream(docdata));

            setPageSize(wordMLPackage, "A5", false);

            FOSettings foSettings = Docx4J.createFOSettings();
            foSettings.setWmlPackage(wordMLPackage);
            Docx4J.toFO(foSettings, out, Docx4J.FLAG_NONE);
            result = out.toByteArray();
            return result;
        }
        catch (Docx4JException e)....

    }


The result is still an A4 PDF :-(
The docx file is in format A5 - best would be to read the size, orientation and margins from the inputdata, but that seems not to be supported?

any help would be nice
thanx
Andreas

Re: setting page size for PDF export

PostPosted: Thu May 08, 2014 8:24 pm
by jason
The hope was that users would contribute code for the papers sizes they need. That hasn't happened.

https://github.com/plutext/docx4j/commi ... 24872fdf5e adds A5; the numbers in it (including 'code') come from creating a docx in Word with the required paper size, then inspecting the resulting sectPr/pgSz. Hopefully this is enough of a hint for others to add paper sizes in the future :-)

To test, I used:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
                WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage(PageSizePaper.A5, false);
 
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


then outputed a PDF. The PDF was A5 size.