Search in sources :

Example 16 with ObjectFactory

use of org.docx4j.wml.ObjectFactory in project Java-Tutorial by gpcodervn.

the class Write_Format method main.

public static void main(String[] args) throws Docx4JException {
    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 green = factory.createColor();
    green.setVal("green");
    rpr.setColor(green);
    r.setRPr(rpr);
    mainDocumentPart.getContent().add(p);
    File exportFile = new File("output/welcome2.docx");
    wordPackage.save(exportFile);
    System.out.println("Done!");
}
Also used : P(org.docx4j.wml.P) R(org.docx4j.wml.R) ObjectFactory(org.docx4j.wml.ObjectFactory) RPr(org.docx4j.wml.RPr) MainDocumentPart(org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart) Color(org.docx4j.wml.Color) Text(org.docx4j.wml.Text) BooleanDefaultTrue(org.docx4j.wml.BooleanDefaultTrue) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) File(java.io.File)

Example 17 with ObjectFactory

use of org.docx4j.wml.ObjectFactory in project Java-Tutorial by gpcodervn.

the class Write_Image method addImageToParagraph.

private static P addImageToParagraph(Inline inline) {
    ObjectFactory factory = new ObjectFactory();
    P p = factory.createP();
    R r = factory.createR();
    p.getContent().add(r);
    Drawing drawing = factory.createDrawing();
    r.getContent().add(drawing);
    drawing.getAnchorOrInline().add(inline);
    return p;
}
Also used : P(org.docx4j.wml.P) Drawing(org.docx4j.wml.Drawing) R(org.docx4j.wml.R) ObjectFactory(org.docx4j.wml.ObjectFactory)

Example 18 with ObjectFactory

use of org.docx4j.wml.ObjectFactory 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 19 with ObjectFactory

use of org.docx4j.wml.ObjectFactory 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 20 with ObjectFactory

use of org.docx4j.wml.ObjectFactory in project tutorials by eugenp.

the class Docx4jExample method addImageToParagraph.

private static P addImageToParagraph(Inline inline) {
    ObjectFactory factory = new ObjectFactory();
    P p = factory.createP();
    R r = factory.createR();
    p.getContent().add(r);
    Drawing drawing = factory.createDrawing();
    r.getContent().add(drawing);
    drawing.getAnchorOrInline().add(inline);
    return p;
}
Also used : P(org.docx4j.wml.P) Drawing(org.docx4j.wml.Drawing) R(org.docx4j.wml.R) ObjectFactory(org.docx4j.wml.ObjectFactory)

Aggregations

ObjectFactory (org.docx4j.wml.ObjectFactory)26 P (org.docx4j.wml.P)18 R (org.docx4j.wml.R)17 File (java.io.File)8 WordprocessingMLPackage (org.docx4j.openpackaging.packages.WordprocessingMLPackage)8 Drawing (org.docx4j.wml.Drawing)8 MainDocumentPart (org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart)7 Text (org.docx4j.wml.Text)6 BooleanDefaultTrue (org.docx4j.wml.BooleanDefaultTrue)5 RPr (org.docx4j.wml.RPr)5 Relationship (org.docx4j.relationships.Relationship)4 PPr (org.docx4j.wml.PPr)4 Tbl (org.docx4j.wml.Tbl)4 Inline (org.docx4j.dml.wordprocessingDrawing.Inline)3 BinaryPartAbstractImage (org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage)3 Tc (org.docx4j.wml.Tc)3 Tr (org.docx4j.wml.Tr)3 Color (org.docx4j.wml.Color)2 Hyperlink (org.docx4j.wml.P.Hyperlink)2 NumPr (org.docx4j.wml.PPrBase.NumPr)2