Using content controls to display repeat data in tables.

Topics: Developer Forum
Jul 13, 2010 at 9:57 AM

Hi All,

We have a requirement to generate a word 2007 template with tables and populate the tables with the data from the database mapped columns. We are looking at achieving this using word 2007 content control toolkit and open xml. Can anyone please suggest any pointers or links or samples mainly related to how to display and populate such tabular data using content controls?

Thanks in advance.

Regards,

Laxmi

Jul 15, 2010 at 5:03 AM

Hi Laxmi. 

See the post I just wrote, which specifically covers populating table rows via what I call a "repeat". 

It'd be great to know whether this addresses your needs, or if not, how the proposal would need to be extended to do so.

Although I say "proposal", please note that this is implemented and working in docx4j v2.5.0.

cheers .. Jason

Jul 19, 2010 at 1:19 PM

Hi Jason,

Thanks a lot for your response!

Could not get time today to look into your complete post, would definetely check it and let you know:).

Meanwhile, I checked that the source code attached is the implementation in Java. We are looking for the above solution in .Net. Has the same been implemented in .Net as well?

Please let me know.

Regards,

Laxmi

 

Jul 22, 2010 at 5:17 PM

I haven't made a .NET implementation, though given that it looks like this is now likely to become part of the content control toolkit, that is a logical next step.

I'd be inclined to build it on top of the Open XML SDK (v2) - are you using that?

cheers .. Jason

Oct 28, 2010 at 8:52 AM
Edited Oct 28, 2010 at 9:27 AM

Hi Jason,

Please do me needful.

I need to add content inside the cell in a table using docx4j. I herewith attach my code.

Its very urgent. pls help...

 

package org.docx4j;

 

import java.io.File;

import java.math.BigInteger;

import java.util.*;

 

import javax.xml.bind.JAXBException;

 

 

import org.docx4j.jaxb.Context;

import org.docx4j.model.datastorage.CustomXmlDataStorage;

import org.docx4j.model.datastorage.CustomXmlDataStorageImpl;

import org.docx4j.openpackaging.Base;

import org.docx4j.openpackaging.packages.WordprocessingMLPackage;

import org.docx4j.openpackaging.parts.PartName;

import org.docx4j.openpackaging.parts.Parts;

import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;

import org.docx4j.openpackaging.parts.relationships.Namespaces;

import org.docx4j.openpackaging.exceptions.InvalidFormatException;

import org.docx4j.openpackaging.io.SaveToZipFile;

import org.docx4j.model.table.*;

import org.docx4j.wml.*;

import org.apache.fop.afp.*;

import org.docx4j.openpackaging.exceptions.*;

import org.w3c.dom.Node;

 

import java.math.BigInteger;

 

public class tableCreation

{

                public static void main(String args[]) throws Exception, Docx4JException, JAXBException

                {

                                try

                                {

                                                System.out.println("Creating Package");

                                                WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();

                                                MainDocumentPart mainDocumentPart = wordMLPackage.getMainDocumentPart();

 

                                                wordMLPackage.getMainDocumentPart()

                                                .addStyledParagraphOfText("Title", "Table creation ");

                                                wordMLPackage.getMainDocumentPart().addParagraphOfText("Paragraph before Table");

 

                                                int writableWidthTwips = wordMLPackage.getDocumentModel().getSections().get(0).getPageDimensions().getWritableWidthTwips();

                                                int rows=2;

                                                int cols=2;

                                                int cellWidthTwips = new Double(Math.floor( (writableWidthTwips/cols ))).intValue();

 

                                                //org.docx4j.wml.Tbl tbl = createTable(rows,cols,cellWidthTwips);

                                                org.docx4j.model.table.TableModel table = new org.docx4j.model.table.TableModel();

                                                wordMLPackage.getMainDocumentPart().addObject(table);

 

                                                 /*// Let's add a table

                                    int writableWidthTwips = wordMLPackage.getDocumentModel().getSections().get(0).getPageDimensions().getWritableWidthTwips();

                                    int cols = 4;

                                    int cellWidthTwips = new Double(Math.floor( (writableWidthTwips/cols ))).intValue();

 

                                    Tbl tbl = TblFactory.createTable(4, 5, cellWidthTwips);

                                    wordMLPackage.getMainDocumentPart().addObject(tbl);*/

 

                                                wordMLPackage.save(new java.io.File(System.getProperty("user.dir") + "/tableCreation.docx") );

 

                                                System.out.println("Done.");

                                }

                                catch(Exception e)

                                {

                                                e.printStackTrace();

 

                                }

                }

 

 

public static Tbl createTable(int rows, int cols, int cellWidthTwips) {

 

                                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>";

                                /*String strTblPr = "<w:tbl>"  + "<w:tblPr>"

                        + "<w:tblStyle w:val=\"TableGrid\"/>"

                        + "<w:tblW w:w=\"0\" w:type=\"auto\"/>"

                        + "<w:tblLook w:val=\"04A0\"/>"

                        + "</w:tblPr>"

                        +"<w:tblGrid>"

                        +"<w:gridCol w:w=\"4788\"/>"

                        +"<w:gridCol w:w=\"4788\"/>"

                        +"</w:tblGrid>"

                        +"<w:tr>"

                        +"<w:tc>"

                        +"<w:tcPr>"

                        +"<w:tcW w:w=\"4788\" w:type=\"dxa\"/>"

                        +"</w:tcPr>"

                        +"<w:p>"

                        +"<w:r>"

                        +"<w:t>R1c1</w:t>"

                        +"</w:r>"

                        +"</w:p>"

                        +"</w:tc>"

                        +"<w:tc>"

                        +"<w:tcPr>"

                        +"<w:tcW w:w=\"4788\" w:type=\"dxa\"/>"

                        +"</w:tcPr>"

                        +"<w:p>"

                        +"<w:r>"

                        +"<w:t>R1c2</w:t>"

                        +"</w:r>"

                        +"</w:p>"

                        +"</w:tc>"

                        +"</w:tr>"

                        +"<w:tr>"

                        +"<w:tc>"

                        +"<w:tcPr>"

                        +"<w:tcW w:w=\"4788\" w:type=\"dxa\"/>"

                        +"</w:tcPr>"

                        +"<w:p>"

                        +"<w:r>"

                        +"<w:t>R2c1</w:t>"

                        +"</w:r>"

                        +"</w:p>"

                        +"</w:tc>"

                        +"<w:tc>"

                        +"<w:tcPr>"

                        +"<w:tcW w:w=\"4788\" w:type=\"dxa\"/>"

                        +"</w:tcPr>"

                        +"<w:p>"

                        +"<w:r>"

                        +"<w:t>R2c2</w:t>"

                        +"</w:r>"

                        +"</w:p>"

                        +"</w:tc>"

                        +"</w:tr>"

                        +"</w:tbl>";*/

                                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);

                                                                // <w:tcW w:w="4788" w:type="dxa"/>

                                                                TblWidth cellWidth = Context.getWmlObjectFactory().createTblWidth();

                                                                tcPr.setTcW(cellWidth);

                                                                cellWidth.setType("dxa");

                                                                cellWidth.setW(BigInteger.valueOf(cellWidthTwips));

 

                                                                // Cell content - an empty <w:p/>

                                                /*           tc.getEGBlockLevelElts().add(

                                                                                                Context.getWmlObjectFactory().createP()

                                                                                                );*/

 

                                                                //Cell content - if not empty

                                                                Node foreign = cell.getContent();

                                                                Object o = XmlUtils.unmarshal(foreign.getChildNodes().item(n));

                                                                tc.getEGBlockLevelElts().add(o);

                                                }

 

                                }

                                return tbl;

                }

 

}