Search in sources :

Example 66 with WordprocessingMLPackage

use of org.docx4j.openpackaging.packages.WordprocessingMLPackage in project flexmark-java by vsch.

the class ComboDocxUserSpecDisabled method testCase.

@Override
protected void testCase(final Node node, final DataHolder options) {
    if (!DUMP_TEST_CASE_FILES)
        return;
    final SpecExample specExample = example();
    if (!specExample.isFullSpecExample() && !specExample.getSection().isEmpty()) {
        // write it out to file, hard-coded for now                    IGNORE
        File file = new File(String.format("%s/%s%s_%d.docx", PROJECT_ROOT_DIRECTORY, FILE_TEST_CASE_DUMP_LOCATION, specExample.getSection(), specExample.getExampleNumber()));
        File file2 = new File(String.format("%s/%s%s_%d.xml", PROJECT_ROOT_DIRECTORY, FILE_TEST_CASE_DUMP_LOCATION, specExample.getSection(), specExample.getExampleNumber()));
        WordprocessingMLPackage mlPackage = DocxRenderer.getDefaultTemplate();
        if (mlPackage != null) {
            RENDERER.withOptions(options).render(node, mlPackage);
            try {
                mlPackage.save(file, Docx4J.FLAG_SAVE_ZIP_FILE);
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                try {
                    mlPackage.save(outputStream, Docx4J.FLAG_SAVE_FLAT_XML);
                    final String xml = outputStream.toString("UTF-8");
                    final String s = XmlDocxSorter.sortDocumentParts(xml);
                    FileWriter fileWriter = new FileWriter(file2);
                    fileWriter.append(s);
                    fileWriter.append('\n');
                    fileWriter.close();
                } catch (Docx4JException e) {
                    e.printStackTrace();
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } catch (Docx4JException e) {
                e.printStackTrace();
            }
        }
    }
}
Also used : SpecExample(com.vladsch.flexmark.spec.SpecExample) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) Docx4JException(org.docx4j.openpackaging.exceptions.Docx4JException)

Example 67 with WordprocessingMLPackage

use of org.docx4j.openpackaging.packages.WordprocessingMLPackage in project flexmark-java by vsch.

the class DocxRenderer method render.

/**
 * Render the tree of nodes to DocX.
 *
 * @param node the root node
 * @return the rendered HTML
 */
public String render(Node node) {
    WordprocessingMLPackage mlPackage = getDefaultTemplate();
    render(node, mlPackage);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try {
        mlPackage.save(outputStream, Docx4J.FLAG_SAVE_FLAT_XML);
        final String s = options.get(RENDER_BODY_ONLY) ? XmlFormatter.formatDocumentBody(outputStream.toString("UTF-8")) : XmlDocxSorter.sortDocumentParts(outputStream.toString("UTF-8"));
        return s;
    } catch (Docx4JException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) Docx4JException(org.docx4j.openpackaging.exceptions.Docx4JException)

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