Search in sources :

Example 31 with P

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

the class Docx4j_工具类_S3_Test method removeTcContent.

/**
 * @Description:设置单元格内容,content为null则清除单元格内容
 */
public void removeTcContent(Tc tc) {
    List<Object> pList = tc.getContent();
    P p = null;
    if (pList != null && pList.size() > 0) {
        if (pList.get(0) instanceof P) {
            p = (P) pList.get(0);
        }
    } else {
        return;
    }
    List<Object> rList = p.getContent();
    if (rList != null && rList.size() > 0) {
        for (int i = 0, len = rList.size(); i < len; i++) {
            // 清除内容(所有的r
            p.getContent().remove(0);
        }
    }
}
Also used : P(org.docx4j.wml.P)

Example 32 with P

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

the class TableWithStyledContent method addStyling.

/**
 *  这里我们添加实际的样式信息, 首先创建一个段落, 然后创建以单元格内容作为值的文本对象;
 *  第三步, 创建一个被称为运行块的对象, 它是一块或多块拥有共同属性的文本的容器, 并将文本对象添加
 *  到其中. 随后我们将运行块R添加到段落内容中.
 *  直到现在我们所做的还没有添加任何样式, 为了达到目标, 我们创建运行块属性对象并给它添加各种样式.
 *  这些运行块的属性随后被添加到运行块. 最后段落被添加到表格的单元格中.
 */
private static 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)

Example 33 with P

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

the class AddingAnInlineImage method addImageToPackage.

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

Example 34 with P

use of org.docx4j.wml.P 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 35 with P

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

the class AddingPageNrToFooter method createFooterWithPageNr.

/**
 *  As in the previous example, we create a footer and a paragraph object. But
 *  this time, instead of adding text to a run, we add a field. And just as with
 *  the table of content, we have to add a begin and end character around the
 *  actual field with the page number. Finally we add the paragraph to the
 *  content of the footer and then return it.
 *
 * @return
 */
public static Ftr createFooterWithPageNr() {
    Ftr ftr = factory.createFtr();
    P paragraph = factory.createP();
    addFieldBegin(paragraph);
    addPageNumberField(paragraph);
    addFieldEnd(paragraph);
    ftr.getContent().add(paragraph);
    return ftr;
}
Also used : P(org.docx4j.wml.P) Ftr(org.docx4j.wml.Ftr)

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