Search in sources :

Example 11 with Tbl

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

the class TableWithStyledContent method main.

/**
 *  跟前面的做的一样, 我们再一次创建了一个表格, 并添加了三个单元格, 其中有两个
 *  单元带有样式. 在新方法中我们传进表格行, 单元格内容, 是否为粗体及字体大小作
 *  为参数. 你需要注意, 因为the Office Open specification规范定义这个属性是半个
 *  点(half-point)大小, 因此字体大小需要是你想在Word中显示大小的两倍,
 */
public static void main(String[] args) throws Docx4JException {
    wordMLPackage = WordprocessingMLPackage.createPackage();
    factory = Context.getWmlObjectFactory();
    Tbl table = factory.createTbl();
    Tr tableRow = factory.createTr();
    addRegularTableCell(tableRow, "Normal text");
    addStyledTableCell(tableRow, "Bold text", true, null);
    addStyledTableCell(tableRow, "Bold large text", true, "40");
    table.getContent().add(tableRow);
    addBorders(table);
    wordMLPackage.getMainDocumentPart().addObject(table);
    wordMLPackage.save(new java.io.File("src/main/files/HelloWord6.docx"));
}
Also used : Tr(org.docx4j.wml.Tr) Tbl(org.docx4j.wml.Tbl)

Example 12 with Tbl

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

the class AddingATable method main.

public static void main(String[] args) throws Docx4JException {
    wordMLPackage = WordprocessingMLPackage.createPackage();
    factory = Context.getWmlObjectFactory();
    Tbl table = factory.createTbl();
    Tr tableRow = factory.createTr();
    addTableCell(tableRow, "Field 1");
    addTableCell(tableRow, "Field 2");
    table.getContent().add(tableRow);
    wordMLPackage.getMainDocumentPart().addObject(table);
    wordMLPackage.save(new java.io.File("src/main/files/HelloWord4.docx"));
}
Also used : Tr(org.docx4j.wml.Tr) Tbl(org.docx4j.wml.Tbl)

Example 13 with Tbl

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

the class AddingAnInlineImageToTable method main.

/**
 *  首先我们创建包和对象工厂, 因此在类的随处我们都可以使用它们. 然后我们创建一个表格并添加
 *  边框. 接下来我们创建一个表格行并在第一个域添加一些文本.
 *  对于第二个域, 我们用与前面一样的图片创建一个段落并添加进去. 最后把行添加到表格中, 并将
 *  表格添加到包中, 然后保存这个包.
 */
public static void main(String[] args) throws Exception {
    wordMLPackage = WordprocessingMLPackage.createPackage();
    factory = Context.getWmlObjectFactory();
    Tbl table = factory.createTbl();
    addBorders(table);
    Tr tr = factory.createTr();
    P paragraphOfText = wordMLPackage.getMainDocumentPart().createParagraphOfText("Field 1");
    addTableCell(tr, paragraphOfText);
    File file = new File("src/main/resources/iProfsLogo.png");
    P paragraphWithImage = addInlineImageToParagraph(createInlineImage(file));
    addTableCell(tr, paragraphWithImage);
    table.getContent().add(tr);
    wordMLPackage.getMainDocumentPart().addObject(table);
    wordMLPackage.save(new java.io.File("src/main/files/HelloWord8.docx"));
}
Also used : P(org.docx4j.wml.P) File(java.io.File) Tr(org.docx4j.wml.Tr) File(java.io.File) Tbl(org.docx4j.wml.Tbl)

Example 14 with Tbl

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

the class WMLPackageUtils method getTable.

/**
 * 该方法找到表格,获取第一行并且遍历提供的map向表格添加新行,在将其返回之前删除模版行。这个方法用到了两个助手方法:addRowToTable 和 getTemplateTable。我们首先看一下后面的那个:
 */
public static Tbl getTable(List<Tbl> tables, String placeholder) throws Docx4JException {
    for (Iterator<Tbl> iterator = tables.iterator(); iterator.hasNext(); ) {
        Tbl tbl = iterator.next();
        // 查找当前table下面的text对象
        List<Text> textElements = getTargetElements(tbl, Text.class);
        for (Text text : textElements) {
            Text textElement = (Text) text;
            // 
            if (textElement.getValue() != null && textElement.getValue().equals(placeholder)) {
                return (Tbl) tbl;
            }
        }
    }
    return null;
}
Also used : Text(org.docx4j.wml.Text) Tbl(org.docx4j.wml.Tbl)

Example 15 with Tbl

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

the class DocxBuilder method getDocxTable.

private static Tbl getDocxTable(WordprocessingMLPackage wPMLpackage, boolean isWordBased, int rows, int cols, List<HashMap<Integer, TrpTableCellType>> allRows, int tablesize, MainDocumentPart mdp) throws Exception {
    int writableWidthTwips = wPMLpackage.getDocumentModel().getSections().get(0).getPageDimensions().getWritableWidthTwips();
    int cellWidthTwips = new Double(Math.floor((writableWidthTwips / cols))).intValue();
    Tbl table = TblFactory.createTable(0, 0, cellWidthTwips);
    TblGrid tblGrid = factory.createTblGrid();
    for (int i = 0; i < cols; i++) {
        TblGridCol col = factory.createTblGridCol();
        col.setW(BigInteger.valueOf(cellWidthTwips));
        tblGrid.getGridCol().add(col);
    }
    table.setTblGrid(tblGrid);
    int i = 0;
    for (HashMap<Integer, TrpTableCellType> entry : allRows) {
        Tr row = factory.createTr();
        table.getContent().add(row);
        i++;
        int d = 0;
        if (entry.keySet().size() != cols) {
            logger.debug("size of entries does not match columns ");
        }
        for (Integer key : entry.keySet()) {
            Tc cell = factory.createTc();
            row.getContent().add(cell);
            String rowSpan = null;
            int colSpan = 1;
            boolean mergedVertical = false;
            int colsize = cellWidthTwips;
            if (entry.get(key) != null) {
                if (entry.get(key).getRowSpan() > 1) {
                    mergedVertical = true;
                    rowSpan = "restart";
                }
                // PageXmlUtils.buildPolygon(entry.get(key).getCoords().getPoints()).getBounds().getMaxX();
                double maxX = entry.get(key).getBoundingBox().getMaxX();
                // PageXmlUtils.buildPolygon(entry.get(key).getCoords().getPoints()).getBounds().getMinX();
                double minX = entry.get(key).getBoundingBox().getMinX();
                double colsizeRel = maxX - minX;
                double colsizetmp = colsizeRel / (double) tablesize;
                // logger.debug("colsizetmp " + colsizetmp);
                colsize = (int) (writableWidthTwips * colsizetmp);
                colSpan = entry.get(key).getColSpan();
                // logger.debug("colsize " + colsize);
                // logger.debug("text in this cell is " + entry.get(key).getUnicodeTextFromLines());
                int colID = entry.get(key).getCol();
                int rowID = entry.get(key).getRow();
            } else {
                // logger.debug("no cell for this column ");
                mergedVertical = true;
            }
            applyGridSpan(cell, colSpan, rowSpan, colsize, mergedVertical);
            P columnPara = factory.createP();
            // P columnPara = (P) column.getContent().get(0);
            cell.getContent().add(columnPara);
            d++;
            Text tx = factory.createText();
            R run = factory.createR();
            if (entry.get(key) != null) {
                // old solution till now: tx.setValue(entry.get(key).getUnicodeTextFromLines());
                if (entry.get(key).getUnicodeTextFromLines() != "") {
                    exportTextRegion(entry.get(key), isWordBased, columnPara, mdp);
                }
            }
            run.getContent().add(tx);
            columnPara.getContent().add(run);
        }
    }
    return table;
}
Also used : TrpTableCellType(eu.transkribus.core.model.beans.pagecontent_trp.TrpTableCellType) Text(org.docx4j.wml.Text) Tc(org.docx4j.wml.Tc) BigInteger(java.math.BigInteger) P(org.docx4j.wml.P) R(org.docx4j.wml.R) TblGrid(org.docx4j.wml.TblGrid) Tr(org.docx4j.wml.Tr) Tbl(org.docx4j.wml.Tbl) TblGridCol(org.docx4j.wml.TblGridCol)

Aggregations

Tbl (org.docx4j.wml.Tbl)36 Tr (org.docx4j.wml.Tr)23 P (org.docx4j.wml.P)10 Tc (org.docx4j.wml.Tc)9 Test (org.junit.Test)8 File (java.io.File)7 TblPr (org.docx4j.wml.TblPr)7 RPr (org.docx4j.wml.RPr)6 TblBorders (org.docx4j.wml.TblBorders)6 BigInteger (java.math.BigInteger)5 WordprocessingMLPackage (org.docx4j.openpackaging.packages.WordprocessingMLPackage)5 MainDocumentPart (org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart)5 R (org.docx4j.wml.R)5 TblWidth (org.docx4j.wml.TblWidth)5 TblStyle (org.docx4j.wml.CTTblPrBase.TblStyle)4 ObjectFactory (org.docx4j.wml.ObjectFactory)4 STHint (org.docx4j.wml.STHint)4 Text (org.docx4j.wml.Text)4 ArrayList (java.util.ArrayList)3 TrpTableCellType (eu.transkribus.core.model.beans.pagecontent_trp.TrpTableCellType)2