A forced page break in TX Text Control is a feature that allows you to manually insert a page break at a specific input position in a document. This forces the content following the break to begin on a new page, regardless of the natural flow of the text.

A forced page break can be added using the CTRL+ENTER key combination or programmatically using the following code.

textControl1.Selection.Text = "\f";
view raw test.cs hosted with ❤ by GitHub

While this can be useful for creating documents quickly, it poses many problems when creating templates that are used to populate dynamic content. A template is a document that contains placeholders for dynamic content. When the template is loaded into TX Text Control, the placeholders are replaced with the actual content.

In the visual document editor of TX Text Control, the forced page break is displayed as a horizontal line. This is a visual indicator that a page break is inserted at this position.

Page Breaks in TX Text Control

Forced Page Breaks in Templates

Now consider a template where longer text is dynamically inserted into a merge field, followed by a forced page break. The following screenshot shows a template with a merge field and a forced page break inserted after it.

Page Breaks in TX Text Control

During the merge process, the merge field is replaced with the actual content. The forced page break is still present in the document, and the content is longer and breaks across pages. The following screenshot shows the result of the merge process.

Page Breaks in TX Text Control

Now, the forced page break is moved to the next page which results in an empty page. This is not the desired behavior when creating templates for dynamic content.

Page Breaks in TX Text Control

Avoiding Forced Page Breaks

When creating templates for dynamic content, it is important to avoid forced page breaks. Instead, use a paragraph setting that inserts a page break before the specified paragraph. Usually you want to start a new heading or chapter on a new page. In this case, use the "page break before" setting that can be defined for a paragraph.

This can be set in the Paragraph settings dialog.

Page Breaks in TX Text Control

Programmatically, this can be set using the following code snippet:

textControl1.Selection.ParagraphFormat.PageBreakBefore = true;
view raw test.cs hosted with ❤ by GitHub

When this setting is added to a paragraph and the control characters are enabled, the setting is indicated by a small centered black dot at the beginning of the paragraph.

Page Breaks in TX Text Control

When merging dynamic content into a template, the defined chapter now always starts on a new page and no extra blank pages are accidentally added.

Use Stylesheets

When creating templates for dynamic content, it is recommended to use stylesheets to define the formatting of the content. This includes the paragraph settings such as the "page break before" setting. Stylesheets can be used to define the formatting of the content and the paragraph settings.

Page Breaks in TX Text Control

Programmatically, this can be set using the following code snippet:

TXTextControl.ParagraphStyle paragraphStyle = new TXTextControl.ParagraphStyle("Chapter");
paragraphStyle.ParagraphFormat.PageBreakBefore = true;
view raw test.cs hosted with ❤ by GitHub

Conclusion

Forced page breaks are useful when creating documents manually, but they can cause issues when creating templates for dynamic content. It is recommended to use paragraph settings such as "page break before" to avoid empty pages when merging dynamic content into a template.

Stylesheets can be used to define the formatting of the content and the paragraph settings. This allows you to create templates that are used to populate dynamic content without the need for manual adjustments.