use of org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart in project Java-Tutorial by gpcodervn.
the class Write_Table method main.
public static void main(String[] args) 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("Cell data");
r.getContent().add(t);
p.getContent().add(r);
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("output/welcome4.docx");
wordPackage.save(exportFile);
System.out.println("Done!");
}
use of org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart 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.parts.WordprocessingML.MainDocumentPart 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.parts.WordprocessingML.MainDocumentPart in project docx4j-template by vindell.
the class Docx4J_简单例子 method addPageBreak.
// 分页
public void addPageBreak(WordprocessingMLPackage wordMLPackage, ObjectFactory factory) {
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
Br breakObj = new Br();
breakObj.setType(STBrType.PAGE);
P paragraph = factory.createP();
paragraph.getContent().add(breakObj);
documentPart.addObject(paragraph);
}
use of org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart 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);
}
Aggregations