Search in sources :

Example 1 with TblStyle

use of org.docx4j.wml.CTTblPrBase.TblStyle 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 2 with TblStyle

use of org.docx4j.wml.CTTblPrBase.TblStyle 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 3 with TblStyle

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

the class Docx4jUtils method createTableWithContent.

/**
 * Our createTableWithContent method follows (we use dxa values). There are 4
 * rows, some cells with vertical merge, and some with horizontal merge.
 */
private Tbl createTableWithContent() {
    Tbl table = factory.createTbl();
    // for TEST: this adds borders to all cells
    TblPr tblPr = new TblPr();
    TblStyle tblStyle = new TblStyle();
    tblStyle.setVal("TableGrid");
    tblPr.setTblStyle(tblStyle);
    table.setTblPr(tblPr);
    Tr tableRow = factory.createTr();
    // a default table cell style
    DocxStyle defStyle = new DocxStyle();
    defStyle.setBold(false);
    defStyle.setItalic(false);
    defStyle.setUnderline(false);
    defStyle.setHorizAlignment(JcEnumeration.CENTER);
    // a specific table cell style
    DocxStyle style = new DocxStyle();
    style.setBold(true);
    style.setItalic(true);
    style.setUnderline(true);
    style.setFontSize("40");
    style.setFontColor("FF0000");
    style.setFontFamily("Book Antiqua");
    style.setTop(300);
    style.setBackground("CCFFCC");
    style.setVerticalAlignment(STVerticalJc.CENTER);
    style.setHorizAlignment(JcEnumeration.CENTER);
    style.setBorderTop(true);
    style.setBorderBottom(true);
    style.setNoWrap(true);
    addTableCell(tableRow, "Field 1", 3500, style, 1, null);
    // start vertical merge for Filed 2 and Field 3 on 3 rows
    addTableCell(tableRow, "Field 2", 3500, defStyle, 1, "restart");
    addTableCell(tableRow, "Field 3", 1500, defStyle, 1, "restart");
    table.getContent().add(tableRow);
    tableRow = factory.createTr();
    addTableCell(tableRow, "Text", 3500, defStyle, 1, null);
    addTableCell(tableRow, "", 3500, defStyle, 1, "");
    addTableCell(tableRow, "", 1500, defStyle, 1, "");
    table.getContent().add(tableRow);
    tableRow = factory.createTr();
    addTableCell(tableRow, "Interval", 3500, defStyle, 1, null);
    addTableCell(tableRow, "", 3500, defStyle, 1, "close");
    addTableCell(tableRow, "", 1500, defStyle, 1, "close");
    table.getContent().add(tableRow);
    // add an image horizontally merged on 3 cells
    String filenameHint = null;
    String altText = null;
    int id1 = 0;
    int id2 = 1;
    byte[] bytes;
    try {
        bytes = getImageBytes(new File("/resources/image.png"));
        P pImage = newImage(wordMLPackage, bytes, filenameHint, altText, id1, id2, 8500);
        tableRow = factory.createTr();
        addTableCell(tableRow, pImage, 8500, defStyle, 3, null);
        table.getContent().add(tableRow);
    } catch (FileNotFoundException e1) {
        e1.printStackTrace();
    } catch (IOException e1) {
        e1.printStackTrace();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return table;
}
Also used : FileNotFoundException(java.io.FileNotFoundException) TblStyle(org.docx4j.wml.CTTblPrBase.TblStyle) IOException(java.io.IOException) TblPr(org.docx4j.wml.TblPr) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Docx4JException(org.docx4j.openpackaging.exceptions.Docx4JException) P(org.docx4j.wml.P) File(java.io.File) Tr(org.docx4j.wml.Tr) Tbl(org.docx4j.wml.Tbl)

Example 4 with TblStyle

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

the class Docx4jStyle_S3 method createTableWithContent.

public Tbl createTableWithContent(WordprocessingMLPackage wordMLPackage, ObjectFactory factory, String imgFilePath) throws Exception {
    Tbl table = factory.createTbl();
    // for TEST: this adds borders to all cells
    TblPr tblPr = new TblPr();
    TblStyle tblStyle = new TblStyle();
    tblStyle.setVal("TableGrid");
    tblPr.setTblStyle(tblStyle);
    table.setTblPr(tblPr);
    Tr tableRow = factory.createTr();
    // a default table cell style
    Docx4jStyle_S3 defStyle = new Docx4jStyle_S3();
    defStyle.setBold(false);
    defStyle.setItalic(false);
    defStyle.setUnderline(false);
    defStyle.setHorizAlignment(JcEnumeration.CENTER);
    // a specific table cell style
    Docx4jStyle_S3 style = new Docx4jStyle_S3();
    style.setBold(true);
    style.setItalic(true);
    style.setUnderline(true);
    style.setFontSize("40");
    style.setFontColor("FF0000");
    style.setCnFontFamily("微软雅黑");
    style.setEnFontFamily("Times New Roman");
    style.setTop(300);
    style.setBackground("CCFFCC");
    style.setVerticalAlignment(STVerticalJc.CENTER);
    style.setHorizAlignment(JcEnumeration.CENTER);
    style.setBorderTop(true);
    style.setBorderBottom(true);
    style.setNoWrap(true);
    addTableCell(factory, tableRow, "测试Field 1", 3500, style, 1, null);
    // start vertical merge for Filed 2 and Field 3 on 3 rows
    addTableCell(factory, tableRow, "测试Field 2", 3500, defStyle, 1, "restart");
    addTableCell(factory, tableRow, "测试Field 3", 1500, defStyle, 1, "restart");
    table.getContent().add(tableRow);
    tableRow = factory.createTr();
    addTableCell(factory, tableRow, "Text", 3500, defStyle, 1, null);
    addTableCell(factory, tableRow, "", 3500, defStyle, 1, "");
    addTableCell(factory, tableRow, "", 1500, defStyle, 1, "");
    table.getContent().add(tableRow);
    tableRow = factory.createTr();
    addTableCell(factory, tableRow, "Interval", 3500, defStyle, 1, null);
    addTableCell(factory, tableRow, "", 3500, defStyle, 1, "close");
    addTableCell(factory, tableRow, "", 1500, defStyle, 1, "close");
    table.getContent().add(tableRow);
    // add an image horizontally merged on 3 cells
    String filenameHint = null;
    String altText = null;
    int id1 = 0;
    int id2 = 1;
    byte[] bytes = getImageBytes(imgFilePath);
    P pImage;
    try {
        pImage = newImage(wordMLPackage, factory, bytes, filenameHint, altText, id1, id2, 8500);
        tableRow = factory.createTr();
        addTableCell(factory, tableRow, pImage, 8500, defStyle, 3, null);
        table.getContent().add(tableRow);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return table;
}
Also used : P(org.docx4j.wml.P) TblStyle(org.docx4j.wml.CTTblPrBase.TblStyle) TblPr(org.docx4j.wml.TblPr) Tr(org.docx4j.wml.Tr) Tbl(org.docx4j.wml.Tbl)

Aggregations

TblStyle (org.docx4j.wml.CTTblPrBase.TblStyle)4 Tbl (org.docx4j.wml.Tbl)4 TblPr (org.docx4j.wml.TblPr)4 Tr (org.docx4j.wml.Tr)4 File (java.io.File)2 BigInteger (java.math.BigInteger)2 P (org.docx4j.wml.P)2 TblWidth (org.docx4j.wml.TblWidth)2 Tc (org.docx4j.wml.Tc)2 TcPr (org.docx4j.wml.TcPr)2 TrPr (org.docx4j.wml.TrPr)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 Docx4JException (org.docx4j.openpackaging.exceptions.Docx4JException)1 GridSpan (org.docx4j.wml.TcPrInner.GridSpan)1