Search in sources :

Example 1 with Text

use of org.docx4j.wml.Text in project Java-Tutorial by gpcodervn.

the class HyperlinkTest method testContent.

private void testContent(Object o, Class clazz, String content) {
    assertEquals(o.getClass(), clazz);
    Object o2;
    if (o instanceof P.Hyperlink) {
        P.Hyperlink h = (P.Hyperlink) o;
        o2 = XmlUtils.unwrap(((R) h.getContent().get(0)).getContent().get(0));
    } else {
        R r = (R) o;
        o2 = XmlUtils.unwrap(r.getContent().get(0));
    }
    String runText = ((Text) o2).getValue();
    assertEquals(content, runText);
}
Also used : P(org.docx4j.wml.P) R(org.docx4j.wml.R) Text(org.docx4j.wml.Text)

Example 2 with Text

use of org.docx4j.wml.Text in project Java-Tutorial by gpcodervn.

the class ReportFromTemplate method main.

public static void main(String[] args) {
    final String XPATH_TO_SELECT_TEXT_NODES = "//w:t";
    String fileName = "";
    try {
        // Populate the Strings that will replace the template text
        Map<String, String> map = new HashMap<String, String>();
        map.put("Project", "BP Mount");
        map.put("Date", "21-Mar-2011");
        // C:\\test\\template1.docx is the template file
        WordprocessingMLPackage template = WordprocessingMLPackage.load(new File("resources/Template1.docx"));
        Parts parts = template.getParts();
        HashMap<PartName, Part> partsMap = parts.getParts();
        PartName partName = null;
        Part part = null;
        Set<PartName> set = partsMap.keySet();
        for (Iterator<PartName> iterator = set.iterator(); iterator.hasNext(); ) {
            PartName name = (PartName) iterator.next();
            if (name.getName().equalsIgnoreCase("/word/media/image1.png")) {
                part = partsMap.get(name);
                partName = name;
            }
        }
        if (part != null && partName != null) {
            part = partsMap.get(partName);
            BinaryPart binaryPart = (BinaryPart) part;
            binaryPart.setBinaryData(fileToBytes(fileToReplace));
        }
        List<Object> texts = template.getMainDocumentPart().getJAXBNodesViaXPath(XPATH_TO_SELECT_TEXT_NODES, true);
        for (Object obj : texts) {
            Text text = (Text) ((JAXBElement) obj).getValue();
            String textValue = text.getValue();
            for (Object key : map.keySet()) {
                // textValue = textValue.replaceAll("\\$\\{" + key + "\\}", (String) map.get(key));
                textValue = textValue.trim().replace("${" + key + "}", (String) map.get(key));
            }
            text.setValue(textValue);
        }
        /*
			 * Add the other contents here
			 */
        template.save(new File("output/ReportFromTemplate.docx"));
        System.out.println("Done");
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        System.out.println("Errors");
    }
}
Also used : BinaryPart(org.docx4j.openpackaging.parts.WordprocessingML.BinaryPart) HashMap(java.util.HashMap) Text(org.docx4j.wml.Text) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) PartName(org.docx4j.openpackaging.parts.PartName) Parts(org.docx4j.openpackaging.parts.Parts) Part(org.docx4j.openpackaging.parts.Part) BinaryPart(org.docx4j.openpackaging.parts.WordprocessingML.BinaryPart) File(java.io.File)

Example 3 with Text

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

the class Docx4J_例子2 method createImageParagraph.

// 段落中插入文字和图片
public P createImageParagraph(WordprocessingMLPackage wordMLPackage, ObjectFactory factory, P p, String fileName, String content, byte[] bytes, JcEnumeration jcEnumeration) throws Exception {
    BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, bytes);
    Inline inline = imagePart.createImageInline(fileName, "这是图片", 1, 2, false);
    Text text = factory.createText();
    text.setValue(content);
    text.setSpace("preserve");
    R run = factory.createR();
    p.getContent().add(run);
    run.getContent().add(text);
    Drawing drawing = factory.createDrawing();
    run.getContent().add(drawing);
    drawing.getAnchorOrInline().add(inline);
    PPr pPr = p.getPPr();
    if (pPr == null) {
        pPr = factory.createPPr();
    }
    Jc jc = pPr.getJc();
    if (jc == null) {
        jc = new Jc();
    }
    jc.setVal(jcEnumeration);
    pPr.setJc(jc);
    p.setPPr(pPr);
    setParagraphSpacing(factory, p, jcEnumeration, true, "0", "0", null, null, true, "240", STLineSpacingRule.AUTO);
    return p;
}
Also used : Drawing(org.docx4j.wml.Drawing) 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) Text(org.docx4j.wml.Text) Inline(org.docx4j.dml.wordprocessingDrawing.Inline) BinaryPartAbstractImage(org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage)

Example 4 with Text

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

the class Docx4J_例子2 method getTextHdr.

public Hdr getTextHdr(WordprocessingMLPackage wordprocessingMLPackage, ObjectFactory factory, Part sourcePart, String content, boolean isUnderLine, String underLineSz, JcEnumeration jcEnumeration) throws Exception {
    Hdr hdr = factory.createHdr();
    P headP = factory.createP();
    Text text = factory.createText();
    text.setValue(content);
    R run = factory.createR();
    run.getContent().add(text);
    headP.getContent().add(run);
    PPr pPr = headP.getPPr();
    if (pPr == null) {
        pPr = factory.createPPr();
    }
    Jc jc = pPr.getJc();
    if (jc == null) {
        jc = new Jc();
    }
    jc.setVal(jcEnumeration);
    pPr.setJc(jc);
    if (isUnderLine) {
        PBdr pBdr = pPr.getPBdr();
        if (pBdr == null) {
            pBdr = factory.createPPrBasePBdr();
        }
        CTBorder value = new CTBorder();
        value.setVal(STBorder.SINGLE);
        value.setColor("000000");
        value.setSpace(new BigInteger("0"));
        value.setSz(new BigInteger(underLineSz));
        pBdr.setBetween(value);
        pPr.setPBdr(pBdr);
        headP.setPPr(pPr);
    }
    setParagraphSpacing(factory, headP, jcEnumeration, true, "0", "0", null, null, true, "240", STLineSpacingRule.AUTO);
    hdr.getContent().add(headP);
    if (isUnderLine) {
        hdr.getContent().add(createHeaderBlankP(wordprocessingMLPackage, factory, underLineSz, jcEnumeration));
    }
    return hdr;
}
Also used : P(org.docx4j.wml.P) R(org.docx4j.wml.R) PPr(org.docx4j.wml.PPr) PBdr(org.docx4j.wml.PPrBase.PBdr) CTBorder(org.docx4j.wml.CTBorder) Hdr(org.docx4j.wml.Hdr) STVerticalJc(org.docx4j.wml.STVerticalJc) Jc(org.docx4j.wml.Jc) CTVerticalJc(org.docx4j.wml.CTVerticalJc) BigInteger(java.math.BigInteger) Text(org.docx4j.wml.Text)

Example 5 with Text

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

the class Docx4J_例子2 method createNormalCell.

public void createNormalCell(ObjectFactory factory, Tr tr, String content, RPr rpr, JcEnumeration jcEnumeration, boolean hasBgColor, String backgroudColor, boolean isHMerger, String mergeVal) {
    Tc tableCell = factory.createTc();
    P p = factory.createP();
    setParagraphSpacing(factory, p, jcEnumeration, true, "0", "0", null, null, true, "240", STLineSpacingRule.AUTO);
    Text t = factory.createText();
    t.setValue(content);
    R run = factory.createR();
    // 设置表格内容字体样式
    run.setRPr(rpr);
    TcPr tcPr = tableCell.getTcPr();
    if (tcPr == null) {
        tcPr = factory.createTcPr();
    }
    CTVerticalJc valign = factory.createCTVerticalJc();
    valign.setVal(STVerticalJc.CENTER);
    tcPr.setVAlign(valign);
    if (isHMerger) {
        HMerge merge = new HMerge();
        if (mergeVal != null) {
            merge.setVal(mergeVal);
        }
        tcPr.setHMerge(merge);
    } else {
        VMerge merge = new VMerge();
        if (mergeVal != null) {
            merge.setVal(mergeVal);
        }
        tcPr.setVMerge(merge);
    }
    run.getContent().add(t);
    p.getContent().add(run);
    tableCell.getContent().add(p);
    if (hasBgColor) {
        CTShd shd = tcPr.getShd();
        if (shd == null) {
            shd = factory.createCTShd();
        }
        shd.setColor("auto");
        shd.setFill(backgroudColor);
        tcPr.setShd(shd);
    }
    tableCell.setTcPr(tcPr);
    tr.getContent().add(tableCell);
}
Also used : P(org.docx4j.wml.P) R(org.docx4j.wml.R) TcPr(org.docx4j.wml.TcPr) HMerge(org.docx4j.wml.TcPrInner.HMerge) CTShd(org.docx4j.wml.CTShd) Text(org.docx4j.wml.Text) CTVerticalJc(org.docx4j.wml.CTVerticalJc) Tc(org.docx4j.wml.Tc) VMerge(org.docx4j.wml.TcPrInner.VMerge)

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