below is my code:-
public void replaceMergeFields(Map<String, Map<String, String>> regularAttributeValues,
Map<String, Map<String, String>> customAttributeValues, ReportParamsBean paramBean, String fileName) {
try {
FindReplaceOptions findReplaceOptions = new FindReplaceOptions();
findReplaceOptions.setReplacingCallback(new FindAndInsertHtml());
findReplaceOptions.setPreserveMetaCharacters(true);
UserBean userBean = paramBean.getUserBean();
TemplateBean item = paramBean.getTemplateBean();
final Map<String, String> customFields = remote.getCustomFields(userBean);
final List<String> htmlFields = getHtmlFields(customFields);
com.aspose.words.Document destDoc = new com.aspose.words.Document();
DocumentBuilder builder = new DocumentBuilder(destDoc);
builder.getCurrentParagraph().getParagraphFormat().setPageBreakBefore(true);
final NodeCollection childNodes = destDoc.getChildNodes();
childNodes.removeAt(0);
CompositeNode destCompNode = childNodes.getContainer();
com.aspose.words.Document tempDoc = null;
Map<String, String> htmlPlaceHolders = new HashMap<>();
int billCount = 0, index = 0, billCounter = 0, customAttrCounter = 1;
for (Map.Entry<String, Map<String, String>> entrySet : regularAttributeValues.entrySet()) {
try {
billCounter++;
customAttrCounter = 1;
tempDoc = new com.aspose.words.Document(new ByteArrayInputStream(item.getTemplateDocument()));
NodeImporter importer = new NodeImporter(tempDoc.getDocument(), destDoc.getDocument(), ImportFormatMode.KEEP_SOURCE_FORMATTING);
String key = entrySet.getKey();
Map<String, String> regularValues = entrySet.getValue();
Map<String, String> customValues = customAttributeValues.get(key);
List<String> fieldNames = new LinkedList<>();
List<String> values = new LinkedList<>();
if (regularValues != null) {
for (Map.Entry<String, String> regularEntrySet : regularValues.entrySet()) {
String key1 = regularEntrySet.getKey();
String value1 = regularEntrySet.getValue();
if (StringUtils.startsWith(key1, "bill_title") || StringUtils.startsWith(key1, "bill_digest")) {
htmlPlaceHolders.put(key1, transformBillXmlXsl(value1, key1));
} else {
fieldNames.add(key1);
values.add(value1);
}
}
}
if (customValues != null) {
for (Map.Entry<String, String> customEntrySet : customValues.entrySet()) {
String customKey = customEntrySet.getKey();
String customValue = customEntrySet.getValue();
if (htmlFields.contains(customKey)) {
htmlPlaceHolders.put(customKey + "_html", customValue);
} else {
fieldNames.add(customKey);
values.add(customValue);
}
}
}
if (htmlPlaceHolders.size() > 0) {
for (Map.Entry<String, String> htmlSet : htmlPlaceHolders.entrySet()) {
String placeHolder = htmlSet.getKey();
String replaceHtml = "";
if (StringUtils.isBlank(htmlSet.getValue())) {
replaceHtml = "<span></span>";
} else {
replaceHtml = htmlSet.getValue();
if (StringUtils.equals("bill_title_html", placeHolder) || StringUtils.equals("bill_digest_html", placeHolder)) {
replaceHtml = StringUtils.replace(replaceHtml, "<br>", "<br/>");
}
}
tempDoc.getRange().replace(Pattern.compile(placeHolder), replaceHtml, findReplaceOptions);
}
}
tempDoc.getMailMerge().setCleanupOptions(MailMergeCleanupOptions.REMOVE_UNUSED_FIELDS);
tempDoc.getMailMerge().execute(fieldNames.toArray(new String[fieldNames.size()]),
values.toArray(new String[values.size()]));
if (paramBean.isDocFieldsExists()) {
boolean summaryDone = true, fiscalDone = true;
String summaryDocFieldName = "", fiscalDocFieldName = "";
for (String docField : paramBean.getDocFields()) {
if (StringUtils.equalsIgnoreCase(docField, "summary_doc")) {
summaryDone = false;
summaryDocFieldName = docField;
} else if (StringUtils.equalsIgnoreCase(docField, "fiscal_doc")) {
fiscalDone = false;
fiscalDocFieldName = docField;
}
}
String analysisType = getAnalysisType(key);
final byte[] analysisFile = remote.getAnalysisFile(userBean, key, analysisType);
if (analysisFile != null) {
com.aspose.words.Document analysisDoc = new com.aspose.words.Document(new ByteArrayInputStream(analysisFile));
final NodeCollection aChildNodes = analysisDoc.getChildNodes(NodeType.PARAGRAPH, true);
final List<Node> fiscalNodes = findMatch(aChildNodes, "FISCAL EFFECT:");
final List<Node> summaryNodes = findMatch(aChildNodes, "SUMMARY:");
final DocumentBuilder tBuilder = new DocumentBuilder(tempDoc);
final NodeCollection tChildNodes = tempDoc.getChildNodes(NodeType.PARAGRAPH, true);
Node labelNode = null;
for (Iterator<Node> iterator = tChildNodes.iterator(); iterator.hasNext();) {
Node replaceNode = iterator.next();
final int nodeType = replaceNode.getNodeType();
tBuilder.moveTo(replaceNode);
CompositeNode destNode = replaceNode.getParentNode().getContainer();
NodeImporter analysisImporter = new NodeImporter(analysisDoc, replaceNode.getDocument(), ImportFormatMode.USE_DESTINATION_STYLES);
if (StringUtils.startsWith(replaceNode.getText(), summaryDocFieldName)) {
labelNode = replaceNode;
if (summaryNodes != null) {
for (Node summaryNode : summaryNodes) {
final Node newNode = analysisImporter.importNode(summaryNode, true);
destNode.insertAfter(newNode, replaceNode);
replaceNode = newNode;
}
//labelNode.remove();
summaryDone = true;
} else {
tempDoc.getRange().replace(Pattern.compile(summaryDocFieldName), "", findReplaceOptions);
summaryDone = true;
}
} else if (StringUtils.startsWith(replaceNode.getText(), fiscalDocFieldName)) {
labelNode = replaceNode;
if (fiscalNodes != null) {
for (Node fiscalNode : fiscalNodes) {
final Node newNode = analysisImporter.importNode(fiscalNode, true);
destNode.insertAfter(newNode, replaceNode);
replaceNode = newNode;
}
//labelNode.remove();
fiscalDone = true;
} else {
tempDoc.getRange().replace(Pattern.compile(fiscalDocFieldName), "", findReplaceOptions);
fiscalDone = true;
}
}
if (summaryDone && fiscalDone) {
break;
}
}
} else {
if(StringUtils.isNotEmpty(summaryDocFieldName)) {
tempDoc.getRange().replace(Pattern.compile(summaryDocFieldName), "", findReplaceOptions);
}
if(StringUtils.isNotEmpty(fiscalDocFieldName)) {
tempDoc.getRange().replace(Pattern.compile(fiscalDocFieldName), "", findReplaceOptions);
}
}
}
final NodeCollection tableNodes = tempDoc.getChildNodes(NodeType.TABLE, true);
Node newNode = null;
if (tableNodes.getCount() > 0) {
if (billCount++ > 0) {
for (Iterator<Node> iterator = tableNodes.iterator(); iterator.hasNext();) {
Table node = (Table) iterator.next();
final Node child = node.getChild(NodeType.ROW, 0, true);
node.removeChild(child);
break;
}
if (!paramBean.isBillOnNewPage()) {
final NodeCollection rowNodes = tempDoc.getChildNodes(NodeType.ROW, true);
final NodeCollection parentTableNodes = destDoc.getChildNodes(NodeType.TABLE, true);
if (parentTableNodes.getCount() > 0) {
final Table parentTable = (Table) parentTableNodes.get(0);
destCompNode = parentTable.getChildNodes().getContainer();
for (Iterator<Node> iterator = rowNodes.iterator(); iterator.hasNext();) {
Node rowNode = (Node) iterator.next();
newNode = importer.importNode(rowNode, true);
destCompNode.appendChild(newNode);
}
}
} else {
final NodeCollection tempChildNodes = tempDoc.getChildNodes();
for (Iterator<Node> iterator = tempChildNodes.iterator(); iterator.hasNext();) {
Node childNode = iterator.next();
newNode = importer.importNode(childNode, true);
destCompNode.appendChild(newNode);
}
}
} else {
final NodeCollection tempChildNodes = tempDoc.getChildNodes();
for (Iterator<Node> iterator = tempChildNodes.iterator(); iterator.hasNext();) {
Node childNode = iterator.next();
newNode = importer.importNode(childNode, true);
destCompNode.appendChild(newNode);
}
}
}
if (paramBean.isBillOnNewPage() && billCounter != regularAttributeValues.size()) {
builder.moveToDocumentEnd();
}
if (index++ == 0) {
try {
mergeHeaderFooter(tempDoc, destDoc, HeaderFooterType.HEADER_PRIMARY);
mergeHeaderFooter(tempDoc, destDoc, HeaderFooterType.HEADER_FIRST);
mergeHeaderFooter(tempDoc, destDoc, HeaderFooterType.HEADER_EVEN);
mergeHeaderFooter(tempDoc, destDoc, HeaderFooterType.FOOTER_PRIMARY);
mergeHeaderFooter(tempDoc, destDoc, HeaderFooterType.FOOTER_FIRST);
mergeHeaderFooter(tempDoc, destDoc, HeaderFooterType.FOOTER_EVEN);
} catch (Exception ex) {
Logger.getLogger(Docx4jWriteHelper.class.getName()).log(Level.SEVERE, null, ex);
}
}
} catch (Exception ex) {
Logger.getLogger(Docx4jWriteHelper.class.getName()).log(Level.SEVERE, null, ex);
}
}
destDoc.save(fileName);
} catch (Exception ex) {
Logger.getLogger(Docx4jWriteHelper.class.getName()).log(Level.SEVERE, null, ex);
}
}