Search in sources :

Example 1 with Tc

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

the class ComplexTable method addTcVMerge.

private static void addTcVMerge(Tr tr, String label, String text, String vMergeVal, String width) {
    Tc tc = factory.createTc();
    TcPr tcPr = new TcPr();
    TblWidth tblwidth = new TblWidth();
    tblwidth.setType("dxa");
    tblwidth.setW(new BigInteger(width));
    tcPr.setTcW(tblwidth);
    VMerge merge = new VMerge();
    if (vMergeVal != null) {
        merge.setVal(vMergeVal);
    }
    tcPr.setVMerge(merge);
    tc.setTcPr(tcPr);
    if (text != null) {
        tc.getEGBlockLevelElts().add(wordMLPackage.getMainDocumentPart().createParagraphOfText(text));
    }
    tr.getEGContentCellContent().add(tc);
}
Also used : TblWidth(org.docx4j.wml.TblWidth) TcPr(org.docx4j.wml.TcPr) BigInteger(java.math.BigInteger) Tc(org.docx4j.wml.Tc) VMerge(org.docx4j.wml.TcPrInner.VMerge)

Example 2 with Tc

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

the class ComplexTable method main.

public static void main(String[] args) throws Exception {
    System.out.println("Creating package..");
    wordMLPackage = WordprocessingMLPackage.createPackage();
    Tbl mainTable = factory.createTbl();
    TblPr tblPr = new TblPr();
    TblStyle tblStyle = new TblStyle();
    tblStyle.setVal("TableGrid");
    tblPr.setTblStyle(tblStyle);
    mainTable.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, "Ab", "Ab", "3192");
    // col 2 of row 1
    addTc(tr, "Ac", "Ac", "3192");
    // a vertically merged col
    addTcVMerge(tr, "test", "Vertical Merge", "restart", "9000");
    mainTable.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("dxa");
    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);
    mainTable.getEGContentRowContent().add(tr2);
    // col2 of row 2
    addTc(tr2, "bA", "bA", "3192");
    // end vertically merged col
    addTcVMerge(tr2, "", "", null, "9000");
    // create row 3
    Tr tr3 = factory.createTr();
    TrPr pr3 = new TrPr();
    tr3.setTrPr(pr3);
    Tc tc31 = factory.createTc();
    TcPr tcPr3 = new TcPr();
    TblWidth width31 = new TblWidth();
    width31.setType("dxa");
    width31.setW(new BigInteger("0"));
    tcPr3.setTcW(width31);
    // for setting colspan of 3
    GridSpan gridSpan = new GridSpan();
    gridSpan.setVal(new BigInteger("3"));
    tcPr3.setGridSpan(gridSpan);
    tc31.setTcPr(tcPr3);
    tc31.getEGBlockLevelElts().add(wordMLPackage.getMainDocumentPart().createParagraphOfText("Horizontal merge 3 col"));
    tr3.getEGContentCellContent().add(tc31);
    mainTable.getEGContentRowContent().add(tr3);
    // create row 4 -6384
    Tr tr4 = factory.createTr();
    TrPr pr4 = new TrPr();
    tr4.setTrPr(pr4);
    Tc tc41 = factory.createTc();
    TcPr tcPr4 = new TcPr();
    TblWidth width41 = new TblWidth();
    width41.setType("dxa");
    width41.setW(new BigInteger("0"));
    tcPr4.setTcW(width31);
    // for setting colspan of 3
    GridSpan gridSpan41 = new GridSpan();
    gridSpan41.setVal(new BigInteger("2"));
    tcPr4.setGridSpan(gridSpan41);
    tc41.setTcPr(tcPr4);
    tc41.getEGBlockLevelElts().add(wordMLPackage.getMainDocumentPart().createParagraphOfText("Horizontal merge 2 col"));
    tr4.getEGContentCellContent().add(tc41);
    // Add an image to the last col
    File file = new File("resources/image.png");
    java.io.InputStream is = new java.io.FileInputStream(file);
    long length = file.length();
    // It needs to be an int type.
    if (length > Integer.MAX_VALUE) {
        System.out.println("File too large!!");
    }
    byte[] bytes = new byte[(int) length];
    int offset = 0;
    int numRead = 0;
    while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
        offset += numRead;
    }
    // Ensure all the bytes have been read in
    if (offset < bytes.length) {
        System.out.println("Could not completely read file " + file.getName());
    }
    is.close();
    String filenameHint = null;
    String altText = null;
    int id1 = 0;
    int id2 = 1;
    org.docx4j.wml.P p2 = newImage(wordMLPackage, bytes, filenameHint, altText, id1, id2, 3000);
    Tc tc42 = factory.createTc();
    tc42.getEGBlockLevelElts().add(p2);
    tr4.getEGContentCellContent().add(tc42);
    mainTable.getEGContentRowContent().add(tr4);
    wordMLPackage.getMainDocumentPart().addObject(mainTable);
    // Now save it
    wordMLPackage.save(new java.io.File("output/ComplexTable.docx"));
    System.out.println("Done.");
}
Also used : TblWidth(org.docx4j.wml.TblWidth) TrPr(org.docx4j.wml.TrPr) TblStyle(org.docx4j.wml.CTTblPrBase.TblStyle) TblPr(org.docx4j.wml.TblPr) File(java.io.File) Tc(org.docx4j.wml.Tc) TcPr(org.docx4j.wml.TcPr) BigInteger(java.math.BigInteger) File(java.io.File) Tr(org.docx4j.wml.Tr) Tbl(org.docx4j.wml.Tbl) GridSpan(org.docx4j.wml.TcPrInner.GridSpan)

Example 3 with Tc

use of org.docx4j.wml.Tc 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 4 with Tc

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

the class Docx4J_例子2 method createNormalCell.

public void createNormalCell(ObjectFactory factory, Tr tr, String content, RPr rpr, JcEnumeration jcEnumeration, boolean hasBgColor, String backgroudColor, boolean isHMerger, String mergeVal) {
    Tc tableCell = factory.createTc();
    P p = factory.createP();
    setParagraphSpacing(factory, p, jcEnumeration, true, "0", "0", null, null, true, "240", STLineSpacingRule.AUTO);
    Text t = factory.createText();
    t.setValue(content);
    R run = factory.createR();
    // 设置表格内容字体样式
    run.setRPr(rpr);
    TcPr tcPr = tableCell.getTcPr();
    if (tcPr == null) {
        tcPr = factory.createTcPr();
    }
    CTVerticalJc valign = factory.createCTVerticalJc();
    valign.setVal(STVerticalJc.CENTER);
    tcPr.setVAlign(valign);
    if (isHMerger) {
        HMerge merge = new HMerge();
        if (mergeVal != null) {
            merge.setVal(mergeVal);
        }
        tcPr.setHMerge(merge);
    } else {
        VMerge merge = new VMerge();
        if (mergeVal != null) {
            merge.setVal(mergeVal);
        }
        tcPr.setVMerge(merge);
    }
    run.getContent().add(t);
    p.getContent().add(run);
    tableCell.getContent().add(p);
    if (hasBgColor) {
        CTShd shd = tcPr.getShd();
        if (shd == null) {
            shd = factory.createCTShd();
        }
        shd.setColor("auto");
        shd.setFill(backgroudColor);
        tcPr.setShd(shd);
    }
    tableCell.setTcPr(tcPr);
    tr.getContent().add(tableCell);
}
Also used : P(org.docx4j.wml.P) R(org.docx4j.wml.R) TcPr(org.docx4j.wml.TcPr) HMerge(org.docx4j.wml.TcPrInner.HMerge) CTShd(org.docx4j.wml.CTShd) Text(org.docx4j.wml.Text) CTVerticalJc(org.docx4j.wml.CTVerticalJc) Tc(org.docx4j.wml.Tc) VMerge(org.docx4j.wml.TcPrInner.VMerge)

Example 5 with Tc

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

the class Docx4jStyle_S3 method addTableCell.

public void addTableCell(ObjectFactory factory, Tr tableRow, P image, int width, Docx4jStyle_S3 style, int horizontalMergedCells, String verticalMergedVal) {
    Tc tableCell = factory.createTc();
    addImageCellStyle(tableCell, image, style);
    setCellWidth(tableCell, width);
    setCellVMerge(tableCell, verticalMergedVal);
    setCellHMerge(tableCell, horizontalMergedCells);
    tableRow.getContent().add(tableCell);
}
Also used : Tc(org.docx4j.wml.Tc)

Aggregations

Tc (org.docx4j.wml.Tc)53 TcPr (org.docx4j.wml.TcPr)22 Tr (org.docx4j.wml.Tr)17 P (org.docx4j.wml.P)11 R (org.docx4j.wml.R)10 Tbl (org.docx4j.wml.Tbl)9 BigInteger (java.math.BigInteger)7 TblWidth (org.docx4j.wml.TblWidth)7 VMerge (org.docx4j.wml.TcPrInner.VMerge)7 Text (org.docx4j.wml.Text)7 GridSpan (org.docx4j.wml.TcPrInner.GridSpan)6 CTVerticalJc (org.docx4j.wml.CTVerticalJc)5 TblPr (org.docx4j.wml.TblPr)5 ArrayList (java.util.ArrayList)4 CTShd (org.docx4j.wml.CTShd)4 File (java.io.File)3 WordprocessingMLPackage (org.docx4j.openpackaging.packages.WordprocessingMLPackage)3 MainDocumentPart (org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart)3 ObjectFactory (org.docx4j.wml.ObjectFactory)3 PPr (org.docx4j.wml.PPr)3