Search in sources :

Example 11 with ObjectFactory

use of org.docx4j.wml.ObjectFactory in project mdw-designer by CenturyLinkCloud.

the class DocxBuilder method addBulletList.

public void addBulletList(Map<String, Object> items) throws Exception {
    if (bulletNum == null) {
        bulletNum = getNdp().addAbstractListNumberingDefinition((Numbering.AbstractNum) XmlUtils.unmarshalString(getFragment("bulletNumbering")));
        bulletNumId = bulletNum.getNumId();
    } else {
        bulletNumId = BigInteger.valueOf(ndp.restart(bulletNumId.longValue(), 0, 1));
    }
    boolean wasTable = false;
    for (String name : items.keySet()) {
        ObjectFactory factory = Context.getWmlObjectFactory();
        P listP = factory.createP();
        Object item = items.get(name);
        PPr listPpr = factory.createPPr();
        listP.setPPr(listPpr);
        Text t = factory.createText();
        R listRun = factory.createR();
        listRun.getContent().add(t);
        listP.getContent().add(listRun);
        if (item instanceof String) {
            t.setValue(name + " = " + item);
            if (wasTable)
                listPpr.setSpacing(createSpacing(100));
            wasTable = false;
        } else if (item instanceof DocxTable) {
            t.setValue(name + ":");
            listPpr.setSpacing(createSpacing(100, 10));
            wasTable = true;
        } else if (item instanceof DocxCodebox) {
            t.setValue(((DocxCodebox) item).label + ":");
            try {
                listRun.getContent().add(createCodeBox((DocxCodebox) item));
            } catch (Exception ex) {
                ex.printStackTrace();
                t.setValue(((DocxCodebox) item).label + ": ![" + ex + "]");
            }
            wasTable = false;
        }
        listPpr.setNumPr(createBulletListNumPr());
        PStyle pStyle = factory.createPPrBasePStyle();
        pStyle.setVal("ListParagraph");
        listPpr.setPStyle(pStyle);
        getMdp().addObject(listP);
        if (item instanceof DocxTable) {
            try {
                addTable((DocxTable) item);
            } catch (Exception ex) {
                ex.printStackTrace();
                t.setValue(name + ": ![" + ex + "]");
            }
        }
    }
}
Also used : Text(org.docx4j.wml.Text) PStyle(org.docx4j.wml.PPrBase.PStyle) JAXBException(javax.xml.bind.JAXBException) InvalidFormatException(org.docx4j.openpackaging.exceptions.InvalidFormatException) IOException(java.io.IOException) Docx4JException(org.docx4j.openpackaging.exceptions.Docx4JException) P(org.docx4j.wml.P) R(org.docx4j.wml.R) PPr(org.docx4j.wml.PPr) ObjectFactory(org.docx4j.wml.ObjectFactory)

Example 12 with ObjectFactory

use of org.docx4j.wml.ObjectFactory in project mdw-designer by CenturyLinkCloud.

the class DocxBuilder method addImage.

public void addImage(byte[] imageBytes) throws Exception {
    BinaryPartAbstractImage.setDensity(600);
    BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, imageBytes);
    Inline inline = imagePart.createImageInline(null, null, 0, 1, false);
    ObjectFactory factory = Context.getWmlObjectFactory();
    P imgP = factory.createP();
    R imgRun = factory.createR();
    imgP.getContent().add(imgRun);
    Drawing drawing = factory.createDrawing();
    imgRun.getContent().add(drawing);
    drawing.getAnchorOrInline().add(inline);
    getMdp().addObject(imgP);
}
Also used : P(org.docx4j.wml.P) Drawing(org.docx4j.wml.Drawing) R(org.docx4j.wml.R) ObjectFactory(org.docx4j.wml.ObjectFactory) Inline(org.docx4j.dml.wordprocessingDrawing.Inline) BinaryPartAbstractImage(org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage)

Example 13 with ObjectFactory

use of org.docx4j.wml.ObjectFactory in project mdw-designer by CenturyLinkCloud.

the class DocxBuilder method createBulletListNumPr.

public NumPr createBulletListNumPr() {
    ObjectFactory factory = Context.getWmlObjectFactory();
    NumPr numPr = factory.createPPrBaseNumPr();
    Ilvl ilvlElement = factory.createPPrBaseNumPrIlvl();
    numPr.setIlvl(ilvlElement);
    ilvlElement.setVal(BigInteger.valueOf(0));
    NumId numIdElement = factory.createPPrBaseNumPrNumId();
    numPr.setNumId(numIdElement);
    numIdElement.setVal(bulletNumId);
    return numPr;
}
Also used : NumId(org.docx4j.wml.PPrBase.NumPr.NumId) ObjectFactory(org.docx4j.wml.ObjectFactory) NumPr(org.docx4j.wml.PPrBase.NumPr) Ilvl(org.docx4j.wml.PPrBase.NumPr.Ilvl)

Example 14 with ObjectFactory

use of org.docx4j.wml.ObjectFactory in project mdw-designer by CenturyLinkCloud.

the class DocxBuilder method addTable.

public Tbl addTable(String[] headers, String[][] values, int fontSize, int indent) {
    ObjectFactory factory = Context.getWmlObjectFactory();
    int writableWidthTwips = wordMLPackage.getDocumentModel().getSections().get(0).getPageDimensions().getWritableWidthTwips();
    int cols = headers.length;
    int cellWidthTwips = new Double(Math.floor((writableWidthTwips / cols))).intValue();
    Tbl tbl = TblFactory.createTable(0, cols, cellWidthTwips);
    Tr thead = factory.createTr();
    for (int i = 0; i < headers.length; i++) {
        Tc tc = factory.createTc();
        tc.getContent().add(createParagraph(headers[i], fontSize, true));
        thead.getContent().add(tc);
    }
    tbl.getContent().add(0, thead);
    for (int i = 0; i < values[0].length; i++) {
        Tr tr = factory.createTr();
        for (int j = 0; j < headers.length; j++) {
            Tc tc = factory.createTc();
            tc.getContent().add(createParagraph(values[j][i], fontSize, false));
            tr.getContent().add(tc);
        }
        tbl.getContent().add(i + 1, tr);
    }
    getMdp().addObject(tbl);
    if (indent != 0) {
        TblPr tblPr = tbl.getTblPr();
        TblWidth tblIndent = factory.createTblWidth();
        tblIndent.setType("dxa");
        tblIndent.setW(BigInteger.valueOf(indent));
        tblPr.setTblInd(tblIndent);
    }
    return tbl;
}
Also used : ObjectFactory(org.docx4j.wml.ObjectFactory) TblWidth(org.docx4j.wml.TblWidth) TblPr(org.docx4j.wml.TblPr) Tr(org.docx4j.wml.Tr) Tbl(org.docx4j.wml.Tbl) Tc(org.docx4j.wml.Tc)

Example 15 with ObjectFactory

use of org.docx4j.wml.ObjectFactory in project mdw-designer by CenturyLinkCloud.

the class DocxBuilder method addNumberedList.

public void addNumberedList(List<String> list) throws Exception {
    if (numberingNum == null) {
        numberingNum = getNdp().addAbstractListNumberingDefinition((Numbering.AbstractNum) XmlUtils.unmarshalString(getFragment("numbering")));
        numberingNumId = numberingNum.getNumId();
    } else {
        numberingNumId = BigInteger.valueOf(ndp.restart(numberingNumId.longValue(), 0, 1));
    }
    for (String item : list) {
        ObjectFactory factory = Context.getWmlObjectFactory();
        P listP = factory.createP();
        Text t = factory.createText();
        t.setValue(item);
        R listRun = factory.createR();
        listRun.getContent().add(t);
        listP.getContent().add(listRun);
        PPr listPpr = factory.createPPr();
        listP.setPPr(listPpr);
        // create and add <w:numPr>
        NumPr numPr = factory.createPPrBaseNumPr();
        listPpr.setNumPr(numPr);
        // the <w:ilvl> element
        Ilvl ilvlElement = factory.createPPrBaseNumPrIlvl();
        numPr.setIlvl(ilvlElement);
        ilvlElement.setVal(BigInteger.valueOf(0));
        // The <w:numId> element
        NumId numIdElement = factory.createPPrBaseNumPrNumId();
        numPr.setNumId(numIdElement);
        numIdElement.setVal(numberingNumId);
        getMdp().addObject(listP);
    }
}
Also used : P(org.docx4j.wml.P) NumId(org.docx4j.wml.PPrBase.NumPr.NumId) R(org.docx4j.wml.R) PPr(org.docx4j.wml.PPr) ObjectFactory(org.docx4j.wml.ObjectFactory) NumPr(org.docx4j.wml.PPrBase.NumPr) Text(org.docx4j.wml.Text) Ilvl(org.docx4j.wml.PPrBase.NumPr.Ilvl)

Aggregations

ObjectFactory (org.docx4j.wml.ObjectFactory)26 P (org.docx4j.wml.P)18 R (org.docx4j.wml.R)17 File (java.io.File)8 WordprocessingMLPackage (org.docx4j.openpackaging.packages.WordprocessingMLPackage)8 Drawing (org.docx4j.wml.Drawing)8 MainDocumentPart (org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart)7 Text (org.docx4j.wml.Text)6 BooleanDefaultTrue (org.docx4j.wml.BooleanDefaultTrue)5 RPr (org.docx4j.wml.RPr)5 Relationship (org.docx4j.relationships.Relationship)4 PPr (org.docx4j.wml.PPr)4 Tbl (org.docx4j.wml.Tbl)4 Inline (org.docx4j.dml.wordprocessingDrawing.Inline)3 BinaryPartAbstractImage (org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage)3 Tc (org.docx4j.wml.Tc)3 Tr (org.docx4j.wml.Tr)3 Color (org.docx4j.wml.Color)2 Hyperlink (org.docx4j.wml.P.Hyperlink)2 NumPr (org.docx4j.wml.PPrBase.NumPr)2