How do I get the total number of pages in a generated PDF document?

1 Answer 114 Views
PdfProcessing
Grzegorz
Top achievements
Rank 1
Grzegorz asked on 17 Mar 2023, 03:31 PM

Hello

I'm trying to implement a simple page counter in my generated document to display "Page x of y" in the footer.

I tried to use the PAGE and NUMPAGES fields, as explained in this article: https://docs.telerik.com/devtools/document-processing/libraries/radwordsprocessing/concepts/fields/page-field, but the NUMPAGES field doesn't update and I get "Page x of " instead.

I also tried the workaround suggested in this forum post: https://www.telerik.com/forums/table-headers-page-numbers, but the result is inconsistent - when testing on the same document I get either a count of 8 or 9, even though it's always the same document and always eight pages long.

How can I get the total number of pages in the generated document?

My current code:

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

//Business logic

PdfFormatProvider provider = new PdfFormatProvider();
RadFixedDocument fixedDocument = provider.ExportToFixedDocument(document);
int count = fixedDocument.Pages.Count;

Footer footer = document.Sections.First().Footers.Add();
editor.MoveToParagraphStart(footer.Blocks.AddParagraph());

editor.InsertText("Page ");
FieldInfo pageFI = editor.InsertField("PAGE", "1");
editor.InsertText($" of {count}");

document.UpdateFields();

using (MemoryStream output = new MemoryStream())
{
    Telerik.Windows.Documents.Extensibility.FontsProviderBase fontsProvider = new FontsProvider();     
    Telerik.Windows.Documents.Extensibility.FixedExtensibilityManager.FontsProvider = fontsProvider;
    PdfFormatProvider pdfFormatProvider = new PdfFormatProvider();
    pdfFormatProvider.Export(document, output);

    return output.ToArray();
}

1 Answer, 1 is accepted

Sort by
0
Yoan
Telerik team
answered on 20 Mar 2023, 02:37 PM

Hello Grzegorz,

I have incorporated the provided code snippet into a project on my end and it seems to be working as expected. The overall approach seems to be correct and the only thing I would suggest is utilizing the NUMPAGES filed for the total number of pages for easier and faster implementation and also implementing the NumberingFieldsProvider.

The evaluation of the Page Numbering Fields (PAGE, PAGEREF, NUMPAGES, SECTION, SECTIONPAGE) requires calculating the size of the document elements. This is why to update them, an implementation of the NumberingFieldsProvider is required which can provide the needed layout logic.

I am attaching the project I used for your disposal. I have made some slight modifications to it in the form of commenting some code that can be skipped and also exporting the RadFlowDocument as a PDF to the file system. Feel free to examine and modify the project and the document as you like and let me know if I am missing something.  

I am looking forward to your reply.

Regards, Yoan Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
PdfProcessing
Asked by
Grzegorz
Top achievements
Rank 1
Answers by
Yoan
Telerik team
Share this question
or