Cold you please let me know how can I achieve this?
Sample Code which I have written to replace placeholders:
- Code: Select all
ByteArrayOutputStream baos = null;
DataRequestVO dataRequest = null;
WordprocessingMLPackage wordMLPackageMain = null;
try {
dataRequest = reportDAO.fetchDocketData(dataRequestNO);
String templateFile = dataRequest.getReportType().getTemplatePath();
String fileName = dataRequest.getDocketNo() + " - " + dataRequest.getDataRequestNo() + "-" + dataRequest.getReportType().getReportName();
URL url = this.getClass().getClassLoader().getResource(templateFile);
String templateFilePath = url.toURI().getPath();
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new File(url.toURI().getPath()));
ContentTypeManager ctm = wordMLPackage.getContentTypeManager();
CTOverride override = ctm.getOverrideContentType().get(new URI("/word/document.xml"));
if (templateFilePath.endsWith("dotm")) {
override.setContentType(
org.docx4j.openpackaging.contenttype.ContentTypes.WORDPROCESSINGML_DOCUMENT_MACROENABLED);
} else {
override.setContentType(org.docx4j.openpackaging.contenttype.ContentTypes.WORDPROCESSINGML_DOCUMENT);
}
// Now save it
DocketReportHelper.prepare(wordMLPackage);
List<Object> texts = DocketReportHelper.getAllElementFromObject(wordMLPackage.getMainDocumentPart(),
Text.class);
DocketReportHelper.replacePlaceholder(texts, dataRequest.getDocketNo(), PLACEHOLDER_DOCKET_NO);
DocketReportHelper.replacePlaceholder(texts, dataRequest.getQuestion(), PLACEHOLDER_QUESTION); //html code text
DocketReportHelper.replacePlaceholder(texts, dataRequest.getResponse(), PLACEHOLDER_RESPONSE); //html code text
// Confidential Reports
if ("yes".equalsIgnoreCase(dataRequest.getConfidential())) {
// Get Confidential Header Footer template
wordMLPackageMain = createConfHeaderFooter();
List<Object> listParamMain = wordMLPackage.getMainDocumentPart().getContent();
for (Object param : listParamMain) {
if (param instanceof P) {
wordMLPackageMain.getMainDocumentPart().addObject((P) param);
} else if(param instanceof JAXBElement) {
wordMLPackageMain.getMainDocumentPart().addObject((JAXBElement) param);
}
}
} else {
wordMLPackageMain = wordMLPackage;
}
baos = new ByteArrayOutputStream();
SaveToZipFile saver = new SaveToZipFile(wordMLPackageMain);
saver.save(baos);
replacePlaceholder method:
- Code: Select all
static void replacePlaceholder(List<Object> texts, String name, String placeholder) {
log.debug("placeholder : " + placeholder);
for (Object text : texts) {
Text textElement = (Text) text;
if (textElement != null && placeholder.equals(textElement.getValue())) {
textElement.setValue(name);
break;
}
}
}
Prepare method:
- Code: Select all
public static void prepare(WordprocessingMLPackage wmlPackage) throws Exception {
// Apply the filter
WordprocessingMLPackage.FilterSettings filterSettings = new WordprocessingMLPackage.FilterSettings();
filterSettings.setRemoveProofErrors(true);
filterSettings.setRemoveContentControls(true);
filterSettings.setRemoveRsids(true);
wmlPackage.filter(filterSettings);
log.debug(XmlUtils.marshaltoString(wmlPackage.getMainDocumentPart().getJaxbElement(), true, true));
// Now clean up some more
org.docx4j.wml.Document wmlDocumentEl = wmlPackage.getMainDocumentPart().getJaxbElement();
Body body = wmlDocumentEl.getBody();
SingleTraversalUtilVisitorCallback paragraphVisitor = new SingleTraversalUtilVisitorCallback(
new TraversalUtilParagraphVisitor());
paragraphVisitor.walkJAXBElements(body);
log.debug(XmlUtils.marshaltoString(wmlPackage.getMainDocumentPart().getJaxbElement(), true, true));
}