use of org.docx4j.openpackaging.packages.WordprocessingMLPackage in project tutorials by eugenp.
the class Docx4jExample method createDocumentPackage.
void createDocumentPackage(String outputPath, String imagePath) throws Exception {
WordprocessingMLPackage wordPackage = WordprocessingMLPackage.createPackage();
MainDocumentPart mainDocumentPart = wordPackage.getMainDocumentPart();
mainDocumentPart.addStyledParagraphOfText("Title", "Hello World!");
mainDocumentPart.addParagraphOfText("Welcome To Baeldung!");
ObjectFactory factory = Context.getWmlObjectFactory();
P p = factory.createP();
R r = factory.createR();
Text t = factory.createText();
t.setValue("Welcome To Baeldung");
r.getContent().add(t);
p.getContent().add(r);
RPr rpr = factory.createRPr();
BooleanDefaultTrue b = new BooleanDefaultTrue();
rpr.setB(b);
rpr.setI(b);
rpr.setCaps(b);
Color red = factory.createColor();
red.setVal("green");
rpr.setColor(red);
r.setRPr(rpr);
mainDocumentPart.getContent().add(p);
File image = new File(imagePath);
byte[] fileContent = Files.readAllBytes(image.toPath());
BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordPackage, fileContent);
Inline inline = imagePart.createImageInline("Baeldung Image", "Alt Text", 1, 2, false);
P Imageparagraph = addImageToParagraph(inline);
mainDocumentPart.getContent().add(Imageparagraph);
int writableWidthTwips = wordPackage.getDocumentModel().getSections().get(0).getPageDimensions().getWritableWidthTwips();
int columnNumber = 3;
Tbl tbl = TblFactory.createTable(3, 3, writableWidthTwips / columnNumber);
List<Object> rows = tbl.getContent();
for (Object row : rows) {
Tr tr = (Tr) row;
List<Object> cells = tr.getContent();
for (Object cell : cells) {
Tc td = (Tc) cell;
td.getContent().add(p);
}
}
mainDocumentPart.getContent().add(tbl);
File exportFile = new File(outputPath);
wordPackage.save(exportFile);
}
use of org.docx4j.openpackaging.packages.WordprocessingMLPackage in project docx4j-template by vindell.
the class Docx4J_例子2 method main.
public static void main(String[] args) throws Exception {
Docx4J_例子2 t = new Docx4J_例子2();
WordprocessingMLPackage wordMLPackage = t.createWordprocessingMLPackage();
MainDocumentPart mp = wordMLPackage.getMainDocumentPart();
ObjectFactory factory = Context.getWmlObjectFactory();
Relationship relationship = t.createHeaderPart(wordMLPackage, mp, factory, false, "3");
relationship = t.createTextHeaderPart(wordMLPackage, mp, factory, "我是页眉,独乐乐不如众乐乐", true, "3", JcEnumeration.CENTER);
t.addParagraphTest(wordMLPackage, mp, factory);
t.addPageBreak(wordMLPackage, factory, STBrType.PAGE);
t.createHeaderReference(wordMLPackage, mp, factory, relationship);
t.createNormalTableTest(wordMLPackage, mp, factory);
t.addPageBreak(wordMLPackage, factory, STBrType.TEXT_WRAPPING);
t.createTableTest(wordMLPackage, mp, factory);
t.addPageBreak(wordMLPackage, factory, STBrType.TEXT_WRAPPING);
P paragraph = factory.createP();
CTBorder topBorder = new CTBorder();
topBorder.setSpace(new BigInteger("1"));
topBorder.setSz(new BigInteger("2"));
topBorder.setVal(STBorder.WAVE);
t.createParagraghLine(wordMLPackage, mp, factory, paragraph, topBorder, topBorder, topBorder, topBorder);
mp.addObject(paragraph);
t.createHyperlink(wordMLPackage, mp, factory, paragraph, "mailto:1329186624@qq.com?subject=docx4j测试", "联系我", "微软雅黑", "24", JcEnumeration.CENTER);
// 页脚
// relationship = t.createFooterPart(wordMLPackage, mp, factory,
// false,"3");
// relationship = t.createTextFooterPart(wordMLPackage, mp,
// factory,"我是页脚", true, "3", JcEnumeration.CENTER);
relationship = t.createFooterPageNumPart(wordMLPackage, mp, factory, false, "3", JcEnumeration.CENTER);
t.createFooterReference(wordMLPackage, mp, factory, relationship);
t.saveWordPackage(wordMLPackage, new File("f:/saveFile/temp/s7_simple.docx"));
}
use of org.docx4j.openpackaging.packages.WordprocessingMLPackage in project docx4j-template by vindell.
the class Docx4jTest method main.
public static void main(String[] args) throws Exception {
// 创建一个word
// 读取可以使用WordprocessingMLPackage.load方法
WordprocessingMLPackage word = WordprocessingMLPackage.createPackage();
String imageFilePath = Docx4jTest.class.getResource("/lw.jpg").getPath();
// 创建页眉
HeaderPart headerPart = createHeader(word);
// 页眉添加图片
headerPart.getContent().add(newImage(word, headerPart, imageFilePath));
// 创建页脚
FooterPart footerPart = createFooter(word);
// 添加图片
footerPart.getContent().add(newImage(word, footerPart, imageFilePath));
// 主内容添加文本和图片
word.getMainDocumentPart().getContent().add(newText("http://www.mybatis.tk"));
word.getMainDocumentPart().getContent().add(newText("http://blog.csdn.net/isea533"));
word.getMainDocumentPart().getContent().add(newImage(word, word.getMainDocumentPart(), imageFilePath));
// 保存
word.save(new File("d:/1.docx"));
}
use of org.docx4j.openpackaging.packages.WordprocessingMLPackage in project docx4j-template by vindell.
the class Docx4j_Helper method name.
public static void name() throws Exception {
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
String titleStr = "测试插入段落";
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);
documentPart.getContent().add(5, p);
String tblPrStr = "<w:tblPr xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\"><w:tblW w:w=\"8522\" w:type=\"dxa\"/><w:tblBorders><w:top w:val=\"single\" w:sz=\"4\" w:space=\"0\"/><w:left w:val=\"single\" w:sz=\"4\" w:space=\"0\"/><w:bottom w:val=\"single\" w:sz=\"4\" w:space=\"0\"/><w:right w:val=\"single\" w:sz=\"4\" w:space=\"0\"/><w:insideH w:val=\"single\" w:sz=\"4\" w:space=\"0\"/></w:tblBorders></w:tblPr>";
Tbl tbl = Docx4j_Helper.factory.createTbl();
TblPr tblPr = (TblPr) XmlUtils.unmarshalString(tblPrStr);
tbl.setTblPr(tblPr);
Tr tr = Docx4j_Helper.factory.createTr();
Tc tc = Docx4j_Helper.factory.createTc();
tr.getContent().add(tc);
tc = Docx4j_Helper.factory.createTc();
tr.getContent().add(tc);
tc = Docx4j_Helper.factory.createTc();
tr.getContent().add(tc);
tc = Docx4j_Helper.factory.createTc();
tr.getContent().add(tc);
tc = Docx4j_Helper.factory.createTc();
tr.getContent().add(tc);
tbl.getContent().add(tr);
tr = Docx4j_Helper.factory.createTr();
tc = Docx4j_Helper.factory.createTc();
tr.getContent().add(tc);
tc = Docx4j_Helper.factory.createTc();
tr.getContent().add(tc);
tc = Docx4j_Helper.factory.createTc();
tr.getContent().add(tc);
tc = Docx4j_Helper.factory.createTc();
tr.getContent().add(tc);
tc = Docx4j_Helper.factory.createTc();
tr.getContent().add(tc);
tbl.getContent().add(tr);
documentPart.getContent().add(9, tbl);
// Docx4j_Helper.saveWordPackage(wordMLPackage, outputfilepath);
}
use of org.docx4j.openpackaging.packages.WordprocessingMLPackage 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);
}
Aggregations