use of org.docx4j.model.datastorage.OpenDoPEHandler in project Java-Tutorial by gpcodervn.
the class ContentControlBindingExtensions method main.
/**
* @param args
*/
public static void main(String[] args) throws Exception {
String inputfilepath = System.getProperty("user.dir") + "/sample-docs/word/databinding/invoice.docx";
String data = System.getProperty("user.dir") + "/sample-docs/word/databinding/invoice-data.xml";
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));
filepathprefix = inputfilepath.substring(0, inputfilepath.lastIndexOf("."));
System.out.println(filepathprefix);
StringBuilder timingSummary = new StringBuilder();
// Find custom xml item id and inject data_file.xml
long startTime = System.currentTimeMillis();
CustomXmlDataStoragePart customXmlDataStoragePart = CustomXmlDataStoragePartSelector.getCustomXmlDataStoragePart(wordMLPackage);
if (customXmlDataStoragePart == null) {
throw new RuntimeException("no xml");
}
customXmlDataStoragePart.getData().setDocument(new FileInputStream(new File(data)));
long endTime = System.currentTimeMillis();
timingSummary.append("\nmerge data: " + (endTime - startTime));
System.out.println("data merged");
SaveToZipFile saver = new SaveToZipFile(wordMLPackage);
saver.save(new File(System.getProperty("user.dir") + "/OUT_injected.docx"));
// Process conditionals and repeats
startTime = System.currentTimeMillis();
OpenDoPEHandler odh = new OpenDoPEHandler(wordMLPackage);
odh.preprocess();
endTime = System.currentTimeMillis();
timingSummary.append("OpenDoPEHandler: " + (endTime - startTime));
// System.out.println(
// XmlUtils.marshaltoString(wordMLPackage.getMainDocumentPart().getJaxbElement(), true, true)
// );
saver.save(filepathprefix + "_1_preprocessed.docx");
System.out.println("Saved: " + filepathprefix + "_1_preprocessed.docx");
startTime = System.currentTimeMillis();
OpenDoPEIntegrity odi = new OpenDoPEIntegrity();
odi.process(wordMLPackage);
endTime = System.currentTimeMillis();
timingSummary.append("\nOpenDoPEIntegrity: " + (endTime - startTime));
// System.out.println(
// XmlUtils.marshaltoString(wordMLPackage.getMainDocumentPart().getJaxbElement(), true, true)
// );
saver = new SaveToZipFile(wordMLPackage);
saver.save(filepathprefix + "_2_integrity.docx");
System.out.println("Saved: " + filepathprefix + "_2_integrity.docx");
// Apply the bindings
saver = new SaveToZipFile(wordMLPackage);
BindingHandler.setHyperlinkStyle("Hyperlink");
startTime = System.currentTimeMillis();
// AtomicInteger bookmarkId = odh.getNextBookmarkId();
AtomicInteger bookmarkId = new AtomicInteger();
BindingHandler bh = new BindingHandler(wordMLPackage);
bh.setStartingIdForNewBookmarks(bookmarkId);
bh.applyBindings(wordMLPackage.getMainDocumentPart());
endTime = System.currentTimeMillis();
timingSummary.append("\nBindingHandler.applyBindings: " + (endTime - startTime));
// System.out.println(
// XmlUtils.marshaltoString(wordMLPackage.getMainDocumentPart().getJaxbElement(), true, true)
// );
saver.save(filepathprefix + "_3_bound.docx");
System.out.println("Saved: " + filepathprefix + "_3_bound.docx");
// Either demonstrate reverter, or stripping of controls;
// you can't do both. So comment out one or the other.
// reverter(inputfilepath, filepathprefix + "_bound.docx");
//
// Strip content controls
startTime = System.currentTimeMillis();
RemovalHandler rh = new RemovalHandler();
rh.removeSDTs(wordMLPackage, Quantifier.ALL);
endTime = System.currentTimeMillis();
timingSummary.append("\nRemovalHandler: " + (endTime - startTime));
saver.save(filepathprefix + "_4_stripped.docx");
System.out.println("Saved: " + filepathprefix + "_4_stripped.docx");
System.out.println(timingSummary);
}
use of org.docx4j.model.datastorage.OpenDoPEHandler 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);
}
Aggregations