Search in sources :

Example 21 with MainDocumentPart

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!");
}
Also used : MainDocumentPart(org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart) Text(org.docx4j.wml.Text) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) Tc(org.docx4j.wml.Tc) P(org.docx4j.wml.P) R(org.docx4j.wml.R) ObjectFactory(org.docx4j.wml.ObjectFactory) File(java.io.File) Tr(org.docx4j.wml.Tr) Tbl(org.docx4j.wml.Tbl)

Example 22 with MainDocumentPart

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);
}
Also used : MainDocumentPart(org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart) Color(org.docx4j.wml.Color) Text(org.docx4j.wml.Text) BooleanDefaultTrue(org.docx4j.wml.BooleanDefaultTrue) Inline(org.docx4j.dml.wordprocessingDrawing.Inline) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) Tc(org.docx4j.wml.Tc) P(org.docx4j.wml.P) R(org.docx4j.wml.R) ObjectFactory(org.docx4j.wml.ObjectFactory) RPr(org.docx4j.wml.RPr) BinaryPartAbstractImage(org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage) File(java.io.File) Tr(org.docx4j.wml.Tr) Tbl(org.docx4j.wml.Tbl)

Example 23 with MainDocumentPart

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"));
}
Also used : P(org.docx4j.wml.P) ObjectFactory(org.docx4j.wml.ObjectFactory) MainDocumentPart(org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart) CTBorder(org.docx4j.wml.CTBorder) Relationship(org.docx4j.relationships.Relationship) BigInteger(java.math.BigInteger) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) File(java.io.File)

Example 24 with MainDocumentPart

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);
}
Also used : Br(org.docx4j.wml.Br) P(org.docx4j.wml.P) MainDocumentPart(org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart)

Example 25 with MainDocumentPart

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);
}
Also used : P(org.docx4j.wml.P) RPr(org.docx4j.wml.RPr) MainDocumentPart(org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) TblPr(org.docx4j.wml.TblPr) Tr(org.docx4j.wml.Tr) Tbl(org.docx4j.wml.Tbl) Tc(org.docx4j.wml.Tc)

Aggregations

MainDocumentPart (org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart)36 WordprocessingMLPackage (org.docx4j.openpackaging.packages.WordprocessingMLPackage)22 File (java.io.File)19 P (org.docx4j.wml.P)13 ObjectFactory (org.docx4j.wml.ObjectFactory)7 Text (org.docx4j.wml.Text)7 Body (org.docx4j.wml.Body)5 Br (org.docx4j.wml.Br)5 R (org.docx4j.wml.R)5 RPr (org.docx4j.wml.RPr)5 Tbl (org.docx4j.wml.Tbl)5 TraversalUtil (org.docx4j.TraversalUtil)4 Relationship (org.docx4j.relationships.Relationship)4 Tr (org.docx4j.wml.Tr)4 Tc (org.docx4j.wml.Tc)3 Child (org.jvnet.jaxb2_commons.ppp.Child)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 BigInteger (java.math.BigInteger)2 ArrayList (java.util.ArrayList)2