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 + "]");
}
}
}
}
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);
}
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;
}
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;
}
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);
}
}
Aggregations