Can you please help me on this issue. I have been struggling on this from last 2days.
I am trying to replace a text in an existing document with a Table that was created dynamically. But I getting data loss issue on this process.
Please find the attached documents "1329921086516_01-1000-14187.docx" original document.
- Code: Select all
public static void main(String[] aa) throws Exception
{
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
.load(new java.io.File(
"test-results/1329921086516_01-1000-14187.docx"));
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
String xpath = "//w:r[w:t[contains(text(),'auto')]]";
List<Object> list = documentPart.getJAXBNodesViaXPath(xpath, false);
if (list.isEmpty()) {
System.out.println("Couldn't find the run");
return;
}
R r = (R) list.get(0);
P parent = (P) r.getParent();
int index = documentPart.getContent().indexOf(parent);
org.docx4j.wml.ObjectFactory factory = new org.docx4j.wml.ObjectFactory();
Tbl tbl = factory.createTbl();
String strTblPr = "<w:tblPr "
+ Namespaces.W_NAMESPACE_DECLARATION
+ ">"
+ "<w:tblStyle w:val=\"TableGrid\"/>"
+ "<w:tblW w:w=\"0\" w:type=\"auto\"/>"
+ "<w:tblBorders>"
+ "<w:top w:val=\"single\" w:sz=\"4\" w:space=\"0\" w:color=\"auto\" /> "
+ "<w:left w:val=\"single\" w:sz=\"4\" w:space=\"0\" w:color=\"auto\" /> "
+ "<w:bottom w:val=\"single\" w:sz=\"4\" w:space=\"0\" w:color=\"auto\" /> "
+ "<w:right w:val=\"single\" w:sz=\"4\" w:space=\"0\" w:color=\"auto\" /> "
+ "<w:insideH w:val=\"single\" w:sz=\"4\" w:space=\"0\" w:color=\"auto\" /> "
+ "<w:insideV w:val=\"single\" w:sz=\"4\" w:space=\"0\" w:color=\"auto\" /> "
+ "</w:tblBorders>" + "<w:tblLook w:val=\"04A0\"/>"
+ "</w:tblPr>";
TblPr tblPr = null;
try {
tblPr = (TblPr) XmlUtils.unmarshalString(strTblPr);
} catch (JAXBException e) {
e.printStackTrace();
}
tbl.setTblPr(tblPr);
TblGrid tblGrid = factory.createTblGrid();
tbl.setTblGrid(tblGrid);
for (int i = 1; i <= 6; i++) {
TblGridCol gridCol = factory.createTblGridCol();
tblGrid.getGridCol().add(gridCol);
}
ArrayList alTblColumnHeadrsList = new ArrayList();
alTblColumnHeadrsList.add("Record Number");
alTblColumnHeadrsList.add("Trademark");
alTblColumnHeadrsList.add("Status");
alTblColumnHeadrsList.add("Int'l Class(es)");
alTblColumnHeadrsList.add("Serial Number");
alTblColumnHeadrsList.add("Page");
Tr tr1 = factory.createTr();
tbl.getContent().add(tr1);
for (int i = 0; i <= 5; i++) {
Tc tc = factory.createTc();
tr1.getContent().add(tc);
TcPr tcPr = factory.createTcPr();
CTVerticalJc vertical = factory.createCTVerticalJc();
vertical.setVal(STVerticalJc.CENTER);
tcPr.setVAlign(vertical);
tc.setTcPr(tcPr);
TblWidth cellWidth = factory.createTblWidth();
cellWidth.setW(new BigInteger("1800"));
tcPr.setTcW(cellWidth);
cellWidth.setType("dxa");
org.docx4j.wml.P p = factory.createP();
org.docx4j.wml.PPr ppr = factory.createPPr();
org.docx4j.wml.Jc jc = factory.createJc();
jc.setVal(JcEnumeration.CENTER);
ppr.setJc(jc);
org.docx4j.wml.RPr objRpr = factory.createRPr();
RStyle rStyle = factory.createRStyle();
rStyle.setVal("bold-field-value1");
objRpr.setRStyle(rStyle);
org.docx4j.wml.Text t = factory.createText();
t.setValue((String) alTblColumnHeadrsList.get(i));
org.docx4j.wml.R run = factory.createR();
run.setRPr(objRpr);
run.getContent().add(t);
p.getContent().add(run);
tc.getContent().add(p);
}
for (int j = 0; j <= 2; j++) {
Tr tr = factory.createTr();
tbl.getContent().add(tr);
for (int i = 1; i <= 6; i++) {
Tc tc = factory.createTc();
tr.getContent().add(tc);
TcPr tcPr = factory.createTcPr();
CTVerticalJc vertical = factory.createCTVerticalJc();
vertical.setVal(STVerticalJc.CENTER);
tcPr.setVAlign(vertical);
tc.setTcPr(tcPr);
TblWidth cellWidth = factory.createTblWidth();
cellWidth.setW(new BigInteger("1800"));
tcPr.setTcW(cellWidth);
cellWidth.setType("dxa");
org.docx4j.wml.P p = factory.createP();
org.docx4j.wml.PPr ppr = factory.createPPr();
org.docx4j.wml.Jc jc = factory.createJc();
jc.setVal(JcEnumeration.CENTER);
ppr.setJc(jc);
p.setPPr(ppr);
org.docx4j.wml.Text t = factory.createText();
t.setValue("" + j);
org.docx4j.wml.R run = factory.createR();
run.getContent().add(t);
p.getContent().add(run);
tc.getContent().add(p);
}
}
parent.getContent().remove(r);
documentPart.getContent().add(index, tbl);
System.out.println("before saving");
wordMLPackage.save(new File(
"test-results/1329921086516_01-1000-14187_out.docx"));
}
}// end of main method
"1329921086516_01-1000-14187_out.docx" is the document that was generated after executing the above code.
Please advice me where I am doing the mistake.
Thanks & Regards,
B.V.Suresh Babu