Search in sources :

Example 36 with P

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

the class AddingPageNrToFooter method addPageBreak.

/**
 * Adds a page break to the document.
 *
 * @param documentPart
 */
private static void addPageBreak(MainDocumentPart documentPart) {
    Br breakObj = new Br();
    breakObj.setType(STBrType.PAGE);
    P paragraph = factory.createP();
    paragraph.getContent().add(breakObj);
    documentPart.getJaxbElement().getBody().getContent().add(paragraph);
}
Also used : Br(org.docx4j.wml.Br) P(org.docx4j.wml.P)

Example 37 with P

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

the class WMLPackageUtils method replaceParagraph.

/**
 *  向模版文档添加段落
 *		你可能想知道为什么我们需要添加段落?我们已经可以添加文本,难道段落不就是一大段的文本吗?好吧,既是也不是,一个段落确实看起来像是一大段文本,但你需要考虑的是换行符,如果你像前面一样添加一个Text元素并且在文本中添加换行符,它们并不会出现,当你想要换行符时,你就需要创建一个新的段落。然而,幸运的是这对于Docx4j来说也非常地容易。
 *		做这个需要下面的几步:
 *
 *		    从模版中找到要替换的段落
 *		    将输入文本拆分成单独的行
 *		    每一行基于模版中的段落创建一个新的段落
 *		    移除原来的段落
 *
 * ******************************************************************
 */
public static void replaceParagraph(String placeholder, String textToAdd, WordprocessingMLPackage template, ContentAccessor addTo) {
    // 1. get the paragraph
    List<P> paragraphs = getTargetElements(template.getMainDocumentPart(), P.class);
    P toReplace = null;
    for (P p : paragraphs) {
        List<Text> texts = getTargetElements(p, Text.class);
        for (Text t : texts) {
            Text content = (Text) t;
            if (content.getValue().equals(placeholder)) {
                toReplace = (P) p;
                break;
            }
        }
    }
    // we now have the paragraph that contains our placeholder: toReplace
    // 2. split into seperate lines
    String[] as = StringUtils.splitPreserveAllTokens(textToAdd, '\n');
    for (int i = 0; i < as.length; i++) {
        String ptext = as[i];
        // 3. copy the found paragraph to keep styling correct
        P copy = (P) XmlUtils.deepCopy(toReplace);
        // replace the text elements from the copy
        List<?> texts = getTargetElements(copy, Text.class);
        if (texts.size() > 0) {
            Text textToReplace = (Text) texts.get(0);
            textToReplace.setValue(ptext);
        }
        // add the paragraph to the document
        addTo.getContent().add(copy);
    }
    // 4. remove the original one
    ((ContentAccessor) toReplace.getParent()).getContent().remove(toReplace);
}
Also used : P(org.docx4j.wml.P) Text(org.docx4j.wml.Text)

Example 38 with P

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

the class WmlElementUtils method createFooter.

/**
 *  First we create a footer, a paragraph, a run and a text. We add the given
 *  given content to the text and add that to the run. The run is then added to
 *  the paragraph, which is in turn added to the footer. Finally we return the
 *  footer.
 *  @param content
 *  @return
 */
public static Ftr createFooter(String content) {
    Ftr footer = factory.createFtr();
    P paragraph = factory.createP();
    R run = factory.createR();
    Text text = new Text();
    text.setValue(content);
    run.getContent().add(text);
    paragraph.getContent().add(run);
    footer.getContent().add(paragraph);
    return footer;
}
Also used : P(org.docx4j.wml.P) R(org.docx4j.wml.R) Ftr(org.docx4j.wml.Ftr) Text(org.docx4j.wml.Text)

Example 39 with P

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

the class ParagraphUtils method addInlineImageToParagraph.

/**
 *  向新的段落中添加内联图片并返回这个段落.
 *  这个方法与前面例子中的方法没有区别.
 * @param inline
 * @return
 */
public static P addInlineImageToParagraph(Inline inline) {
    // Now add the in-line image to a paragraph
    ObjectFactory factory = new ObjectFactory();
    P paragraph = factory.createP();
    R run = factory.createR();
    paragraph.getContent().add(run);
    Drawing drawing = factory.createDrawing();
    run.getContent().add(drawing);
    drawing.getAnchorOrInline().add(inline);
    return paragraph;
}
Also used : P(org.docx4j.wml.P) Drawing(org.docx4j.wml.Drawing) R(org.docx4j.wml.R) ObjectFactory(org.docx4j.wml.ObjectFactory)

Example 40 with P

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

the class DocxBuilder method createIt.

public static P createIt() {
    org.docx4j.wml.ObjectFactory wmlObjectFactory = new org.docx4j.wml.ObjectFactory();
    P p = wmlObjectFactory.createP();
    // Create object for pPr
    PPr ppr = wmlObjectFactory.createPPr();
    p.setPPr(ppr);
    // Create object for rPr
    ParaRPr pararpr = wmlObjectFactory.createParaRPr();
    ppr.setRPr(pararpr);
    // Create object for u
    U u = wmlObjectFactory.createU();
    pararpr.setU(u);
    u.setVal(org.docx4j.wml.UnderlineEnumeration.SINGLE);
    // Create object for lang
    CTLanguage language = wmlObjectFactory.createCTLanguage();
    pararpr.setLang(language);
    language.setVal("en-AU");
    // Create object for jc
    Jc jc = wmlObjectFactory.createJc();
    ppr.setJc(jc);
    jc.setVal(org.docx4j.wml.JcEnumeration.CENTER);
    // Create object for r
    R r = wmlObjectFactory.createR();
    p.getContent().add(r);
    // Create object for rPr
    RPr rpr = wmlObjectFactory.createRPr();
    r.setRPr(rpr);
    // Create object for u
    U u2 = wmlObjectFactory.createU();
    rpr.setU(u2);
    u2.setVal(org.docx4j.wml.UnderlineEnumeration.SINGLE);
    // Create object for lang
    CTLanguage language2 = wmlObjectFactory.createCTLanguage();
    rpr.setLang(language2);
    language2.setVal("en-AU");
    // Create object for t (wrapped in JAXBElement)
    Text text = wmlObjectFactory.createText();
    JAXBElement<org.docx4j.wml.Text> textWrapped = wmlObjectFactory.createRT(text);
    r.getContent().add(textWrapped);
    text.setValue("Underlined and centred");
    return p;
}
Also used : Text(org.docx4j.wml.Text) P(org.docx4j.wml.P) CTLanguage(org.docx4j.wml.CTLanguage) R(org.docx4j.wml.R) PPr(org.docx4j.wml.PPr) U(org.docx4j.wml.U) RPr(org.docx4j.wml.RPr) ParaRPr(org.docx4j.wml.ParaRPr) ParaRPr(org.docx4j.wml.ParaRPr) Jc(org.docx4j.wml.Jc)

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