Search in sources :

Example 31 with WordprocessingMLPackage

use of org.docx4j.openpackaging.packages.WordprocessingMLPackage in project tutorials by eugenp.

the class Docx4jExample method isTextExist.

boolean isTextExist(String testText) throws Docx4JException, JAXBException {
    File doc = new File("helloWorld.docx");
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(doc);
    MainDocumentPart mainDocumentPart = wordMLPackage.getMainDocumentPart();
    String textNodesXPath = "//w:t";
    List<Object> paragraphs = mainDocumentPart.getJAXBNodesViaXPath(textNodesXPath, true);
    for (Object obj : paragraphs) {
        Text text = (Text) ((JAXBElement) obj).getValue();
        String textValue = text.getValue();
        if (textValue != null && textValue.contains(testText)) {
            return true;
        }
    }
    return false;
}
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 32 with WordprocessingMLPackage

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

the class DocxConverterCommonMark method main.

public static void main(String[] args) {
    final String markdown = "### header\n" + "1. List item started from 1\n" + "### header \n" + "1. List item started from 1\n" + "1. List item continued 2\n" + "### test\n" + "1. List item started from 1\n" + "";
    System.out.println("markdown\n");
    System.out.println(markdown);
    final Parser PARSER = Parser.builder(OPTIONS).build();
    final DocxRenderer RENDERER = DocxRenderer.builder(OPTIONS).build();
    Node document = PARSER.parse(markdown);
    // to get XML
    String xml = RENDERER.render(document);
    // or to control the package
    final WordprocessingMLPackage template = DocxRenderer.getDefaultTemplate();
    RENDERER.render(document, template);
    File file = new File("/Users/vlad/src/pdf/flexmark-java-issue-176.docx");
    try {
        template.save(file, Docx4J.FLAG_SAVE_ZIP_FILE);
    } catch (Docx4JException e) {
        e.printStackTrace();
    }
}
Also used : Node(com.vladsch.flexmark.ast.Node) DocxRenderer(com.vladsch.flexmark.docx.converter.internal.DocxRenderer) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) File(java.io.File) Docx4JException(org.docx4j.openpackaging.exceptions.Docx4JException) Parser(com.vladsch.flexmark.parser.Parser)

Example 33 with WordprocessingMLPackage

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

the class ComboDocxConverterIssuesSpecTest method fullTestSpecComplete.

@Override
protected void fullTestSpecComplete() {
    if (!DUMP_ALL_TESTS_FILES || myPackage == null)
        return;
    // write it out to file, hard-coded for now                    IGNORE
    File file = new File(String.format("%s%s.docx", PROJECT_ROOT_DIRECTORY, FILE_ALL_TESTS_DUMP_NAME));
    File file2 = new File(String.format("%s%s.xml", PROJECT_ROOT_DIRECTORY, FILE_ALL_TESTS_DUMP_NAME));
    WordprocessingMLPackage mlPackage = myPackage;
    try {
        mlPackage.save(file, Docx4J.FLAG_SAVE_ZIP_FILE);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        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();
    }
}
Also used : WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) Docx4JException(org.docx4j.openpackaging.exceptions.Docx4JException)

Example 34 with WordprocessingMLPackage

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

the class ComboDocxConverterSpecTest method fullTestSpecComplete.

@Override
protected void fullTestSpecComplete() {
    if (!DUMP_ALL_TESTS_FILES || myPackage == null)
        return;
    // write it out to file, hard-coded for now                    IGNORE
    File file = new File(String.format("%s%s.docx", PROJECT_ROOT_DIRECTORY, FILE_ALL_TESTS_DUMP_NAME));
    File file2 = new File(String.format("%s%s.xml", PROJECT_ROOT_DIRECTORY, FILE_ALL_TESTS_DUMP_NAME));
    WordprocessingMLPackage mlPackage = myPackage;
    try {
        mlPackage.save(file, Docx4J.FLAG_SAVE_ZIP_FILE);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        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();
    }
}
Also used : WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) Docx4JException(org.docx4j.openpackaging.exceptions.Docx4JException)

Example 35 with WordprocessingMLPackage

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

the class ComboDocxConverterSpecTest 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);
        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)

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