I programatically generate a document using docx4j.
I set the following document defaults when generating the document.
- Code: Select all
Styles styles = dleReportMLPackage.getMainDocumentPart().getStyleDefinitionsPart().getJaxbElement();
DocDefaults docDefaults = styles.getDocDefaults();
RFonts defaultRPrRFonts = docDefaults.getRPrDefault().getRPr().getRFonts();
defaultRPrRFonts.setAscii(TIMES_NEW_ROMAN);
defaultRPrRFonts.setHAnsi(TIMES_NEW_ROMAN);
defaultRPrRFonts.setCs(TIMES_NEW_ROMAN);
I then run the sample ConvertToPDF code to convert the document, I get no errors.
- Code: Select all
public boolean convert(WordprocessingMLPackage wMLP) throws Docx4JException {
// Font regex (optional)
// Set regex if you want to restrict to some defined subset of fonts
// Here we have to do this before calling createContent,
// since that discovers fonts
// String regex = null;
// Windows:
String regex = ".*(calibri|cour|arial|times|comic|georgia|impact|LSANS|pala|tahoma|trebuc|verdana|symbol|webdings|wingding).*";
PhysicalFonts.setRegex(regex);
// Document loading (required)
// Set up font mapper (optional)
Mapper fontMapper = new IdentityPlusMapper();
try {
wMLP.setFontMapper(fontMapper);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// .. example of mapping missing font Algerian to installed font
// Comic
// Sans MS
PhysicalFont font = PhysicalFonts.getPhysicalFonts().get(
"Times New Roman");
fontMapper.getFontMappings().put("Calibri", font);
// New code
FOSettings foSettings = Docx4J.createFOSettings();
if (saveFO) {
foSettings.setFoDumpFile(new java.io.File(DIR_OUT + inputfile + ".fo"));
}
foSettings.setWmlPackage(wMLP);
// exporter writes to an OutputStream.
try {
String outputFile = FilenameUtils.removeExtension(inputfile) + ".pdf";
OutputStream os = new java.io.FileOutputStream(DIR_OUT + outputFile);
// Don't care what type of exporter you use
Docx4J.toFO(foSettings, os, Docx4J.FLAG_NONE);
System.out.println("Saved " + DIR_OUT + outputFile);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// end new code
return false;
}
If I open the original generated word docucent and just add a simple character and save in Word 2010.
I then run the ConvertToPDF code to try and convert to PDF, I get a NULL pointer error with the default font settings.
The error exception is a NULL pointer from the RunFontSelector when retriving getPhysicalFont(getDafaultFont())
When retriving the PropertResolver, I notice, in debug, the documentDefaultPPr and documentdeaultRPr values are null.
This is then causing the NULL pointer exception when calling propertyResolver.getDocumentDefaultRPr().getRFonts();
The online ConvertPDF converter tool, running docx4j 2.8, is not having this issue when converting this document.
It appears to be failing with the new docx4j 3.0.0 version.
It also appears, when I save the document in word 2010, it is overriding my deafult document settings (from above). Using Open XML 2.5 toolkit to validate the document code.
Document enclosed
Please advise.
Thanks