Page 1 of 1

I’m creating a table in a bookmark place but didn’t show PDF

PostPosted: Thu Nov 21, 2013 6:04 pm
by tariq
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);
}

Re: I’m creating a table in a bookmark place but didn’t show

PostPosted: Thu Nov 21, 2013 6:58 pm
by jason
Using docx4j 3.0 beta, I was able to see your table in PDF output from the docx you provided.

Re: I’m creating a table in a bookmark place but didn’t show

PostPosted: Fri Nov 22, 2013 6:47 pm
by tariq
Jason, I used docx4j 3.0 beta_2 but same problem occur.
I have Download docx4j 3.0 beta_2 zip file and extract then add all jar file in Project library
But when I complied file error display in log: java.lang.NoClassDefFoundError: org/apache/log4j/Level
And I also download log4j-1.2.17.jar and slf4j-1.7.5 add in library but problem is same not to display dynamically table in PDF
is there any missing jar or some think else. Help…….

Re: I’m creating a table in a bookmark place but didn’t show

PostPosted: Fri Nov 22, 2013 7:46 pm
by jason
What happens when you run your docx through https://github.com/plutext/docx4j/blob/ ... utPDF.java

What happens when you run it through the webapp (linked in menu above)?

Re: I’m creating a table in a bookmark place but didn’t show

PostPosted: Fri Nov 22, 2013 9:22 pm
by tariq
Jason, didn't work this code as you given to me link. can i send you source, docx, PDF generated. maybe i'm doing something wrong

Re: I’m creating a table in a bookmark place but didn’t show

PostPosted: Sat Nov 23, 2013 7:35 am
by jason
What are your answers to the 2 questions I asked you?

If you are saying the code linked in the first question "didn't work", what happened? You need to get that working.