My problem is I’m creating table in bookmark place in .docx file its display dynamic table in .docx file but when I convert .docx file to PDF it doesn’t display dynamic table in PDF. i use (docx4j 2.8.1).
Its my code.
Much appreciated,
private void createTableInTemplate()throws Exception{
InputStream is = new FileInputStream(new File("c:\example.docx"));
wordMLPackage = WordprocessingMLPackage.load(is);
final String xpath = "//w:bookmarkStart[@w:name='tableg']/..";
List objects = wordMLPackage.getMainDocumentPart().getJAXBNodesViaXPath(xpath, false);
P para = (P)XmlUtils.unwrap(objects.get(0));
Tbl tbl = createTable();
R run = Context.getWmlObjectFactory().createR();
run.getContent().add(tbl);
para.getContent().add(run);
wordMLPackage.save(new File("C:\test.docx"));
if(is!=null)
is.close();
convertIntoPDF("C:\test.pdf");
System.out.println(" Document Successfully Created ");
}
private Tbl createTable()throws Exception {
Tbl tbl = Context.getWmlObjectFactory().createTbl();
int DEFAULT_PAGE_WIDTH_TWIPS = 9600;
addBorders(tbl);
Tr newRow = null;
for(int i=0; i<=2; i++){
newRow = Context.getWmlObjectFactory().createTr();
for(int j=0; j<=3; j++){
org.docx4j.wml.P p = Context.getWmlObjectFactory().createP();
org.docx4j.wml.Text t = Context.getWmlObjectFactory().createText();
t.setValue(" Test ");
org.docx4j.wml.R run = Context.getWmlObjectFactory().createR();
run.getContent().add(t);
p.getContent().add(run);
org.docx4j.wml.RPr rpr = Context.getWmlObjectFactory().createRPr();
org.docx4j.wml.BooleanDefaultTrue b = new org.docx4j.wml.BooleanDefaultTrue();
b.setVal(true);
rpr.setB(b);
run.setRPr(rpr);
org.docx4j.wml.PPr ppr = Context.getWmlObjectFactory().createPPr();
p.setPPr( ppr );
org.docx4j.wml.ParaRPr paraRpr = Context.getWmlObjectFactory().createParaRPr();
ppr.setRPr(paraRpr);
rpr.setB(b);
Tc newCell = Context.getWmlObjectFactory().createTc();
newCell.getContent().add(p);
newRow.getContent().add(newCell);
}
tbl.getContent().add(newRow);
}
System.out.println(" Created Table ");
return tbl;
}
private void convertIntoPDF(String fileName)throws Exception{
InputStream is = new FileInputStream(new File("c:\test.docx"));
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(is);
PdfSettings pdfSettings = new PdfSettings();
OutputStream out = new FileOutputStream(new File(fileName));
PdfConversion converter = new org.docx4j.convert.out.pdf.viaXSLFO.Conversion(wordMLPackage);
converter.output(out, pdfSettings);
if(is !=null)
is.close();
if(out !=null)
out.close();
System.out.println(" Converted To PDF ");
}
private void addBorders(Tbl table) {
table.setTblPr(new TblPr());
CTBorder border = new CTBorder();
border.setColor("auto");
border.setSz(new BigInteger("1"));
border.setSpace(new BigInteger("0"));
border.setVal(STBorder.SINGLE);
TblBorders borders = new TblBorders();
borders.setBottom(border);
borders.setLeft(border);
borders.setRight(border);
borders.setTop(border);
borders.setInsideH(border);
borders.setInsideV(border);
table.getTblPr().setTblBorders(borders);
}