Search in sources :

Example 16 with Docx4JException

use of org.docx4j.openpackaging.exceptions.Docx4JException in project flexmark-java by vsch.

the class ComboDocxConverterIssuesSpecTest 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();
        RENDERER.withOptions(options).render(node, mlPackage);
        File parentDir = file.getParentFile();
        if (!parentDir.exists()) {
            parentDir.mkdirs();
        }
        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 17 with Docx4JException

use of org.docx4j.openpackaging.exceptions.Docx4JException 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 18 with Docx4JException

use of org.docx4j.openpackaging.exceptions.Docx4JException 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)

Example 19 with Docx4JException

use of org.docx4j.openpackaging.exceptions.Docx4JException in project flexmark-java by vsch.

the class CoreNodeDocxRenderer method render.

private void render(Footnote node, final DocxRendererContext docx) {
    final FootnoteBlock footnoteBlock = node.getFootnoteBlock();
    if (footnoteBlock == null) {
        // just text
        final org.docx4j.wml.Text text = docx.addWrappedText();
        text.setValue("[^");
        docx.renderChildren(node);
        final org.docx4j.wml.Text text1 = docx.addWrappedText();
        text1.setValue("]");
    } else {
        try {
            BigInteger footnoteId = footnoteIDs.containsKey(footnoteBlock) ? footnoteIDs.get(footnoteBlock) : BigInteger.ZERO;
            final CTFtnEdn ftnEdn = docx.addFootnote(footnoteId);
            final BigInteger ftnEdnId = ftnEdn.getId();
            if (ftnEdnId.compareTo(footnoteId) != 0) {
                // Word does not like re-using footnotes, so we create a new one for every reference
                // footnoteIDs.put(footnoteBlock, ftnEdnId);
                docx.contextFramed(new Runnable() {

                    @Override
                    public void run() {
                        docx.setBlockFormatProvider(new FootnoteBlockFormatProvider<Node>(docx));
                        docx.setRunFormatProvider(new FootnoteRunFormatProvider<Node>(docx));
                        docx.setContentContainer(new ContentContainer() {

                            @Override
                            public RelationshipsPart getRelationshipsPart() {
                                try {
                                    return docx.getFootnotesPart().getRelationshipsPart();
                                } catch (Docx4JException e) {
                                    e.printStackTrace();
                                    return docx.getDocxDocument().getRelationshipsPart();
                                }
                            }

                            @Override
                            public Part getContainerPart() {
                                try {
                                    return docx.getFootnotesPart();
                                } catch (Docx4JException e) {
                                    e.printStackTrace();
                                    return docx.getDocxDocument();
                                }
                            }

                            @Override
                            public List<Object> getContent() {
                                return ftnEdn.getContent();
                            }

                            @Override
                            public Object getLastContentElement() {
                                final List<Object> content = getContent();
                                return content != null && content.size() > 0 ? content.get(content.size() - 1) : null;
                            }

                            @Override
                            public void addContentElement(final Object element) {
                                getContent().add(element);
                            }
                        });
                        docx.renderChildren(footnoteBlock);
                    }
                });
            }
        } catch (Docx4JException e) {
            e.printStackTrace();
        }
    }
}
Also used : Docx4JException(org.docx4j.openpackaging.exceptions.Docx4JException) org.docx4j.wml(org.docx4j.wml) BigInteger(java.math.BigInteger) FootnoteBlock(com.vladsch.flexmark.ext.footnotes.FootnoteBlock)

Aggregations

Docx4JException (org.docx4j.openpackaging.exceptions.Docx4JException)19 WordprocessingMLPackage (org.docx4j.openpackaging.packages.WordprocessingMLPackage)10 File (java.io.File)7 IOException (java.io.IOException)4 SpecExample (com.vladsch.flexmark.spec.SpecExample)3 FileOutputStream (java.io.FileOutputStream)3 Save (org.docx4j.openpackaging.io3.Save)3 Node (com.vladsch.flexmark.ast.Node)2 DocxRenderer (com.vladsch.flexmark.docx.converter.internal.DocxRenderer)2 Parser (com.vladsch.flexmark.parser.Parser)2 FileNotFoundException (java.io.FileNotFoundException)2 BigInteger (java.math.BigInteger)2 Load3 (org.docx4j.openpackaging.io3.Load3)2 UnzippedPartStore (org.docx4j.openpackaging.io3.stores.UnzippedPartStore)2 ZipPartStore (org.docx4j.openpackaging.io3.stores.ZipPartStore)2 OpcPackage (org.docx4j.openpackaging.packages.OpcPackage)2 GeometryFactory (com.revolsys.geometry.model.GeometryFactory)1 FootnoteBlock (com.vladsch.flexmark.ext.footnotes.FootnoteBlock)1 TrpDoc (eu.transkribus.core.model.beans.TrpDoc)1 ExportCache (eu.transkribus.core.model.builder.ExportCache)1