I'm testing docx4j for a project to export docx to pdf. (Sorry for my poor English).
I have tried export to PDF and it doesn't look like to my docx :
- the header is missing
- the margins are not respected.
Am I doing something wrong ?
I'm using docx4j-nightly-20100422.jar and the code which generates the pdf is :
- Code: Select all
String inputfilepath = "C:\\Documents and Settings\\ndt\\Bureau\\edition.docx";
boolean save = true;
String outputfilepath = "C:\\Documents and Settings\\ndt\\Bureau\\pv.docx";
// Open a document from the file system
// 1. Load the Package
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));
// 2. Fetch the document part
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
org.docx4j.wml.Hdr hdr = wordMLPackage.getDocumentModel().getSections().get(0).getHeaderFooterPolicy().getDefaultHeader().getJaxbElement();
org.docx4j.wml.Document wmlDocumentEl = (org.docx4j.wml.Document) documentPart.getJaxbElement();
String header = XmlUtils.marshaltoString(hdr, true);
HashMap<String, String> mappings = new HashMap<String, String>();
mappings.put("edit", "INJEANDEL");
mappings.put("titre", "Procès Verbal");
mappings.put("type", "PV");
//valorize template
Object obj = XmlUtils.unmarshallFromTemplate(header, mappings);
wordMLPackage.getDocumentModel().getSections().get(0).getHeaderFooterPolicy().getDefaultHeader().setJaxbElement((Hdr)obj);
//change JaxbElement
//documentPart.setJaxbElement((Document) obj);
// Save it
if (save) {
SaveToZipFile saver = new SaveToZipFile(wordMLPackage);
saver.save(outputfilepath);
System.out.println( "Saved output to:" + outputfilepath );
} else {
// Display the Main Document Part.
}
//Fonts identity mapping – best on Microsoft Windows
wordMLPackage.setFontMapper(new IdentityPlusMapper());
// Set up converter
org.docx4j.convert.out.pdf.PdfConversion c = new org.docx4j.convert.out.pdf.viaXSLFO.Conversion(wordMLPackage);
// Write to output stream
OutputStream os = new java.io.FileOutputStream("C:\\Documents and Settings\\ndt\\Bureau\\pv.pdf");
c.output(os);
Thank you for your help in advance.
Regards,
Slucky31