I am updating an existing document file by replacing a word with a table using IReplacingCallback interface. The table is building as expected but is created on the next page. I am attaching the input document template.docx (15.5 KB), the sample code for reproducing TblPrintNextPg.zip (2.5 KB) and also attaching the output file output.docx (14.5 KB).
@Gptrnt The problem occurs because all rows in the table has heading format set. You should modify your code to reset this flag for not heading rows. Please see the following modified code:
public void table () throws Exception {
try {
builder.startTable();
insertRow("Sl.No.","Names of the Companies ","Test Content one ","Test Content two","Test Content three", true);
builder.getRowFormat().setAllowBreakAcrossPages(false);
insertMergeRow("Group one", false);
for (int i = 0; i < 10; i++){
insertRow(String.valueOf(i+1),"Name " + i , "Designation " + i, "Role" , "Attendance " +i,false);
}
insertMergeRow("Group Two", false);
for (int i = 0; i < 5; i++){
insertRow(String.valueOf(i+1),"Name " + i , "Designation " + i, "Role" , "Attendance " +i,false);
}
insertMergeRow("Group Three", false);
for (int i = 0; i < 5; i++){
insertRow(String.valueOf(i+1),"Name " + i , "Designation " + i, "Role" , "Attendance " +i,false);
}
insertMergeRow("Group Four", false);
for (int i = 0; i < 5; i++){
insertRow(String.valueOf(i+1),"Name " + i , "Designation " + i, "Role" , "Attendance " +i,false);
}
Table table = builder.endTable();
table.autoFit(AutoFitBehavior.AUTO_FIT_TO_WINDOW);
}catch (Exception ex) {
}
}
private void insertRow(String slNo, String c2, String c3, String c4, String c5, boolean heading) throws Exception {
if (heading){
builder.getRowFormat().setHeadingFormat(true);
builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
builder.getParagraphFormat().setSpaceAfter(10);
builder.getFont().setBold(true);
}
else
{
builder.getRowFormat().setHeadingFormat(false);
}
builder.insertCell();
builder.getRowFormat().setAllowBreakAcrossPages(false);
builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(10));
builder.write(slNo);
builder.insertCell();
builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(22));
builder.insertHtml(c2,USE_BUILDER_FORMATTING);
builder.insertCell();
builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(22));
builder.insertHtml(c3,USE_BUILDER_FORMATTING);
builder.insertCell();
builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(22));
builder.insertHtml(c4,USE_BUILDER_FORMATTING);
builder.insertCell();
builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(22));
builder.insertHtml(c5,USE_BUILDER_FORMATTING);
builder.endRow();
}
The above solution is working fine for the issue. However, I am facing another issue with the same sample document if I use another template template.docx (32.8 KB) then I am getting the output output.pdf (32.3 KB). As u can see I have added the replacing word with some left indentation, which does not take the replacing table instead it takes the paragraph inside the table cell. I want to create the table left indentation added in the replacement word and also remove the indentation taken inside the table cell paragraph.
@Gptrnt Table and paragraph indentations are different options. If it is required to specify table indentation you should use Table.LeftIndent property.