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();
}