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;
}
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();
}
}
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();
}
}
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();
}
}
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();
}
}
}
Aggregations