Search in sources :

Example 36 with WordprocessingMLPackage

use of org.docx4j.openpackaging.packages.WordprocessingMLPackage 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 37 with WordprocessingMLPackage

use of org.docx4j.openpackaging.packages.WordprocessingMLPackage 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 38 with WordprocessingMLPackage

use of org.docx4j.openpackaging.packages.WordprocessingMLPackage in project Java-Tutorial by gpcodervn.

the class XhtmlToDocxAndBack method main.

public static void main(String[] args) throws Exception {
    String xhtml = "<table border=\"1\" cellpadding=\"1\" cellspacing=\"1\" style=\"width:100%;\"><tbody><tr><td>test</td><td>test</td></tr><tr><td>test</td><td>test</td></tr><tr><td>test</td><td>test</td></tr></tbody></table>";
    // To docx, with content controls
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
    XHTMLImporterImpl XHTMLImporter = new XHTMLImporterImpl(wordMLPackage);
    // XHTMLImporter.setDivHandler(new DivToSdt());
    wordMLPackage.getMainDocumentPart().getContent().addAll(XHTMLImporter.convert(xhtml, null));
    System.out.println(XmlUtils.marshaltoString(wordMLPackage.getMainDocumentPart().getJaxbElement(), true, true));
    // wordMLPackage.save(new java.io.File(System.getProperty("user.dir")
    // + "/OUT_from_XHTML.docx"));
    // Back to XHTML
    HTMLSettings htmlSettings = Docx4J.createHTMLSettings();
    htmlSettings.setWmlPackage(wordMLPackage);
    // output to an OutputStream.
    OutputStream os = new ByteArrayOutputStream();
    // If you want XHTML output
    Docx4jProperties.setProperty("docx4j.Convert.Out.HTML.OutputMethodXML", true);
    Docx4J.toHTML(htmlSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);
    System.out.println(((ByteArrayOutputStream) os).toString());
}
Also used : XHTMLImporterImpl(org.docx4j.convert.in.xhtml.XHTMLImporterImpl) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HTMLSettings(org.docx4j.convert.out.HTMLSettings) ByteArrayOutputStream(java.io.ByteArrayOutputStream) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage)

Example 39 with WordprocessingMLPackage

use of org.docx4j.openpackaging.packages.WordprocessingMLPackage 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 40 with WordprocessingMLPackage

use of org.docx4j.openpackaging.packages.WordprocessingMLPackage 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)

Aggregations

WordprocessingMLPackage (org.docx4j.openpackaging.packages.WordprocessingMLPackage)67 File (java.io.File)50 MainDocumentPart (org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart)22 Docx4JException (org.docx4j.openpackaging.exceptions.Docx4JException)11 XHTMLImporterImpl (org.docx4j.convert.in.xhtml.XHTMLImporterImpl)9 OutputStream (java.io.OutputStream)8 ObjectFactory (org.docx4j.wml.ObjectFactory)8 Test (org.junit.Test)8 P (org.docx4j.wml.P)7 Text (org.docx4j.wml.Text)7 FileOutputStream (java.io.FileOutputStream)6 FileInputStream (java.io.FileInputStream)5 Body (org.docx4j.wml.Body)5 Tbl (org.docx4j.wml.Tbl)5 TraversalUtil (org.docx4j.TraversalUtil)4 NumberingDefinitionsPart (org.docx4j.openpackaging.parts.WordprocessingML.NumberingDefinitionsPart)4 R (org.docx4j.wml.R)4 RPr (org.docx4j.wml.RPr)4 SpecExample (com.vladsch.flexmark.spec.SpecExample)3 IOException (java.io.IOException)3