use of org.docx4j.wml.P in project docx4j-template by vindell.
the class Docx4j_Helper method testDocx4jSetPageSize.
public void testDocx4jSetPageSize() throws Exception {
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();
String titleStr = "静夜思 李白";
String str = "床前明月光,疑似地上霜。";
String str2 = "举头望明月,低头思故乡。";
P p = Docx4j_Helper.factory.createP();
String rprStr = "<w:rPr xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\"><w:rFonts w:hint=\"eastAsia\" w:ascii=\"Times New Roman\" w:hAnsi=\"Times New Roman\" w:eastAsia=\"宋体\"/><w:b/><w:color w:val=\"333333\"/><w:sz w:val=\"32\"/><w:szCs w:val=\"32\"/></w:rPr>";
RPr rpr = (RPr) XmlUtils.unmarshalString(rprStr);
setParagraphContent(p, rpr, titleStr);
mdp.addObject(p);
p = Docx4j_Helper.factory.createP();
setParagraphContent(p, rpr, str);
mdp.addObject(p);
p = Docx4j_Helper.factory.createP();
PPr pPr = Docx4j_Helper.factory.createPPr();
// 设置文字方向
SectPr sectPr = Docx4j_Helper.factory.createSectPr();
TextDirection textDirect = Docx4j_Helper.factory.createTextDirection();
// 文字方向:垂直方向从右往左
textDirect.setVal("tbRl");
sectPr.setTextDirection(textDirect);
Type sectType = Docx4j_Helper.factory.createSectPrType();
// 下一页
sectType.setVal("nextPage");
sectPr.setType(sectType);
// 设置页面大小
PgSz pgSz = Docx4j_Helper.factory.createSectPrPgSz();
pgSz.setW(new BigInteger("8335"));
pgSz.setH(new BigInteger("11850"));
sectPr.setPgSz(pgSz);
pPr.setSectPr(sectPr);
p.setPPr(pPr);
setParagraphContent(p, rpr, str2);
mdp.addObject(p);
p = createParagraphWithHAlign();
setParagraphContent(p, rpr, titleStr);
mdp.addObject(p);
p = createParagraphWithHAlign();
setParagraphContent(p, rpr, str);
mdp.addObject(p);
p = createParagraphWithHAlign();
setParagraphContent(p, rpr, str2);
mdp.addObject(p);
// Docx4j_Helper.saveWordPackage(wordMLPackage, outputfilepath);
}
use of org.docx4j.wml.P in project docx4j-template by vindell.
the class Docx4jStyle_S3 method newImage.
public P newImage(WordprocessingMLPackage wordMLPackage, ObjectFactory factory, byte[] bytes, String filenameHint, String altText, int id1, int id2, long cx) throws Exception {
BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, bytes);
Inline inline = imagePart.createImageInline(filenameHint, altText, id1, id2, cx, false);
// Now add the inline in w:p/w:r/w:drawing
P p = factory.createP();
R run = factory.createR();
p.getContent().add(run);
Drawing drawing = factory.createDrawing();
run.getContent().add(drawing);
drawing.getAnchorOrInline().add(inline);
return p;
}
use of org.docx4j.wml.P in project docx4j-template by vindell.
the class Docx4jStyle_S3 method createTableWithContent.
public Tbl createTableWithContent(WordprocessingMLPackage wordMLPackage, ObjectFactory factory, String imgFilePath) throws Exception {
Tbl table = factory.createTbl();
// for TEST: this adds borders to all cells
TblPr tblPr = new TblPr();
TblStyle tblStyle = new TblStyle();
tblStyle.setVal("TableGrid");
tblPr.setTblStyle(tblStyle);
table.setTblPr(tblPr);
Tr tableRow = factory.createTr();
// a default table cell style
Docx4jStyle_S3 defStyle = new Docx4jStyle_S3();
defStyle.setBold(false);
defStyle.setItalic(false);
defStyle.setUnderline(false);
defStyle.setHorizAlignment(JcEnumeration.CENTER);
// a specific table cell style
Docx4jStyle_S3 style = new Docx4jStyle_S3();
style.setBold(true);
style.setItalic(true);
style.setUnderline(true);
style.setFontSize("40");
style.setFontColor("FF0000");
style.setCnFontFamily("微软雅黑");
style.setEnFontFamily("Times New Roman");
style.setTop(300);
style.setBackground("CCFFCC");
style.setVerticalAlignment(STVerticalJc.CENTER);
style.setHorizAlignment(JcEnumeration.CENTER);
style.setBorderTop(true);
style.setBorderBottom(true);
style.setNoWrap(true);
addTableCell(factory, tableRow, "测试Field 1", 3500, style, 1, null);
// start vertical merge for Filed 2 and Field 3 on 3 rows
addTableCell(factory, tableRow, "测试Field 2", 3500, defStyle, 1, "restart");
addTableCell(factory, tableRow, "测试Field 3", 1500, defStyle, 1, "restart");
table.getContent().add(tableRow);
tableRow = factory.createTr();
addTableCell(factory, tableRow, "Text", 3500, defStyle, 1, null);
addTableCell(factory, tableRow, "", 3500, defStyle, 1, "");
addTableCell(factory, tableRow, "", 1500, defStyle, 1, "");
table.getContent().add(tableRow);
tableRow = factory.createTr();
addTableCell(factory, tableRow, "Interval", 3500, defStyle, 1, null);
addTableCell(factory, tableRow, "", 3500, defStyle, 1, "close");
addTableCell(factory, tableRow, "", 1500, defStyle, 1, "close");
table.getContent().add(tableRow);
// add an image horizontally merged on 3 cells
String filenameHint = null;
String altText = null;
int id1 = 0;
int id2 = 1;
byte[] bytes = getImageBytes(imgFilePath);
P pImage;
try {
pImage = newImage(wordMLPackage, factory, bytes, filenameHint, altText, id1, id2, 8500);
tableRow = factory.createTr();
addTableCell(factory, tableRow, pImage, 8500, defStyle, 3, null);
table.getContent().add(tableRow);
} catch (Exception e) {
e.printStackTrace();
}
return table;
}
use of org.docx4j.wml.P in project docx4j-template by vindell.
the class Docx4jStyle_S3 method addCellStyle.
public void addCellStyle(ObjectFactory factory, Tc tableCell, String content, Docx4jStyle_S3 style) {
if (style != null) {
P paragraph = factory.createP();
Text text = factory.createText();
text.setValue(content);
R run = factory.createR();
run.getContent().add(text);
paragraph.getContent().add(run);
setHorizontalAlignment(paragraph, style.getHorizAlignment());
RPr runProperties = factory.createRPr();
if (style.isBold()) {
addBoldStyle(runProperties);
}
if (style.isItalic()) {
addItalicStyle(runProperties);
}
if (style.isUnderline()) {
addUnderlineStyle(runProperties);
}
setFontSize(runProperties, style.getFontSize());
setFontColor(runProperties, style.getFontColor());
setFontFamily(runProperties, style.getCnFontFamily(), style.getEnFontFamily());
setCellMargins(tableCell, style.getTop(), style.getRight(), style.getBottom(), style.getLeft());
setCellColor(tableCell, style.getBackground());
setVerticalAlignment(tableCell, style.getVerticalAlignment());
setCellBorders(tableCell, style.isBorderTop(), style.isBorderRight(), style.isBorderBottom(), style.isBorderLeft());
run.setRPr(runProperties);
tableCell.getContent().add(paragraph);
}
}
use of org.docx4j.wml.P in project docx4j-template by vindell.
the class Docx4j_工具类_S3_Test method setTcContent.
/**
* @Description:设置单元格内容,content为null则清除单元格内容
*/
public void setTcContent(Tc tc, RPr rpr, String content) {
List<Object> pList = tc.getContent();
P p = null;
if (pList != null && pList.size() > 0) {
if (pList.get(0) instanceof P) {
p = (P) pList.get(0);
}
} else {
p = new P();
tc.getContent().add(p);
}
R run = null;
List<Object> rList = p.getContent();
if (rList != null && rList.size() > 0) {
for (int i = 0, len = rList.size(); i < len; i++) {
// 清除内容(所有的r
p.getContent().remove(0);
}
}
run = new R();
p.getContent().add(run);
if (content != null) {
String[] contentArr = content.split("\n");
Text text = new Text();
text.setSpace("preserve");
text.setValue(contentArr[0]);
run.setRPr(rpr);
run.getContent().add(text);
for (int i = 1, len = contentArr.length; i < len; i++) {
Br br = new Br();
// 换行
run.getContent().add(br);
text = new Text();
text.setSpace("preserve");
text.setValue(contentArr[i]);
run.setRPr(rpr);
run.getContent().add(text);
}
}
}
Aggregations