Articles on: Tutorials

How to insert a page break without having a blank page?

Inserting page break without an extra blank page

Integrating a page break to separate elements in a list is a standard practice, as illustrated in the provided DOCX template screenshot. However, a consequential issue arises during document generation—namely, the insertion of a page break for the last element in the loop results in an empty page in the final output. This article gives two solutions to avoid the blank page on the generated document. The provided solutions are predicated on a list denoted as d.list[i] ; users are advised to adapt the nomenclature to align with their JSON structure and list nomenclature.

Screenshot of a DOCX template with a page break inside a loop

Here is a template example as plain text:
Part {d.list[i].id}
----PAGE BREAK----

Solution 1: Insert a "page break" on the first element of the list

To avoid the blank page at the end of the report:
Delete the existing page break at the bottom. A Page Break will be applied on the paragraph of the first tag of the loop.
For Microsoft Word: Select the paragraph of the first loop tag {d.list[i].id} (1) > Right Click >`Paragraph...` > Click on the tab Line and Page breaks (2) > Check the option Page break before (3).
For LibreOffice: Select the paragraph of the first loop tag {d.list[i].id} > Right Click > Paragraph > Paragraph... > Click on the tab Text Flow > Check the option Insert Page Before

Screenshot of a Docx Template with a "Page Break Before" applied on a paragraph

Template as plain text:

Solution 2: Delete the page break conditionally if the last element of the list is printed

This solution deletes the page break if the last element of the list is printed, so that, it won't create a blank page.
The following expression must be placed on the paragraph of the page break: {d.list[i]..list:len():sub(1):ifEQ(.i):drop(p)}

Detail of the expression:
{d.list[i] : Access the list element using the iterator [i], the index i will be used for the condition.
..list:len() : Retrieve the length of the list by backtracking to the parent object.
:sub(1): Subtract one from the list length, accounting for zero-based indexing. (The list length is counted from one, however the index from [i] is starting from zero.)
:ifEQ(.i):drop(p)}: Delete the paragraph, including the page break, if the current index .i is equal to the list size, indicating the last element.

Screenshot of a DOCX template including a condition to delete the page break

The template as plain text:
Part {d.list[i].id}
{d.list[i]..list:len():sub(1):ifEQ(.i):drop(p)} ----PAGE BREAK----

Updated on: 03/06/2024

Was this article helpful?

Share your feedback


Thank you!