Page 1 of 1

Data Loss Issue While Replacing a Text with Table

PostPosted: Thu Feb 23, 2012 4:08 am
by sureshbabubv
Hi Jason,

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
1329921086516_01-1000-14187_out.docx
(7.64 KiB) Downloaded 319 times
1329921086516_01-1000-14187.docx
(7.79 KiB) Downloaded 257 times

Re: Data Loss Issue While Replacing a Text with Table

PostPosted: Thu Feb 23, 2012 11:57 pm
by sureshbabubv
Hi Jason,

I identified the root cause of this issue.

"Computer/Application doesn't make the mistake, programmer does it "


I did the mistake while rendering the image Entry Text box to the document.

Instead of adding the Drawing/Pict to the RunContent, I added it directly to ParagraphContent.


Thanks & Regards,
B.V.Suresh Babu.