Version

Print Page Numbers

When you print a report, the WPF Reporting engine only prints the report’s content. If you want to print a header or footer, you can set the Report object or EmbeddedVisualReportSection object’s PageHeader or PageFooter properties. However, if you want to add page numbers to the header or footer of a report, setting each individual section’s header/footer to an integer is the wrong way to do it. Instead, you should create a data template and bind to the PhysicalPageNumber property of the ReportPagePresenter class.

Adding page numbers to a header or footer in a report is a two-step process.

  1. Create a DataTemplate object in XAML.

  2. Assign the DataTemplate object to the PageHeaderTemplate or PageFooterTemplate property of a Report object or EmbeddedVisualReportSection object.

The following example code demonstrates how to print page numbers to a report using a data template. The XAML creates a basic DataTemplate object and the procedural code assigns it to the PageHeaderTemplate property of a Report object named report1.

In XAML:

<!--Add this DataTemplate to the Window's ResourceDictionary-->
<DataTemplate x:Key="pageNumberDataTemplate">
    <DockPanel Margin="5">
        <!--This TextBlock's Text property is bound to the ReportPagePresenter's PhysicalPageNumber property-->
        <TextBlock
            xmlns:igReporting="http://infragistics.com/Reporting"
            DockPanel.Dock="Right"
            Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type igReporting:ReportPagePresenter}}, Path=PhysicalPageNumber}" />
            <TextBlock Text="Page Number: " DockPanel.Dock="Right" />
            <!--This ContentPresenter will display the Header property of the Report or EmbeddedVisualReportSection object.-->
            <ContentPresenter Content="{Binding}" />
        </DockPanel>
</DataTemplate>

In Visual Basic:

report1.PageHeaderTemplate = TryCast(Me.TryFindResource("pageNumberDataTemplate"), DataTemplate)

In C#:

report1.PageHeaderTemplate = this.TryFindResource("pageNumberDataTemplate") as DataTemplate;