Telerik blogs

Have a look at the new Page Numbering fields in the WordsProcessing library and how to implement them.

In the R3 2022 Release for RadWordsProccessing from Progress Telerik Document Processing Libraries, we introduced support for five new page numbering type fields: Page, PageRef, NumPages, SectionPages and Section.

They are commonly used fields for inserting page numbering and their evaluation requires calculating the layout of the document and the size of the document elements. That calculation is done by the NumberingFieldsProvider, which needs to be implemented in order to update the fields.

Inserting Fields

For the default implementation of the NumberingFieldsProvider to work, the Telerik.Documents.Flow.FormatProviders.Pdf.dll assembly should be referenced in the project we are working with.

When it comes to inserting the fields in the document, the easiest and recommended way of doing that is via the RadFlowDocumentEditor’s InsertField method.

We will incorporate all this functionality in a simple project for you to see, but before that here is a brief overview of all the newly added fields:

  • The PAGE field inserts the current page number.
PAGE [*Format Switch]
  • The PAGEREF field inserts the page number of a bookmark for cross-reference.
PAGEREF Bookmark [* Format Switch ] 
  • The SECTION field inserts the number of the current section.
SECTION
  • The SECTIONPAGES field inserts the total number of pages in a section.
SECTIONPAGES
  • The NUMPAGES field inserts the total number of pages in the document.
NUMPAGES

Now let us go together through a couple of examples that demonstrate the implementation of some of those fields in an empty RadFlowDocument:

Example 1: Page & NumPages

The first thing we need to do is create an empty document and a RadFlowDocumentEditor instance for that document, and then register the NumberingFieldsProvider:

RadFlowDocument document = new RadFlowDocument();
RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document);

FlowExtensibilityManager.NumberingFieldsProvider = new NumberingFieldsProvider();

From this point on, the implementation of the various page numbering fields is mostly similar, with a few differences in case we are utilizing the switches for formatting. For example, let’s add text that shows the current page number out of the total number of pages. For this we will need the PAGE and NUMPAGES fields:

editor.InsertText("Page ");
editor.InsertField("PAGE", "");
editor.InsertText(" of ");
editor.InsertField("NUMPAGES", "");

When you are ready with the modifications of the document, you must ensure that the fields are updated, otherwise nothing will be visualized in their place. We can do that by calling the UpdateFields method of the document:

document.UpdateFields();

And after exporting the document the result that the above-provided code snippet produces should be “Page 1 of 1.”

Example 2: PageRef

Let’s do the same thing but this time bring up the page number of a bookmark we have added. To achieve that, we will use the PAGEREF field with a formatting switch:

Bookmark bookmark = editor.InsertBookmark(“MyBookmark");

editor.InsertText("Bookmark Page: ");
editor.InsertField("PAGEREF MyBookmark \h", "");

document.UpdateFields();

The “\h” switch in this case creates a hyperlink to the bookmarked paragraph and the final result ends up being “Bookmark Page: 1.”

If you are curious and want to find out more about the capabilities and functionality of those recently introduced fields as well as some other examples, you can head to the WordsProccessing: Fields documentation and explore further.

Try These New Features out & Share Your Feedback with Us

Regardless if you are new to Telerik Document Processing or already a user, you can head over and download a free trial. Your Telerik Account provides access to the latest bits you might need, or you can just update the NuGet package references in your .NET solutions.

Each input we get, whether big or small, can make the huge difference we are looking for in our growth and progression. So please, feel welcome to look around and share your thoughts in our Feedback Portal or just leave a comment below about your experience.


About the Author

Yoan Karamanov

Yoan was a Technical Support Engineer, part of the Document Processing Team in Sofia Bulgaria.

 

 

Related Posts

Comments

Comments are disabled in preview mode.