I’ve written up a condensed version of my code that displays the issue. It looks like the issue is related to the copyDocument method. I believe I found that method here on the forums before, but I can’t find exactly where I got it from. If I skip the copyDocument part it works correctly, but breaks if it’s in there.
package com.jamasoftware.contour.report.util;
import java.util.ArrayList;
import java.util.Random;
import com.aspose.words.ControlChar;
import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.ImportFormatMode;
import com.aspose.words.Node;
import com.aspose.words.NodeType;
import com.aspose.words.Paragraph;
import com.aspose.words.Run;
import com.aspose.words.Section;
import com.aspose.words.SectionStart;
public class DemonstrateTableIssue {
public static Document demonstrateIssue() throws Exception {
Document finalDocument = new Document();
Document templateDoc = new Document();
populateTemplateWithTable(templateDoc);
for (int i = 0; i < 4; i++) {
Document tempDoc = copyDocument(templateDoc);
cleanEmptyParagraphs(tempDoc);
finalDocument.appendDocument(tempDoc, ImportFormatMode.USE_DESTINATION_STYLES);
}
removeContinuousSectionBreak(finalDocument);
return finalDocument;
}
private static void populateTemplateWithTable(Document document) {
DocumentBuilder docBuild = new DocumentBuilder(document);
String style = docBuild.getParagraphFormat().getStyleName();
docBuild.getParagraphFormat().setStyleName(“Heading 1”);
docBuild.writeln(“HEADER”);
docBuild.getParagraphFormat().setStyleName(style);
docBuild.getParagraphFormat().setKeepTogether(true);
docBuild.startTable();
for (int i = 0; i < 10; i++) {
String column1Value = generateRandomString();
String column2Value = generateRandomString();
docBuild.insertCell();
docBuild.getCellFormat().setWidth(250);
docBuild.write(column1Value);
docBuild.insertCell();
docBuild.getCellFormat().setWidth(250);
docBuild.write(column2Value);
docBuild.endRow();
}
docBuild.endTable();
docBuild.getRowFormat().clearFormatting();
// docBuild.write(“static text”);//Uncomment this line to make tables work correctly
}
private static final String AB = “0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz”;
private static String generateRandomString() {
StringBuilder sb = new StringBuilder(25);
for (int i = 0; i < 25; i++) {
sb.append(AB.charAt(new Random().nextInt(AB.length())));
}
return sb.toString();
}
private static void removeContinuousSectionBreak(Document docPrequal) {
// Create a paragraph with page break.
// We will insert this paragraph ad the end of section and then append content of the next section to the current.
Paragraph breakParagraph = new Paragraph(docPrequal);
breakParagraph.appendChild(new Run(docPrequal, ControlChar.PAGE_BREAK));
breakParagraph.getParagraphBreakFont().setSize(1);
breakParagraph.getParagraphFormat().setSpaceAfter(0);
breakParagraph.getParagraphFormat().setSpaceBefore(0);
ArrayList list = new ArrayList();
Section firstSection = docPrequal.getFirstSection();
for (Section section : docPrequal.getSections()) {
if (section.getPageSetup().getSectionStart() == SectionStart.CONTINUOUS && section != firstSection) {
list.add(section);
firstSection.appendContent(section);
}
else if (section.getPageSetup().getSectionStart() == SectionStart.NEW_PAGE && section != firstSection) {
// Append paragraph with page break.
firstSection.getBody().appendChild(breakParagraph.deepClone(true));
list.add(section);
firstSection.appendContent(section);
}
else {
firstSection = section;
}
}
for (Section section : list) {
docPrequal.removeChild(section);
}
}
private static void cleanEmptyParagraphs(Document doc) {
Node[] paragraphs = doc.getChildNodes(NodeType.PARAGRAPH, true).toArray();
for (Node paragraph : paragraphs) {
try {
if (!((Paragraph) paragraph).hasChildNodes())
paragraph.remove();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
private static Document copyDocument(Document doc) throws Exception {
Document newDoc = new Document();
newDoc.appendDocument(doc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
for (Section s : newDoc.getSections()) {
for (Paragraph p : s.getBody().getParagraphs()) {
p.getParagraphFormat().setOutlineLevel(3);
}
s.getPageSetup().setSectionStart(SectionStart.CONTINUOUS);
}
return newDoc;
}
}