Search in sources :

Example 21 with Text

use of org.docx4j.wml.Text 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 22 with Text

use of org.docx4j.wml.Text 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 23 with Text

use of org.docx4j.wml.Text 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 24 with Text

use of org.docx4j.wml.Text in project tutorials by eugenp.

the class Docx4jExample method isTextExist.

boolean isTextExist(String testText) throws Docx4JException, JAXBException {
    File doc = new File("helloWorld.docx");
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(doc);
    MainDocumentPart mainDocumentPart = wordMLPackage.getMainDocumentPart();
    String textNodesXPath = "//w:t";
    List<Object> paragraphs = mainDocumentPart.getJAXBNodesViaXPath(textNodesXPath, true);
    for (Object obj : paragraphs) {
        Text text = (Text) ((JAXBElement) obj).getValue();
        String textValue = text.getValue();
        if (textValue != null && textValue.contains(testText)) {
            return true;
        }
    }
    return false;
}
Also used : MainDocumentPart(org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart) Text(org.docx4j.wml.Text) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) File(java.io.File)

Example 25 with Text

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

Text (org.docx4j.wml.Text)62 R (org.docx4j.wml.R)53 P (org.docx4j.wml.P)32 PPr (org.docx4j.wml.PPr)14 RPr (org.docx4j.wml.RPr)12 Jc (org.docx4j.wml.Jc)10 CTVerticalJc (org.docx4j.wml.CTVerticalJc)9 File (java.io.File)8 Tc (org.docx4j.wml.Tc)7 WordprocessingMLPackage (org.docx4j.openpackaging.packages.WordprocessingMLPackage)6 MainDocumentPart (org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart)6 STVerticalJc (org.docx4j.wml.STVerticalJc)6 BigInteger (java.math.BigInteger)5 Br (org.docx4j.wml.Br)5 ObjectFactory (org.docx4j.wml.ObjectFactory)5 Inline (org.docx4j.dml.wordprocessingDrawing.Inline)4 BinaryPartAbstractImage (org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage)4 CTShd (org.docx4j.wml.CTShd)4 Ftr (org.docx4j.wml.Ftr)4 Tbl (org.docx4j.wml.Tbl)4