Search in sources :

Example 86 with P

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

the class Docx4j_Helper method testDocx4jSetPageSize.

public void testDocx4jSetPageSize() throws Exception {
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
    MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();
    String titleStr = "静夜思    李白";
    String str = "床前明月光,疑似地上霜。";
    String str2 = "举头望明月,低头思故乡。";
    P p = Docx4j_Helper.factory.createP();
    String rprStr = "<w:rPr xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\"><w:rFonts w:hint=\"eastAsia\" w:ascii=\"Times New Roman\" w:hAnsi=\"Times New Roman\" w:eastAsia=\"宋体\"/><w:b/><w:color w:val=\"333333\"/><w:sz w:val=\"32\"/><w:szCs w:val=\"32\"/></w:rPr>";
    RPr rpr = (RPr) XmlUtils.unmarshalString(rprStr);
    setParagraphContent(p, rpr, titleStr);
    mdp.addObject(p);
    p = Docx4j_Helper.factory.createP();
    setParagraphContent(p, rpr, str);
    mdp.addObject(p);
    p = Docx4j_Helper.factory.createP();
    PPr pPr = Docx4j_Helper.factory.createPPr();
    // 设置文字方向
    SectPr sectPr = Docx4j_Helper.factory.createSectPr();
    TextDirection textDirect = Docx4j_Helper.factory.createTextDirection();
    // 文字方向:垂直方向从右往左
    textDirect.setVal("tbRl");
    sectPr.setTextDirection(textDirect);
    Type sectType = Docx4j_Helper.factory.createSectPrType();
    // 下一页
    sectType.setVal("nextPage");
    sectPr.setType(sectType);
    // 设置页面大小
    PgSz pgSz = Docx4j_Helper.factory.createSectPrPgSz();
    pgSz.setW(new BigInteger("8335"));
    pgSz.setH(new BigInteger("11850"));
    sectPr.setPgSz(pgSz);
    pPr.setSectPr(sectPr);
    p.setPPr(pPr);
    setParagraphContent(p, rpr, str2);
    mdp.addObject(p);
    p = createParagraphWithHAlign();
    setParagraphContent(p, rpr, titleStr);
    mdp.addObject(p);
    p = createParagraphWithHAlign();
    setParagraphContent(p, rpr, str);
    mdp.addObject(p);
    p = createParagraphWithHAlign();
    setParagraphContent(p, rpr, str2);
    mdp.addObject(p);
// Docx4j_Helper.saveWordPackage(wordMLPackage, outputfilepath);
}
Also used : P(org.docx4j.wml.P) Type(org.docx4j.wml.SectPr.Type) PPr(org.docx4j.wml.PPr) TextDirection(org.docx4j.wml.TextDirection) RPr(org.docx4j.wml.RPr) SectPr(org.docx4j.wml.SectPr) MainDocumentPart(org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart) BigInteger(java.math.BigInteger) PgSz(org.docx4j.wml.SectPr.PgSz) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage)

Example 87 with P

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

the class Docx4jStyle_S3 method newImage.

public P newImage(WordprocessingMLPackage wordMLPackage, ObjectFactory factory, byte[] bytes, String filenameHint, String altText, int id1, int id2, long cx) throws Exception {
    BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, bytes);
    Inline inline = imagePart.createImageInline(filenameHint, altText, id1, id2, cx, false);
    // Now add the inline in w:p/w:r/w:drawing
    P p = factory.createP();
    R run = factory.createR();
    p.getContent().add(run);
    Drawing drawing = factory.createDrawing();
    run.getContent().add(drawing);
    drawing.getAnchorOrInline().add(inline);
    return p;
}
Also used : P(org.docx4j.wml.P) Drawing(org.docx4j.wml.Drawing) R(org.docx4j.wml.R) Inline(org.docx4j.dml.wordprocessingDrawing.Inline) BinaryPartAbstractImage(org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage)

Example 88 with P

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

Example 89 with P

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

the class Docx4jStyle_S3 method addCellStyle.

public void addCellStyle(ObjectFactory factory, Tc tableCell, String content, Docx4jStyle_S3 style) {
    if (style != null) {
        P paragraph = factory.createP();
        Text text = factory.createText();
        text.setValue(content);
        R run = factory.createR();
        run.getContent().add(text);
        paragraph.getContent().add(run);
        setHorizontalAlignment(paragraph, style.getHorizAlignment());
        RPr runProperties = factory.createRPr();
        if (style.isBold()) {
            addBoldStyle(runProperties);
        }
        if (style.isItalic()) {
            addItalicStyle(runProperties);
        }
        if (style.isUnderline()) {
            addUnderlineStyle(runProperties);
        }
        setFontSize(runProperties, style.getFontSize());
        setFontColor(runProperties, style.getFontColor());
        setFontFamily(runProperties, style.getCnFontFamily(), style.getEnFontFamily());
        setCellMargins(tableCell, style.getTop(), style.getRight(), style.getBottom(), style.getLeft());
        setCellColor(tableCell, style.getBackground());
        setVerticalAlignment(tableCell, style.getVerticalAlignment());
        setCellBorders(tableCell, style.isBorderTop(), style.isBorderRight(), style.isBorderBottom(), style.isBorderLeft());
        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 90 with P

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

the class Docx4j_工具类_S3_Test method setTcContent.

/**
 * @Description:设置单元格内容,content为null则清除单元格内容
 */
public void setTcContent(Tc tc, RPr rpr, String content) {
    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 {
        p = new P();
        tc.getContent().add(p);
    }
    R run = null;
    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);
        }
    }
    run = new R();
    p.getContent().add(run);
    if (content != null) {
        String[] contentArr = content.split("\n");
        Text text = new Text();
        text.setSpace("preserve");
        text.setValue(contentArr[0]);
        run.setRPr(rpr);
        run.getContent().add(text);
        for (int i = 1, len = contentArr.length; i < len; i++) {
            Br br = new Br();
            // 换行
            run.getContent().add(br);
            text = new Text();
            text.setSpace("preserve");
            text.setValue(contentArr[i]);
            run.setRPr(rpr);
            run.getContent().add(text);
        }
    }
}
Also used : P(org.docx4j.wml.P) Br(org.docx4j.wml.Br) R(org.docx4j.wml.R) 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