Search in sources :

Example 41 with WordprocessingMLPackage

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

Example 42 with WordprocessingMLPackage

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

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

the class ContentControlsMergeXML281 method main.

public static void main(String[] args) throws Exception {
    // the docx 'template'
    String input_DOCX = System.getProperty("user.dir") + "/sample-docs/word/databinding/binding-simple.docx";
    // the instance data
    String input_XML = System.getProperty("user.dir") + "/sample-docs/word/databinding/binding-simple-data.xml";
    // resulting docx
    String OUTPUT_DOCX = System.getProperty("user.dir") + "/OUT_ContentControlsMergeXML.docx";
    // Load input_template.docx
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(input_DOCX));
    // Inject data_file.xml
    // (this code assumes it is not a StandardisedAnswersPart)
    CustomXmlDataStoragePart customXmlDataStoragePart = CustomXmlDataStoragePartSelector.getCustomXmlDataStoragePart(wordMLPackage);
    if (customXmlDataStoragePart == null) {
        System.out.println("Couldn't find CustomXmlDataStoragePart! exiting..");
        return;
    }
    System.out.println("Getting " + input_XML);
    FileInputStream fis = new FileInputStream(new File(input_XML));
    customXmlDataStoragePart.getData().setDocument(fis);
    SaveToZipFile saver = new SaveToZipFile(wordMLPackage);
    OpenDoPEHandler odh = null;
    try {
        // Process conditionals and repeats
        odh = new OpenDoPEHandler(wordMLPackage);
        odh.preprocess();
        OpenDoPEIntegrity odi = new OpenDoPEIntegrity();
        odi.process(wordMLPackage);
        if (DEBUG) {
            String save_preprocessed;
            if (OUTPUT_DOCX.lastIndexOf(".") == -1) {
                save_preprocessed = OUTPUT_DOCX + "_INT.docx";
            } else {
                save_preprocessed = OUTPUT_DOCX.substring(0, OUTPUT_DOCX.lastIndexOf(".")) + "_INT.docx";
            }
            // System.out.println(
            // XmlUtils.marshaltoString(wordMLPackage.getMainDocumentPart().getJaxbElement(), true, true)
            // );
            saver.save(save_preprocessed);
            System.out.println("Saved: " + save_preprocessed);
        }
    } catch (Docx4JException d) {
        // Probably this docx doesn't contain OpenDoPE convention parts
        System.out.println(d.getMessage());
    }
    // Apply the bindings
    BindingHandler.setHyperlinkStyle("Hyperlink");
    // For docx4j <= 3.2.0
    // BindingHandler.applyBindings(wordMLPackage.getMainDocumentPart());
    // For docx4j > 3.2.0, replace that with:
    AtomicInteger bookmarkId = odh.getNextBookmarkId();
    BindingHandler bh = new BindingHandler(wordMLPackage);
    bh.setStartingIdForNewBookmarks(bookmarkId);
    bh.applyBindings(wordMLPackage.getMainDocumentPart());
    // If you inspect the output, you should see your data in 2 places:
    // 1. the custom xml part
    // 2. (more importantly) the main document part
    // System.out.println(
    // XmlUtils.marshaltoString(wordMLPackage.getMainDocumentPart().getJaxbElement(), true, true)
    // );
    // Strip content controls
    RemovalHandler rh = new RemovalHandler();
    rh.removeSDTs(wordMLPackage, Quantifier.ALL);
    saver.save(OUTPUT_DOCX);
    System.out.println("Saved: " + OUTPUT_DOCX);
}
Also used : OpenDoPEIntegrity(org.docx4j.model.datastorage.OpenDoPEIntegrity) SaveToZipFile(org.docx4j.openpackaging.io.SaveToZipFile) RemovalHandler(org.docx4j.model.datastorage.RemovalHandler) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) Docx4JException(org.docx4j.openpackaging.exceptions.Docx4JException) File(java.io.File) FileInputStream(java.io.FileInputStream) OpenDoPEHandler(org.docx4j.model.datastorage.OpenDoPEHandler) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CustomXmlDataStoragePart(org.docx4j.openpackaging.parts.CustomXmlDataStoragePart) File(java.io.File) SaveToZipFile(org.docx4j.openpackaging.io.SaveToZipFile) BindingHandler(org.docx4j.model.datastorage.BindingHandler)

Example 44 with WordprocessingMLPackage

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

the class ConvertInXHTMLFile method main.

public static void main(String[] args) throws Exception {
    String inputfilepath = System.getProperty("user.dir") + "/somedir/some.html";
    // String baseURL = "file:///C:/Users/jharrop/git/docx4j-ImportXHTML/somedir/";
    String baseURL = "file:///C:/Users/jharrop/git/docx4j-ImportXHTML/";
    String stringFromFile = FileUtils.readFileToString(new File(inputfilepath), "UTF-8");
    String unescaped = stringFromFile;
    // if (stringFromFile.contains("&lt;/") ) {
    // unescaped = StringEscapeUtils.unescapeHtml(stringFromFile);
    // }
    // XHTMLImporter.setTableFormatting(FormattingOption.IGNORE_CLASS);
    // XHTMLImporter.setParagraphFormatting(FormattingOption.IGNORE_CLASS);
    System.out.println("Unescaped: " + unescaped);
    // Setup font mapping
    RFonts rfonts = Context.getWmlObjectFactory().createRFonts();
    rfonts.setAscii("Century Gothic");
    XHTMLImporterImpl.addFontMapping("Century Gothic", rfonts);
    // Create an empty docx package
    // WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new File(System.getProperty("user.dir") + "/styled.docx"));
    NumberingDefinitionsPart ndp = new NumberingDefinitionsPart();
    wordMLPackage.getMainDocumentPart().addTargetPart(ndp);
    ndp.unmarshalDefaultNumbering();
    // Convert the XHTML, and add it into the empty docx we made
    XHTMLImporterImpl XHTMLImporter = new XHTMLImporterImpl(wordMLPackage);
    XHTMLImporter.setHyperlinkStyle("Hyperlink");
    wordMLPackage.getMainDocumentPart().getContent().addAll(XHTMLImporter.convert(unescaped, baseURL));
    System.out.println(XmlUtils.marshaltoString(wordMLPackage.getMainDocumentPart().getJaxbElement(), true, true));
    // System.out.println(
    // XmlUtils.marshaltoString(wordMLPackage.getMainDocumentPart().getNumberingDefinitionsPart().getJaxbElement(), true, true));
    wordMLPackage.save(new java.io.File(System.getProperty("user.dir") + "/OUT_from_XHTML.docx"));
}
Also used : XHTMLImporterImpl(org.docx4j.convert.in.xhtml.XHTMLImporterImpl) NumberingDefinitionsPart(org.docx4j.openpackaging.parts.WordprocessingML.NumberingDefinitionsPart) RFonts(org.docx4j.wml.RFonts) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) File(java.io.File) File(java.io.File)

Example 45 with WordprocessingMLPackage

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

the class DocxToXhtmlAndBack method main.

public static void main(String[] args) throws Exception {
    // String baseURL = "file:///C:/Users/jharrop/git/docx4j-ImportXHTML/images";
    Docx4jProperties.setProperty("docx4j.Convert.Out.HTML.OutputMethodXML", true);
    try {
        getInputFilePath(args);
    } catch (IllegalArgumentException e) {
    }
    System.out.println(inputfilepath);
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(dir + inputfilepath));
    // XHTML export
    AbstractHtmlExporter exporter = new HtmlExporterNG2();
    HtmlSettings htmlSettings = new HtmlSettings();
    htmlSettings.setWmlPackage(wordMLPackage);
    htmlSettings.setImageDirPath(dir + inputfilepath + "_files");
    htmlSettings.setImageTargetUri(dir + inputfilepath + "_files");
    String htmlFilePath = dir + "/DocxToXhtmlAndBack.html";
    OutputStream os = new java.io.FileOutputStream(htmlFilePath);
    // javax.xml.transform.stream.StreamResult result = new javax.xml.transform.stream.StreamResult(os);
    // exporter.html(wordMLPackage, result, htmlSettings);
    // os.flush();
    // os.close();
    Docx4J.toHTML(htmlSettings, os, Docx4J.FLAG_NONE);
    // XHTML to docx
    String stringFromFile = FileUtils.readFileToString(new File(htmlFilePath), "UTF-8");
    WordprocessingMLPackage docxOut = WordprocessingMLPackage.createPackage();
    NumberingDefinitionsPart ndp = new NumberingDefinitionsPart();
    docxOut.getMainDocumentPart().addTargetPart(ndp);
    ndp.unmarshalDefaultNumbering();
    XHTMLImporterImpl XHTMLImporter = new XHTMLImporterImpl(docxOut);
    XHTMLImporter.setHyperlinkStyle("Hyperlink");
    docxOut.getMainDocumentPart().getContent().addAll(XHTMLImporter.convert(stringFromFile, null));
    docxOut.save(new java.io.File(dir + "/DocxToXhtmlAndBack.docx"));
}
Also used : XHTMLImporterImpl(org.docx4j.convert.in.xhtml.XHTMLImporterImpl) NumberingDefinitionsPart(org.docx4j.openpackaging.parts.WordprocessingML.NumberingDefinitionsPart) OutputStream(java.io.OutputStream) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) File(java.io.File) HtmlSettings(org.docx4j.convert.out.html.AbstractHtmlExporter.HtmlSettings) AbstractHtmlExporter(org.docx4j.convert.out.html.AbstractHtmlExporter) HtmlExporterNG2(org.docx4j.convert.out.html.HtmlExporterNG2) 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