- Code: Select all
Docx4J.toFO(foSettings, myStream, Docx4J.FLAG_EXPORT_PREFER_XSL);
The stacktrace is
- Code: Select all
java.lang.NullPointerException
at org.docx4j.convert.out.common.preprocess.ParagraphStylesInTableFix$StyleRenamer.getCellPStyle(ParagraphStylesInTableFix.java:353)
at org.docx4j.convert.out.common.preprocess.ParagraphStylesInTableFix$StyleRenamer.apply(ParagraphStylesInTableFix.java:543)
at org.docx4j.convert.out.common.preprocess.ParagraphStylesInTableFix$StyleRenamer.walkJAXBElements(ParagraphStylesInTableFix.java:592)
at org.docx4j.convert.out.common.preprocess.ParagraphStylesInTableFix$StyleRenamer.walkJAXBElements(ParagraphStylesInTableFix.java:599)
at org.docx4j.convert.out.common.preprocess.ParagraphStylesInTableFix$StyleRenamer.walkJAXBElements(ParagraphStylesInTableFix.java:599)
at org.docx4j.convert.out.common.preprocess.ParagraphStylesInTableFix$StyleRenamer.walkJAXBElements(ParagraphStylesInTableFix.java:599)
at org.docx4j.TraversalUtil.<init>(TraversalUtil.java:209)
at org.docx4j.convert.out.common.preprocess.ParagraphStylesInTableFix.process(ParagraphStylesInTableFix.java:156)
at org.docx4j.convert.out.common.Preprocess.process(Preprocess.java:185)
at org.docx4j.convert.out.common.AbstractWmlExporter.preprocess(AbstractWmlExporter.java:51)
at org.docx4j.convert.out.common.AbstractWmlExporter.preprocess(AbstractWmlExporter.java:32)
at org.docx4j.convert.out.common.AbstractExporter.export(AbstractExporter.java:64)
at org.docx4j.Docx4J.toFO(Docx4J.java:466)
I do not know what to include in my code to avoid this exception. It seems some style is missing.
The DOCX file I have used when running the AddIn is attached (Tabela.docx). The generated one by docx4j that does not export well is the generatedUsingDocx4j.docx.
If I try to export to PDF direct from the attached file, the generated PDF is OK. But, if I run the Add In based on that file, get the code, create a docx based on it and generate a PDF from it, then the NPE arises.
Here is the code I used to generate the docx and the PDF:
- Code: Select all
public final ByteArrayOutputStream bindDocx(File templateWordFile, boolean returnAsPdf) throws Exception {
ByteArrayOutputStream wordAsStream = new ByteArrayOutputStream();
WordprocessingMLPackage output = null;
try {
WordprocessingMLPackage wordMLPackage = (WordprocessingMLPackage)OpcPackage.load(templateWordFile);
List<Map<DataFieldName, String>> data = new ArrayList<Map<DataFieldName, String>>();
data.add(this.getFieldSampleValues());
org.docx4j.model.fields.merge.MailMerger.setMERGEFIELDInOutput(OutputField.KEEP_MERGEFIELD);
output = org.docx4j.model.fields.merge.MailMerger.getConsolidatedResultCrude(wordMLPackage, data, true);
output = this.bindComplexElementsToDocx(context, output);
if(returnAsPdf) {
Mapper fontMapper = new IdentityPlusMapper();
output.setFontMapper(fontMapper);
FOSettings foSettings = Docx4J.createFOSettings();
foSettings.setWmlPackage(output);
Docx4J.toFO(foSettings, wordAsStream , Docx4J.FLAG_EXPORT_PREFER_XSL);
if (wordMLPackage.getMainDocumentPart().getFontTablePart()!=null) {
wordMLPackage.getMainDocumentPart().getFontTablePart().deleteEmbeddedFontTempFiles();
}
} else {
output.save(wordAsStream );
}
return wordAsStream;
} catch (Exception e) {
Logger.error(e.getMessage();
throw e;
} finally {
try {
wordAsStream.close();
} catch (IOException e) {
Logger.error("Fail to close stream", e);
}
}
}
public WordprocessingMLPackage bindComplexElementsToDocx(JoinRHContext context, WordprocessingMLPackage output) throws JoinRHException {
try {
org.docx4j.wml.ObjectFactory wmlObjectFactory = new org.docx4j.wml.ObjectFactory();
// Create object for tbl (wrapped in JAXBElement)
Tbl tbl = wmlObjectFactory.createTbl();
JAXBElement<org.docx4j.wml.Tbl> tblWrapped = wmlObjectFactory.createBodyTbl(tbl);
// Create object for tr
Tr tr = wmlObjectFactory.createTr();
tbl.getContent().add( tr);
// Create object for tc (wrapped in JAXBElement)
Tc tc = wmlObjectFactory.createTc();
JAXBElement<org.docx4j.wml.Tc> tcWrapped = wmlObjectFactory.createTrTc(tc);
tr.getContent().add( tcWrapped);
// Create object for p
P p = wmlObjectFactory.createP();
tc.getContent().add( p);
// Create object for pPr
PPr ppr = wmlObjectFactory.createPPr();
p.setPPr(ppr);
// Create object for rPr
ParaRPr pararpr = wmlObjectFactory.createParaRPr();
ppr.setRPr(pararpr);
// Create object for b
BooleanDefaultTrue booleandefaulttrue = wmlObjectFactory.createBooleanDefaultTrue();
pararpr.setB(booleandefaulttrue);
// Create object for color
Color color = wmlObjectFactory.createColor();
pararpr.setColor(color);
color.setVal( "B66B34");
// Create object for jc
Jc jc = wmlObjectFactory.createJc();
ppr.setJc(jc);
jc.setVal(org.docx4j.wml.JcEnumeration.CENTER);
// Create object for r
R r = wmlObjectFactory.createR();
p.getContent().add( r);
// Create object for rPr
RPr rpr = wmlObjectFactory.createRPr();
r.setRPr(rpr);
// Create object for b
BooleanDefaultTrue booleandefaulttrue2 = wmlObjectFactory.createBooleanDefaultTrue();
rpr.setB(booleandefaulttrue2);
// Create object for color
Color color2 = wmlObjectFactory.createColor();
rpr.setColor(color2);
color2.setVal( "B66B34");
// Create object for t (wrapped in JAXBElement)
Text text = wmlObjectFactory.createText();
JAXBElement<org.docx4j.wml.Text> textWrapped = wmlObjectFactory.createRT(text);
r.getContent().add( textWrapped);
text.setValue( "Data");
// Create object for tcPr
TcPr tcpr = wmlObjectFactory.createTcPr();
tc.setTcPr(tcpr);
// Create object for tcW
TblWidth tblwidth = wmlObjectFactory.createTblWidth();
tcpr.setTcW(tblwidth);
tblwidth.setW( BigInteger.valueOf( 1763) );
tblwidth.setType( "dxa");
//TC2 until TC19 removed to shorten post
Tc tc20 = wmlObjectFactory.createTc();
JAXBElement<org.docx4j.wml.Tc> tcWrapped20 = wmlObjectFactory.createTrTc(tc20);
tr5.getContent().add( tcWrapped20);
// Create object for p
P p20 = wmlObjectFactory.createP();
tc20.getContent().add( p20);
// Create object for pPr
PPr ppr20 = wmlObjectFactory.createPPr();
p20.setPPr(ppr20);
// Create object for jc
Jc jc20 = wmlObjectFactory.createJc();
ppr20.setJc(jc20);
jc20.setVal(org.docx4j.wml.JcEnumeration.RIGHT);
// Create object for r
R r20 = wmlObjectFactory.createR();
p20.getContent().add( r20);
// Create object for t (wrapped in JAXBElement)
Text text20 = wmlObjectFactory.createText();
JAXBElement<org.docx4j.wml.Text> textWrapped20 = wmlObjectFactory.createRT(text20);
r20.getContent().add( textWrapped20);
text20.setValue( "3,66");
// Create object for tcPr
TcPr tcpr20 = wmlObjectFactory.createTcPr();
tc20.setTcPr(tcpr20);
// Create object for tcW
TblWidth tblwidth20 = wmlObjectFactory.createTblWidth();
tcpr20.setTcW(tblwidth20);
tblwidth20.setW( BigInteger.valueOf( 1764) );
tblwidth20.setType( "dxa");
// Create object for shd
CTShd shd12 = wmlObjectFactory.createCTShd();
tcpr20.setShd(shd12);
shd12.setFill( "auto");
shd12.setColor( "auto");
shd12.setVal(org.docx4j.wml.STShd.CLEAR);
// Create object for tblPr
TblPr tblpr = wmlObjectFactory.createTblPr();
tbl.setTblPr(tblpr);
// Create object for tblLook
CTTblLook tbllook = wmlObjectFactory.createCTTblLook();
tblpr.setTblLook(tbllook);
tbllook.setVal( "04A0");
tbllook.setFirstColumn(org.docx4j.sharedtypes.STOnOff.ONE);
tbllook.setFirstRow(org.docx4j.sharedtypes.STOnOff.ONE);
tbllook.setLastColumn(org.docx4j.sharedtypes.STOnOff.ZERO);
tbllook.setLastRow(org.docx4j.sharedtypes.STOnOff.ZERO);
tbllook.setNoHBand(org.docx4j.sharedtypes.STOnOff.ZERO);
tbllook.setNoVBand(org.docx4j.sharedtypes.STOnOff.ONE);
// Create object for tblStyle
CTTblPrBase.TblStyle tblprbasetblstyle = wmlObjectFactory.createCTTblPrBaseTblStyle();
tblpr.setTblStyle(tblprbasetblstyle);
tblprbasetblstyle.setVal( "Tabelacomgrade");
// Create object for tblW
TblWidth tblwidth21 = wmlObjectFactory.createTblWidth();
tblpr.setTblW(tblwidth21);
tblwidth21.setW( BigInteger.valueOf( 0) );
tblwidth21.setType( "auto");
// Create object for tblGrid
TblGrid tblgrid = wmlObjectFactory.createTblGrid();
tbl.setTblGrid(tblgrid);
// Create object for gridCol
TblGridCol tblgridcol = wmlObjectFactory.createTblGridCol();
tblgrid.getGridCol().add( tblgridcol);
tblgridcol.setW( BigInteger.valueOf( 1763) );
// Create object for gridCol
TblGridCol tblgridcol2 = wmlObjectFactory.createTblGridCol();
tblgrid.getGridCol().add( tblgridcol2);
tblgridcol2.setW( BigInteger.valueOf( 1764) );
// Create object for gridCol
TblGridCol tblgridcol3 = wmlObjectFactory.createTblGridCol();
tblgrid.getGridCol().add( tblgridcol3);
tblgridcol3.setW( BigInteger.valueOf( 1763) );
// Create object for gridCol
TblGridCol tblgridcol4 = wmlObjectFactory.createTblGridCol();
tblgrid.getGridCol().add( tblgridcol4);
tblgridcol4.setW( BigInteger.valueOf( 1764) );
List<Object> paragraphs = this.getAllElementFromObject(output.getMainDocumentPart(), P.class);
boolean found = false;
for (int i=0; !found && i<paragraphs.size(); i++) {
Object parag = paragraphs.get(i);
List<Object> textos = this.getAllElementFromObject(parag, Text.class);
for (Object content : textos) {
if (content instanceof Text && ((Text)content).getValue().contains("placeholder")) {
found = true;
output.getMainDocumentPart().getContent().set(i, tblWrapped);
((Text)content).setValue("");
break;
}
}
}
return output;
} catch (Exception e) {
Logger.error("Fail to bind placeholder", e);
throw e;
}
}
I did not included the section the AddIn suggested. Is it a problem?
If you need any detail, please, let me know. Thanks in advance!