Search in sources :

Example 6 with TblWidth

use of org.docx4j.wml.TblWidth in project Java-Tutorial by gpcodervn.

the class NestedTable method main.

public static void main(String[] args) throws Exception {
    System.out.println("Creating package..");
    wordMLPackage = WordprocessingMLPackage.createPackage();
    Tbl tblCredProg = factory.createTbl();
    TblPr tblPr = new TblPr();
    TblStyle tblStyle = new TblStyle();
    tblStyle.setVal("TableGrid");
    tblPr.setTblStyle(tblStyle);
    tblCredProg.setTblPr(tblPr);
    TblWidth width = new TblWidth();
    width.setType("auto");
    width.setW(new BigInteger("0"));
    tblPr.setTblW(width);
    // create row 1
    Tr tr = factory.createTr();
    // col 1 of row 1
    addTc(tr, "A");
    // col 2 of row 1
    addTc(tr, "B");
    tblCredProg.getEGContentRowContent().add(tr);
    // create row 2
    Tr tr2 = factory.createTr();
    TrPr pr = new TrPr();
    tr2.setTrPr(pr);
    // col 1 of row 2
    Tc tc1 = factory.createTc();
    TcPr tcPr = new TcPr();
    TblWidth widtha = new TblWidth();
    widtha.setType("auto");
    widtha.setW(new BigInteger("0"));
    tcPr.setTcW(widtha);
    tc1.setTcPr(tcPr);
    tc1.getEGBlockLevelElts().add(XmlUtils.unmarshalString(tblXML));
    // The following is important or you may get a corrupted docx file
    tc1.getEGBlockLevelElts().add(wordMLPackage.getMainDocumentPart().createParagraphOfText(""));
    tr2.getEGContentCellContent().add(tc1);
    tblCredProg.getEGContentRowContent().add(tr2);
    // col2 of row 2
    addTc(tr2, "C");
    wordMLPackage.getMainDocumentPart().addObject(tblCredProg);
    // Now save it
    wordMLPackage.save(new java.io.File("output/NestedTable.docx"));
    System.out.println("Done.");
}
Also used : TblWidth(org.docx4j.wml.TblWidth) TcPr(org.docx4j.wml.TcPr) TrPr(org.docx4j.wml.TrPr) BigInteger(java.math.BigInteger) TblStyle(org.docx4j.wml.CTTblPrBase.TblStyle) TblPr(org.docx4j.wml.TblPr) Tr(org.docx4j.wml.Tr) Tbl(org.docx4j.wml.Tbl) Tc(org.docx4j.wml.Tc)

Example 7 with TblWidth

use of org.docx4j.wml.TblWidth in project docx4j-template by vindell.

the class Docx4jStyle_S3 method setCellWidth.

public void setCellWidth(Tc tableCell, int width) {
    if (width > 0) {
        TcPr tableCellProperties = tableCell.getTcPr();
        if (tableCellProperties == null) {
            tableCellProperties = new TcPr();
            tableCell.setTcPr(tableCellProperties);
        }
        TblWidth tableWidth = new TblWidth();
        tableWidth.setType("dxa");
        tableWidth.setW(BigInteger.valueOf(width));
        tableCellProperties.setTcW(tableWidth);
    }
}
Also used : TblWidth(org.docx4j.wml.TblWidth) TcPr(org.docx4j.wml.TcPr)

Example 8 with TblWidth

use of org.docx4j.wml.TblWidth in project docx4j-template by vindell.

the class Docx4j_工具类_S3_Test method setTableWidth.

/**
 * @Description: 设置表格总宽度
 */
public void setTableWidth(Tbl tbl, String width) {
    if (StringUtils.isNotBlank(width)) {
        TblPr tblPr = getTblPr(tbl);
        TblWidth tblW = tblPr.getTblW();
        if (tblW == null) {
            tblW = new TblWidth();
            tblPr.setTblW(tblW);
        }
        tblW.setW(new BigInteger(width));
        tblW.setType("dxa");
    }
}
Also used : TblWidth(org.docx4j.wml.TblWidth) BigInteger(java.math.BigInteger) TblPr(org.docx4j.wml.TblPr)

Example 9 with TblWidth

use of org.docx4j.wml.TblWidth in project TranskribusCore by Transkribus.

the class DocxBuilder method applyGridSpan.

private static void applyGridSpan(final Tc cell, final int colSpan, final String rowSpan, int w, boolean mergedVertical) {
    TcPr tcPr = factory.createTcPr();
    TblWidth tblWidth = factory.createTblWidth();
    tblWidth.setType("dxa");
    tblWidth.setW(BigInteger.valueOf(w * colSpan));
    tcPr.setTcW(tblWidth);
    if (colSpan > 1) {
        GridSpan gridSpan = factory.createTcPrInnerGridSpan();
        gridSpan.setVal(BigInteger.valueOf(colSpan));
        tcPr.setGridSpan(gridSpan);
    }
    if (mergedVertical) {
        // logger.debug(" this is vertical span");
        VMerge gridVSpan = factory.createTcPrInnerVMerge();
        if (rowSpan != null)
            gridVSpan.setVal(rowSpan);
        tcPr.setVMerge(gridVSpan);
    }
    cell.setTcPr(tcPr);
}
Also used : TblWidth(org.docx4j.wml.TblWidth) TcPr(org.docx4j.wml.TcPr) GridSpan(org.docx4j.wml.TcPrInner.GridSpan) VMerge(org.docx4j.wml.TcPrInner.VMerge)

Example 10 with TblWidth

use of org.docx4j.wml.TblWidth in project mdw-designer by CenturyLinkCloud.

the class DocxBuilder method addTable.

public Tbl addTable(String[] headers, String[][] values, int fontSize, int indent) {
    ObjectFactory factory = Context.getWmlObjectFactory();
    int writableWidthTwips = wordMLPackage.getDocumentModel().getSections().get(0).getPageDimensions().getWritableWidthTwips();
    int cols = headers.length;
    int cellWidthTwips = new Double(Math.floor((writableWidthTwips / cols))).intValue();
    Tbl tbl = TblFactory.createTable(0, cols, cellWidthTwips);
    Tr thead = factory.createTr();
    for (int i = 0; i < headers.length; i++) {
        Tc tc = factory.createTc();
        tc.getContent().add(createParagraph(headers[i], fontSize, true));
        thead.getContent().add(tc);
    }
    tbl.getContent().add(0, thead);
    for (int i = 0; i < values[0].length; i++) {
        Tr tr = factory.createTr();
        for (int j = 0; j < headers.length; j++) {
            Tc tc = factory.createTc();
            tc.getContent().add(createParagraph(values[j][i], fontSize, false));
            tr.getContent().add(tc);
        }
        tbl.getContent().add(i + 1, tr);
    }
    getMdp().addObject(tbl);
    if (indent != 0) {
        TblPr tblPr = tbl.getTblPr();
        TblWidth tblIndent = factory.createTblWidth();
        tblIndent.setType("dxa");
        tblIndent.setW(BigInteger.valueOf(indent));
        tblPr.setTblInd(tblIndent);
    }
    return tbl;
}
Also used : ObjectFactory(org.docx4j.wml.ObjectFactory) TblWidth(org.docx4j.wml.TblWidth) TblPr(org.docx4j.wml.TblPr) Tr(org.docx4j.wml.Tr) Tbl(org.docx4j.wml.Tbl) Tc(org.docx4j.wml.Tc)

Aggregations

TblWidth (org.docx4j.wml.TblWidth)20 TcPr (org.docx4j.wml.TcPr)14 BigInteger (java.math.BigInteger)10 TblPr (org.docx4j.wml.TblPr)8 Tc (org.docx4j.wml.Tc)7 Tbl (org.docx4j.wml.Tbl)5 Tr (org.docx4j.wml.Tr)4 TblGrid (org.docx4j.wml.TblGrid)3 TblGridCol (org.docx4j.wml.TblGridCol)3 TblStyle (org.docx4j.wml.CTTblPrBase.TblStyle)2 STHint (org.docx4j.wml.STHint)2 TcMar (org.docx4j.wml.TcMar)2 GridSpan (org.docx4j.wml.TcPrInner.GridSpan)2 VMerge (org.docx4j.wml.TcPrInner.VMerge)2 TrPr (org.docx4j.wml.TrPr)2 File (java.io.File)1 CTTblCellMar (org.docx4j.wml.CTTblCellMar)1 CTVerticalJc (org.docx4j.wml.CTVerticalJc)1 Jc (org.docx4j.wml.Jc)1 ObjectFactory (org.docx4j.wml.ObjectFactory)1