When filling text into a word template, how can I automatically fill in the remaining text into the next cell after one cell is filled?

If I have a very long text that needs to be filled into this word template, how can I automatically fill the first page and then fill the remaining text into the second page?
word模板.docx (13.9 KB)

@DengPan1993 MS Word documents are flow documents by their nature, so in general no additional actions are required to achieve what you need. But in your template there are two tables, on different pages, which imitate page border. To achieve what you need you should simply modify your template to use actual page borders instead of tables: in.docx (12.3 KB)

I’ll add my requirements. This is only a part of an application form with strict formatting rules. It consists of three pages, with a 1x1 table on each page. The content should not exceed the boundaries of the table; any excess content will automatically flow to the next page. Additionally, each page should have a title (I’ll use “Title” to represent it).
image.png (1.7 KB)

word模板.docx (11.3 KB)

@DengPan1993 I am afraid there is no way to achieve this using tree tables. Instead you can use one table. Please see the following modified template, which has one table that will grow to the next page:
in.docx (12.9 KB)

Document doc = new Document("C:\\Temp\\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveTo(doc.getFirstSection().getBody().getTables().get(0).getLastRow().getFirstCell().getFirstParagraph());
for (int i = 0; i < 200; i++)
{
    builder.write("This is some dummy text");
}
doc.save("C:\\Temp\\out.docx");

here is the output:
out.docx (10.7 KB)

Why can your Word template automatically copy a new page template when one page is filled with content, but the template I sent you cannot. Another question is: Can aspose words automatically change font size and line spacing to fit cell size? (The cell size is fixed and cannot be changed, and the content length is variable)

@DengPan1993 In the modified template row height is specified as at least:


So the row height grows if more content is added. In this case the content is moved to the next page, by actually it is still one cell. The header is configured as header row of the table, so it is repeated on each page.

There is no direct way to achieve this. Theoretically, you can try calculating actual bounds of content using LayoutCollector and LayoutEnumerator and then check whether content fits the cell rectangle. But such approach is much less efficient and much more error prone than the approach suggested above. So I would not recommend to use it.

Thank you very much for your answer

1 Like