Hi There,
I have a template which has hierarchical tables and inside one of the table I have bookmark for repeating merge field.
I am using below code to replace bookmark with tableStart and tableEnd.
public Document replaceTagForBookmarkWithTables(String inputFileName, String tableName, Document doc) throws Exception {
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToDocumentEnd();
builder.insertBreak(BreakType.LINE_BREAK);
// Get All Sections(Pages) in the document
SectionCollection sections = doc.getSections();
Section section = null;
// Iterate over each section.
for (int sectionIndex = 0; sectionIndex < sections.getCount(); sectionIndex++) {
section = doc.getSections().get(sectionIndex);
// Get all tables in the document.
TableCollection tables = section.getBody().getTables();
Table table = null;
// iterate on the tables
for (int tableIndex = 0; tableIndex < tables.getCount(); tableIndex++) {
table = tables.get(tableIndex);
RowCollection rows = table.getRows();
Row row = null;
// Iterate on the table rows.
for (int rowIndex = 0; rowIndex < rows.getCount(); rowIndex++) {
row = rows.get(rowIndex);
CellCollection cells = row.getCells();
Cell cell = null;
// Iterate on the cells in a row (i.e.,table Columns)
for (int cellIndex = 0; cellIndex < cells.getCount(); cellIndex++) {
cell = cells.get(cellIndex);
TableCollection innerTable = cell.getTables();
// Get all the bookmarks in the cell range
BookmarkCollection bookmarks = cell.getRange().getBookmarks();
if (bookmarks != null && bookmarks.getCount() > 0) {
// Get the first bookmark and insert the new merge field in the first cell and
// the last cell
// and remove the bookmark from the document.
Bookmark bookmark = bookmarks.get(0);
BookmarkStart bms = cell.getRange().getBookmarks().get(0).getBookmarkStart();
BookmarkEnd bme = cell.getRange().getBookmarks().get(0).getBookmarkEnd();
CompositeNode bmsParent = bms.getParentNode().getParentNode();
CompositeNode bmeParent = bme.getParentNode().getParentNode();
if (bookmark != null) {
String bookmarkName = bookmark.getName();
tableName = bookmarkName;
// Get the cursor position to the beginning of the first cell and insert the new
// merge field.
builder.moveToCell(tableIndex, rowIndex, cellIndex, 0);
builder.insertField("MERGEFIELD TableStart:" + tableName + " \\* MERGEFORMAT",
"«TableStart:" + tableName + "»");
if (bmsParent != null && bmeParent != null && bmsParent == bmeParent) {
builder.moveToCell(tableIndex, rowIndex, cellIndex, -1);
}else {
builder.moveToCell(tableIndex, rowIndex, cells.getCount() - 1, -1);
}
builder.insertField("MERGEFIELD TableEnd:" + tableName + " \\* MERGEFORMAT", "+ tableName + ");
bookmark.remove();
}
}
}
}
}
}
return doc;
}
I am getting below exception for my template
java.lang.IllegalStateException: Mail merge region ‘bBookMark’ is badly formed. TableStart and TableEnd should be in the same section, same table row or same table cell.
HTML Template contains structure similar to:
<html>
<head>
</head>
<body lang=EN-GB style='tab-interval:36.0pt;word-wrap:break-word'>
<div class=WordSection1>
<table>
<tr>
<td>
<table>
<tr>
<td>
<table>
<tr>
<td>This is test template format for testing repeating merge field</td>
</tr>
<tr>
<td>
<table align='center'>
<tr>
<td>
<p class=MsoNormal>
<a name=bBookMark></a>
<!--[if supportFields]><span
style='mso-bookmark:bBookMark'></span><span style='mso-element:field-begin'></span><span
style='mso-bookmark:bBookMark'><span style='mso-fareast-font-family:
"Times New Roman"'><span style='mso-spacerun:yes'> </span>MERGEFIELD<span
style='mso-spacerun:yes'> </span>firstName<span
style='mso-spacerun:yes'> </span>\* MERGEFORMAT <span
style='mso-element:field-separator'></span></span></span><![endif]-->
<span style='mso-bookmark:bBookMark'>
<span style='mso-fareast-font-family:
"Times New Roman"'><span style='mso-no-proof:yes'>«firstName»</span></span>
</span>
<!--[if supportFields]><span
style='mso-bookmark:bBookMark'></span><span style='mso-element:field-end'></span><![endif]-->
<span style='mso-bookmark:bBookMark'>
<span style='mso-fareast-font-family:
"Times New Roman"'><o:p></o:p></span>
</span>
</p>
</td>
<span style='mso-bookmark:bBookMark'></span>
<td width=24 valign=top style='width:18.0pt;border:solid windowtext 1.0pt;
border-left:none;mso-border-left-alt:solid windowtext .5pt;mso-border-alt:
solid windowtext .5pt;padding:0mm 5.4pt 0mm 5.4pt'>
<p class=MsoNormal>
<!--[if supportFields]><span style='mso-fareast-font-family:
"Times New Roman"'><span style='mso-element:field-begin'></span><span
style='mso-spacerun:yes'> </span>MERGEFIELD<span
style='mso-spacerun:yes'> </span>lastName<span
style='mso-spacerun:yes'> </span>\* MERGEFORMAT <span
style='mso-element:field-separator'></span></span><![endif]-->
<span style='mso-fareast-font-family:"Times New Roman"'>
<span style='mso-no-proof:yes'>«lastName»</span>
</span>
<!--[if supportFields]><span
style='mso-fareast-font-family:"Times New Roman"'><span
style='mso-element:field-end'></span></span><![endif]-->
<span style='mso-fareast-font-family:"Times New Roman"'><o:p></o:p></span>
</p>
</td>
</tr>
<tr>
<td>
<span style='mso-bookmark:bBookMark'></span>
<p class=MsoNormal>
<span style='mso-bookmark:bBookMark'>
<span style='mso-fareast-font-family:"Times New Roman"'><o:p> </o:p></span>
</span>
</p>
</td>
<span style='mso-bookmark:bBookMark'></span>
<td>
<p class=MsoNormal><span style='mso-fareast-font-family:"Times New Roman"'><o:p> </o:p></span></p>
</td>
</tr>
</table>
<span style='mso-bookmark:bBookMark'></span>
</td>
<span style='mso-bookmark:bBookMark'></span>
</tr>
</table>
</td>
<td style='padding:.75pt .75pt .75pt .75pt'></td>
</tr>
<tr style='mso-yfti-irow:1;mso-yfti-lastrow:yes'>
<td style='padding:.75pt .75pt .75pt .75pt'>
<table class=MsoNormalTable border=1 cellpadding=0 style='mso-cellspacing:
1.5pt;border:solid windowtext 1.0pt;mso-border-alt:solid windowtext .5pt;
mso-yfti-tbllook:1184;mso-border-insideh:.5pt solid windowtext;mso-border-insidev:
.5pt solid windowtext'>
<tr style='mso-yfti-irow:0;mso-yfti-firstrow:yes;mso-yfti-lastrow:yes'>
<td style='border:solid windowtext 1.0pt;mso-border-alt:solid windowtext .5pt;
padding:.75pt .75pt .75pt .75pt'>
<p class=MsoNormal><span style='mso-fareast-font-family:"Times New Roman"'>Some Text at the bottom<o:p></o:p></span></p>
</td>
</tr>
</table>
</td>
<td style='padding:.75pt .75pt .75pt .75pt'></td>
</tr>
</table>
</td>
</tr>
</table>
<p class=MsoNormal><span style='mso-fareast-font-family:"Times New Roman"'><o:p> </o:p></span></p>
</div>
</body>
</html>