Search in sources :

Example 16 with MainDocumentPart

use of org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart in project TranskribusCore by Transkribus.

the class DocxBuilder method main.

public static void main(String[] args) throws Exception {
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
    MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();
    // Setup FootnotesPart if necessary,
    // along with DocumentSettings
    FootnotesPart footnotesPart = mdp.getFootnotesPart();
    if (footnotesPart == null) {
        // that'll be the case in this example
        // initialise it
        footnotesPart = new FootnotesPart();
        mdp.addTargetPart(footnotesPart);
        CTFootnotes footnotes = (CTFootnotes) XmlUtils.unwrap(XmlUtils.unmarshalString(footnotePartXML));
        footnotesPart.setJaxbElement(footnotes);
        // Usually the settings part contains footnote properties;
        // so add these if not present
        DocumentSettingsPart dsp = mdp.getDocumentSettingsPart();
        if (dsp == null) {
            // create it
            dsp = new DocumentSettingsPart();
            mdp.addTargetPart(dsp);
        }
        CTSettings settings = dsp.getContents();
        if (settings == null) {
            settings = wmlObjectFactory.createCTSettings();
            dsp.setJaxbElement(settings);
        }
        CTFtnDocProps ftndocprops = settings.getFootnotePr();
        if (ftndocprops == null) {
            String openXML = "<w:footnotePr xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\">" + // these 2 numbers are special, and correspond with string footnotePartXML below
            "<w:footnote w:id=\"-1\"/>" + "<w:footnote w:id=\"0\"/>" + "</w:footnotePr>";
            settings.setFootnotePr((CTFtnDocProps) XmlUtils.unmarshalString(openXML, Context.jc, CTFtnDocProps.class));
        }
    }
    // Example
    // Create and add p
    org.docx4j.wml.ObjectFactory factory = Context.getWmlObjectFactory();
    org.docx4j.wml.P p = factory.createP();
    mdp.getContent().add(p);
    // Add a run
    R r = new R();
    p.getContent().add(r);
    org.docx4j.wml.Text t = factory.createText();
    t.setValue("Hello world");
    r.getContent().add(t);
    // OK, add a footnote
    addFootnote(1, "my footnote", footnotesPart, r);
    // Note: your footnote ids must be distinct; they don't need to be ordered (though Word will do that when you open the docx)
    // Save it
    wordMLPackage.save(new java.io.File("C:/Users/Administrator/footnoteTest2.docx"));
    System.out.println("Saved " + "C:/Users/Administrator/footnoteTest2.docx");
/*
		 * add comments example: Kommentar auf der rechten Seite des Dokuments
		 */
// WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
// 
// // Create and add a Comments Part
// CommentsPart cp = new CommentsPart();
// wordMLPackage.getMainDocumentPart().addTargetPart(cp);
// 
// // Part must have minimal contents
// Comments comments = factory.createComments();
// cp.setJaxbElement(comments);
// 
// // Add a comment to the comments part
// java.math.BigInteger commentId = BigInteger.valueOf(0);
// Comment theComment = createComment(commentId, "fred", null,
// "my first comment");
// comments.getComment().add(theComment);
// 
// // Add comment reference to document
// //P paraToCommentOn = wordMLPackage.getMainDocumentPart().addParagraphOfText("here is some content");
// P p = new P();
// 
// wordMLPackage.getMainDocumentPart().getContent().add(p);
// 
// // Create object for commentRangeStart
// CommentRangeStart commentrangestart = factory.createCommentRangeStart();
// commentrangestart.setId( commentId );  // substitute your comment id
// 
// 
// // The actual content, in the middle
// p.getContent().add(commentrangestart);
// 
// org.docx4j.wml.Text  t = factory.createText();
// t.setValue("hello");
// 
// org.docx4j.wml.R  run = factory.createR();
// run.getContent().add(t);
// 
// p.getContent().add(run);
// 
// // Create object for commentRangeEnd
// CommentRangeEnd commentrangeend = factory.createCommentRangeEnd();
// commentrangeend.setId( commentId );  // substitute your comment id
// 
// p.getContent().add(commentrangeend);
// 
// p.getContent().add(createRunCommentReference(commentId));
// 
// System.out.println(wordMLPackage.getMainDocumentPart().getXML());
// 
// 
// // ++, for next comment ...
// commentId = commentId.add(java.math.BigInteger.ONE);
// 
// wordMLPackage.save(new java.io.File("C:/Users/Administrator/commentTest.docx") );
// System.out.println("Saved " + "C:/Users/Administrator/commentTest.docx");
// 
// 
// System.out.println("Done.");
}
Also used : MainDocumentPart(org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) File(java.io.File) R(org.docx4j.wml.R) DocumentSettingsPart(org.docx4j.openpackaging.parts.WordprocessingML.DocumentSettingsPart) CTFootnotes(org.docx4j.wml.CTFootnotes) FootnotesPart(org.docx4j.openpackaging.parts.WordprocessingML.FootnotesPart) CTFtnDocProps(org.docx4j.wml.CTFtnDocProps) CTSettings(org.docx4j.wml.CTSettings) Text(org.docx4j.wml.Text) P(org.docx4j.wml.P)

Example 17 with MainDocumentPart

use of org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart in project TranskribusCore by Transkribus.

the class CoreUtils method extractTextFromDocx.

/**
 * FIXME not sure if this method does extract text from every possible variation of a docx...
 */
public static String extractTextFromDocx(String filename) throws Docx4JException, JAXBException {
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(filename));
    MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
    String pageTxt = "";
    for (Object o : documentPart.getContent()) {
        if (o == null)
            continue;
        pageTxt += o.toString() + "\n";
    }
    return StringUtils.stripEnd(pageTxt, "\n");
}
Also used : MainDocumentPart(org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) File(java.io.File)

Example 18 with MainDocumentPart

use of org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart in project Java-Tutorial by gpcodervn.

the class ReadFile method main.

public static void main(String[] args) throws Docx4JException, JAXBException {
    File doc = new File("output/welcome1.docx");
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(doc);
    MainDocumentPart mainDocumentPart = wordMLPackage.getMainDocumentPart();
    // get all text nodes from the main document part
    String textNodesXPath = "//w:t";
    List<Object> textNodes = mainDocumentPart.getJAXBNodesViaXPath(textNodesXPath, true);
    for (Object obj : textNodes) {
        Text text = (Text) ((JAXBElement) obj).getValue();
        String textValue = text.getValue();
        System.out.println(textValue);
    }
}
Also used : MainDocumentPart(org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart) Text(org.docx4j.wml.Text) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) File(java.io.File)

Example 19 with MainDocumentPart

use of org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart 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 20 with MainDocumentPart

use of org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart in project Java-Tutorial by gpcodervn.

the class Write_HelloWorld 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");
    File exportFile = new File("output/welcome1.docx");
    wordPackage.save(exportFile);
    System.out.println("Done!");
}
Also used : MainDocumentPart(org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) File(java.io.File)

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