Search in sources :

Example 91 with P

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

the class Docx4j_工具类_S3_Test method setTcHidden.

/**
 * @Description: 隐藏单元格内容
 */
public void setTcHidden(Tc tc, boolean hidden) {
    List<P> pList = getTcAllP(tc);
    for (P p : pList) {
        PPr ppr = getPPr(p);
        List<Object> objRList = getAllElementFromObject(p, R.class);
        if (objRList == null) {
            continue;
        }
        for (Object objR : objRList) {
            if (objR instanceof R) {
                R r = (R) objR;
                RPr rpr = getRPr(r);
                setRPrVanishStyle(rpr, hidden);
            }
        }
        setParaVanish(ppr, hidden);
    }
}
Also used : P(org.docx4j.wml.P) R(org.docx4j.wml.R) PPr(org.docx4j.wml.PPr) RPr(org.docx4j.wml.RPr) ParaRPr(org.docx4j.wml.ParaRPr)

Example 92 with P

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

the class Docx4j_工具类_S3_Test method createTable.

/**
 * @Description:创建表格(默认水平居中,垂直居中)
 */
public Tbl createTable(int rowNum, int colsNum, int[] widthArr) throws Exception {
    colsNum = Math.max(1, Math.min(colsNum, widthArr.length));
    rowNum = Math.max(1, rowNum);
    Tbl tbl = new Tbl();
    StringBuffer tblSb = new StringBuffer();
    tblSb.append("<w:tblPr ").append(Namespaces.W_NAMESPACE_DECLARATION).append(">");
    tblSb.append("<w:tblStyle w:val=\"TableGrid\"/>");
    tblSb.append("<w:tblW w:w=\"0\" w:type=\"auto\"/>");
    // 上边框
    tblSb.append("<w:tblBorders>");
    tblSb.append("<w:top w:val=\"single\" w:sz=\"1\" w:space=\"0\" w:color=\"auto\"/>");
    // 左边框
    tblSb.append("<w:left w:val=\"single\" w:sz=\"1\" w:space=\"0\" w:color=\"auto\"/>");
    // 下边框
    tblSb.append("<w:bottom w:val=\"single\" w:sz=\"1\" w:space=\"0\" w:color=\"auto\"/>");
    // 右边框
    tblSb.append("<w:right w:val=\"single\" w:sz=\"1\" w:space=\"0\" w:color=\"auto\"/>");
    tblSb.append("<w:insideH w:val=\"single\" w:sz=\"1\" w:space=\"0\" w:color=\"auto\"/>");
    tblSb.append("<w:insideV w:val=\"single\" w:sz=\"1\" w:space=\"0\" w:color=\"auto\"/>");
    tblSb.append("</w:tblBorders>");
    tblSb.append("</w:tblPr>");
    TblPr tblPr = null;
    tblPr = (TblPr) XmlUtils.unmarshalString(tblSb.toString());
    Jc jc = new Jc();
    // 单元格居中对齐
    jc.setVal(JcEnumeration.CENTER);
    tblPr.setJc(jc);
    tbl.setTblPr(tblPr);
    // 设定各单元格宽度
    TblGrid tblGrid = new TblGrid();
    tbl.setTblGrid(tblGrid);
    for (int i = 0; i < colsNum; i++) {
        TblGridCol gridCol = new TblGridCol();
        gridCol.setW(BigInteger.valueOf(widthArr[i]));
        tblGrid.getGridCol().add(gridCol);
    }
    // 新增行
    for (int j = 0; j < rowNum; j++) {
        Tr tr = new Tr();
        tbl.getContent().add(tr);
        // 列
        for (int i = 0; i < colsNum; i++) {
            Tc tc = new Tc();
            tr.getContent().add(tc);
            TcPr tcPr = new TcPr();
            TblWidth cellWidth = new TblWidth();
            cellWidth.setType("dxa");
            cellWidth.setW(BigInteger.valueOf(widthArr[i]));
            tcPr.setTcW(cellWidth);
            tc.setTcPr(tcPr);
            // 垂直居中
            setTcVAlign(tc, STVerticalJc.CENTER);
            P p = new P();
            PPr pPr = new PPr();
            pPr.setJc(jc);
            p.setPPr(pPr);
            R run = new R();
            p.getContent().add(run);
            tc.getContent().add(p);
        }
    }
    return tbl;
}
Also used : TblWidth(org.docx4j.wml.TblWidth) TblPr(org.docx4j.wml.TblPr) Tc(org.docx4j.wml.Tc) P(org.docx4j.wml.P) R(org.docx4j.wml.R) PPr(org.docx4j.wml.PPr) TcPr(org.docx4j.wml.TcPr) STVerticalJc(org.docx4j.wml.STVerticalJc) Jc(org.docx4j.wml.Jc) CTVerticalJc(org.docx4j.wml.CTVerticalJc) TblGrid(org.docx4j.wml.TblGrid) Tr(org.docx4j.wml.Tr) Tbl(org.docx4j.wml.Tbl) TblGridCol(org.docx4j.wml.TblGridCol)

Example 93 with P

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

the class Docx4j_工具类_S3_Test method addTrByIndex.

/**
 * @Description: 在表格指定位置新增一行(默认按表格定义的列数添加)
 */
public void addTrByIndex(Tbl tbl, int index, STVerticalJc vAlign, JcEnumeration hAlign) {
    TblGrid tblGrid = tbl.getTblGrid();
    Tr tr = new Tr();
    if (tblGrid != null) {
        List<TblGridCol> gridList = tblGrid.getGridCol();
        for (TblGridCol tblGridCol : gridList) {
            Tc tc = new Tc();
            setTcWidth(tc, tblGridCol.getW().toString());
            if (vAlign != null) {
                // 垂直居中
                setTcVAlign(tc, vAlign);
            }
            P p = new P();
            if (hAlign != null) {
                PPr pPr = new PPr();
                Jc jc = new Jc();
                // 单元格居中对齐
                jc.setVal(hAlign);
                pPr.setJc(jc);
                p.setPPr(pPr);
            }
            R run = new R();
            p.getContent().add(run);
            tc.getContent().add(p);
            tr.getContent().add(tc);
        }
    } else {
        // 大部分情况都不会走到这一步
        Tr firstTr = getTblAllTr(tbl).get(0);
        int cellSize = getTcCellSizeWithMergeNum(firstTr);
        for (int i = 0; i < cellSize; i++) {
            Tc tc = new Tc();
            if (vAlign != null) {
                // 垂直居中
                setTcVAlign(tc, vAlign);
            }
            P p = new P();
            if (hAlign != null) {
                PPr pPr = new PPr();
                Jc jc = new Jc();
                // 单元格居中对齐
                jc.setVal(hAlign);
                pPr.setJc(jc);
                p.setPPr(pPr);
            }
            R run = new R();
            p.getContent().add(run);
            tc.getContent().add(p);
            tr.getContent().add(tc);
        }
    }
    if (index >= 0 && index < tbl.getContent().size()) {
        tbl.getContent().add(index, tr);
    } else {
        tbl.getContent().add(tr);
    }
}
Also used : P(org.docx4j.wml.P) R(org.docx4j.wml.R) PPr(org.docx4j.wml.PPr) STVerticalJc(org.docx4j.wml.STVerticalJc) Jc(org.docx4j.wml.Jc) CTVerticalJc(org.docx4j.wml.CTVerticalJc) TblGrid(org.docx4j.wml.TblGrid) Tr(org.docx4j.wml.Tr) TblGridCol(org.docx4j.wml.TblGridCol) Tc(org.docx4j.wml.Tc)

Example 94 with P

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

the class WordprocessingMLPackageRender method addImageToPackage.

/**
 *  Docx4j拥有一个由字节数组创建图片部件的工具方法, 随后将其添加到给定的包中. 为了能将图片添加
 *  到一个段落中, 我们需要将图片转换成内联对象. 这也有一个方法, 方法需要文件名提示, 替换文本,
 *  两个id标识符和一个是嵌入还是链接到的指示作为参数.
 *  一个id用于文档中绘图对象不可见的属性, 另一个id用于图片本身不可见的绘制属性. 最后我们将内联
 *  对象添加到段落中并将段落添加到包的主文档部件.
 *
 *  @param wordMLPackage 要添加图片的包
 *  @param bytes         图片对应的字节数组
 *  @throws Exception    不幸的createImageInline方法抛出一个异常(没有更多具体的异常类型)
 */
public void addImageToPackage(byte[] bytes) throws Exception {
    BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wmlPackage, bytes);
    int docPrId = 1;
    int cNvPrId = 2;
    Inline inline = imagePart.createImageInline("Filename hint", "Alternative text", docPrId, cNvPrId, false);
    P paragraph = WmlElementUtils.addInlineImageToParagraph(inline);
    wmlPackage.getMainDocumentPart().addObject(paragraph);
}
Also used : P(org.docx4j.wml.P) Inline(org.docx4j.dml.wordprocessingDrawing.Inline) BinaryPartAbstractImage(org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage)

Example 95 with P

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

the class WordprocessingMLPackageRender method addStyling.

/**
 *  这里我们添加实际的样式信息, 首先创建一个段落, 然后创建以单元格内容作为值的文本对象;
 *  第三步, 创建一个被称为运行块的对象, 它是一块或多块拥有共同属性的文本的容器, 并将文本对象添加
 *  到其中. 随后我们将运行块R添加到段落内容中.
 *  直到现在我们所做的还没有添加任何样式, 为了达到目标, 我们创建运行块属性对象并给它添加各种样式.
 *  这些运行块的属性随后被添加到运行块. 最后段落被添加到表格的单元格中.
 */
public void addStyling(Tc tableCell, String content, boolean bold, String fontSize) {
    P paragraph = factory.createP();
    Text text = factory.createText();
    text.setValue(content);
    R run = factory.createR();
    run.getContent().add(text);
    paragraph.getContent().add(run);
    RPr runProperties = factory.createRPr();
    if (bold) {
        addBoldStyle(runProperties);
    }
    if (fontSize != null && !fontSize.isEmpty()) {
        setFontSize(runProperties, fontSize);
    }
    run.setRPr(runProperties);
    tableCell.getContent().add(paragraph);
}
Also used : P(org.docx4j.wml.P) R(org.docx4j.wml.R) RPr(org.docx4j.wml.RPr) Text(org.docx4j.wml.Text)

Aggregations

P (org.docx4j.wml.P)101 R (org.docx4j.wml.R)58 Text (org.docx4j.wml.Text)32 Test (org.junit.Test)23 PPr (org.docx4j.wml.PPr)22 RPr (org.docx4j.wml.RPr)21 ObjectFactory (org.docx4j.wml.ObjectFactory)18 Jc (org.docx4j.wml.Jc)16 BigInteger (java.math.BigInteger)14 CTVerticalJc (org.docx4j.wml.CTVerticalJc)14 Drawing (org.docx4j.wml.Drawing)14 MainDocumentPart (org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart)13 STVerticalJc (org.docx4j.wml.STVerticalJc)11 Tc (org.docx4j.wml.Tc)11 File (java.io.File)10 Inline (org.docx4j.dml.wordprocessingDrawing.Inline)10 BinaryPartAbstractImage (org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage)10 Tbl (org.docx4j.wml.Tbl)10 Tr (org.docx4j.wml.Tr)10 Br (org.docx4j.wml.Br)9