Page 1 of 1

Borders in table

PostPosted: Tue Sep 06, 2011 3:20 am
by ghossio
I'm trying to create a table with docxj4. i created the table but without borders. I'm looking for but dont find anything. Any help will be grateful. Thanks for the attention and a salute. This is the code
Code: Select all
public static Tbl createTable(int rows, int cols, int cellWidthTwips) {
      
      log.info("Creando la tabla |||||||||||");
      Tbl tbl = Context.getWmlObjectFactory().createTbl();      
      
      // w:tblPr
      String strTblPr =  "<w:tblPr " + Namespaces.W_NAMESPACE_DECLARATION + ">"
         + "<w:tblStyle w:val=\"TableGrid\"/>"
         +    "<w:tblW w:w=\"0\" w:type=\"auto\"/>"
         +   "<w:tblLook w:val=\"04A0\"/>"
         + "</w:tblPr>";
      TblPr tblPr = null;
      try {
         //tblPr = (TblPr)XmlUtils.unmarshalString(strTblPr);
      } catch (Exception e) {
         // Shouldn't happen
         e.printStackTrace();
      }
      tbl.setTblPr(tblPr);
      
      // <w:tblGrid><w:gridCol w:w="4788"/>      
      TblGrid tblGrid = Context.getWmlObjectFactory().createTblGrid();
      tbl.setTblGrid(tblGrid);
      // Add required <w:gridCol w:w="4788"/>
      for (int i=1 ; i<=cols; i++) {
         TblGridCol gridCol = Context.getWmlObjectFactory().createTblGridCol();
         gridCol.setW(BigInteger.valueOf(cellWidthTwips));
         tblGrid.getGridCol().add(gridCol);
      }
            
      // Now the rows
      for (int j=1 ; j<=rows; j++) {
         Tr tr = Context.getWmlObjectFactory().createTr();
         tbl.getEGContentRowContent().add(tr);
         
         // The cells
         for (int i=1 ; i<=cols; i++) {

            Tc tc = Context.getWmlObjectFactory().createTc();

            tr.getEGContentCellContent().add(tc);

            TcPr tcPr = Context.getWmlObjectFactory().createTcPr();
            tc.setTcPr(tcPr);
            TblWidth cellWidth = Context.getWmlObjectFactory().createTblWidth();
            tcPr.setTcW(cellWidth);
            cellWidth.setType("dxa");
            cellWidth.setW(BigInteger.valueOf(cellWidthTwips));
            org.docx4j.wml.ObjectFactory factory = Context.getWmlObjectFactory();
            org.docx4j.wml.P p = factory.createP();
            org.docx4j.wml.Text t = factory.createText();
            t.setValue("hi: " + i + ", " + j);
            org.docx4j.wml.R run = factory.createR();
            run.getRunContent().add(t);
            
            p.getParagraphContent().add(run);
            tc.getEGBlockLevelElts().add(p);
            
            }
         
      }
      log.info("tabla creada|||||||||||");
      return tbl;
   }

Re: Borders in table

PostPosted: Tue Sep 06, 2011 4:59 am
by ghossio
ok i found it. the problem was tblr. that source create a table with borders:

Code: Select all
public static Tbl createTable(int rows, int cols, int cellWidthTwips) {
      
      log.info("Creando la tabla |||||||||||");
      Tbl tbl = Context.getWmlObjectFactory().createTbl();      
      
      // w:tblPr
      String strTblPr =  "<w:tblPr " + Namespaces.W_NAMESPACE_DECLARATION + ">"
         + "<w:tblStyle w:val=\"TableGrid\"/>"
         +    "<w:tblW w:w=\"0\" w:type=\"auto\"/>"
         +   "<w:tblLook w:val=\"04A0\"/>"
         + "</w:tblPr>";
      TblPr tblPr = null;

        try {

                        tblPr = (TblPr)XmlUtils.unmarshalString(strTblPr);

        } catch (JAXBException e) {

                        // Shouldn't happen

                        e.printStackTrace();

        }

        tbl.setTblPr(tblPr);
      
      // <w:tblGrid><w:gridCol w:w="4788"/>      
      TblGrid tblGrid = Context.getWmlObjectFactory().createTblGrid();
      tbl.setTblGrid(tblGrid);
      // Add required <w:gridCol w:w="4788"/>
      for (int i=1 ; i<=cols; i++) {
         TblGridCol gridCol = Context.getWmlObjectFactory().createTblGridCol();
         gridCol.setW(BigInteger.valueOf(cellWidthTwips));
         tblGrid.getGridCol().add(gridCol);
      }
            
      // Now the rows
      for (int j=1 ; j<=rows; j++) {
         Tr tr = Context.getWmlObjectFactory().createTr();
         tbl.getEGContentRowContent().add(tr);
         
         // The cells
         for (int i=1 ; i<=cols; i++) {

            Tc tc = Context.getWmlObjectFactory().createTc();

            tr.getEGContentCellContent().add(tc);

            TcPr tcPr = Context.getWmlObjectFactory().createTcPr();
            tc.setTcPr(tcPr);
            TblWidth cellWidth = Context.getWmlObjectFactory().createTblWidth();
            tcPr.setTcW(cellWidth);
            cellWidth.setType("dxa");
            cellWidth.setW(BigInteger.valueOf(cellWidthTwips));
            org.docx4j.wml.ObjectFactory factory = Context.getWmlObjectFactory();
            org.docx4j.wml.P p = factory.createP();
            org.docx4j.wml.Text t = factory.createText();
            t.setValue("hi: " + i + ", " + j);
            org.docx4j.wml.R run = factory.createR();
            run.getRunContent().add(t);
            
            p.getParagraphContent().add(run);
            tc.getEGBlockLevelElts().add(p);
            
            
            
             try{
                 TcPrInner.TcBorders tcBorders = Context.getWmlObjectFactory().createTcPrInnerTcBorders();
                 tcPr.setTcBorders(tcBorders);
                 log.error("Esto es tcPr: " + tcPr);
         
         
                TblBorders tblBorders = Context.getWmlObjectFactory().createTblBorders();
                tblPr.setTblBorders(tblBorders);
                log.error("Esto es tblPr: " + tblPr);
            
                //tcPr.getTcBorders().setRight( (CTBorder)this.getObject() );
             }catch(Exception e){ log.error("Error del exception " + e.getMessage() + " IIIIIIII ");
             e.printStackTrace();}
                
             }
         
      }
      log.info("tabla creada|||||||||||");
      return tbl;
   }