Headers and Footers


SmartPaginator provides support for headers and footers. Header and footer information is defined at the Section level. Thus, if you want to add headers and footers to your document, you must define a Section in your document.

Size

The header and footer will be given the same width as the content. Thus their width can be calculated as:

   HeaderWidth = FooterWidth = PageSize.Width - Margin.Left - Margin.Right

The header and footer will be given the same height as the appropriate margin. Thus, their height can be calculated as:

   HeaderHeight = Margin.Top
   FooterHeight = Margin.Bottom

HeadersAndFooters.jpg

Vertical Alignment

The header is bottom-aligned and the footer is top-aligned.

Defining headers and footers

Headers and footers are defined by setting the attached properties SectionExtensions.HeaderTemplate and SectionExtensions.FooterTemplate in the Section object.

<FlowDocument xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                        xmlns:s="http://smartpaginator.codeplex.com/">

    <!-- Cover page has no header and footer -->
    <Section Name="CoverPage">
        <Paragraph>
            COVER PAGE
        </Paragraph>
    </Section>

    <Section Name="MainBody" s:SectionExtensions.ResetPageNumbers="True">
        <!-- Add the document title to the header -->
        <s:SectionExtensions.HeaderTemplate>
            <DataTemplate>
                <Border BorderBrush="Black" BorderThickness="0,0,0,1">
                    <TextBlock Text="Document title" FontSize="16" />
                </Border>
            </DataTemplate>
        </s:SectionExtensions.HeaderTemplate>


        <!-- Add the page count to the footer -->
        <s:SectionExtensions.FooterTemplate>
            <DataTemplate>
                <Border BorderBrush="Black" BorderThickness="0,1,0,0">
                    <TextBlock Text="{Binding}" FontSize="16" />
                </Border>
            </DataTemplate>
        </s:SectionExtensions.FooterTemplate>
        <Paragraph>
        ...
        </Paragraph>
    </Section>
</FlowDocument>

PaginationInfo class

As seen in the example below, the content of the header and footer is set to an instance of PaginationInfo which provides pagination information to the header and footer templates. This class has the following properties:
  • PageNumber
  • TotalPages

The ToString() method of a PaginationInfo class will return "Page {0} of {1}", which simplifies its usage on the DataTemplate as seen above. If you want to change the format in which this information is printed, define a multi-binding on the data template. For example:

    <Section Name="MainBody" s:SectionExtensions.ResetPageNumbers="True">

        <!-- Add the page count to the footer -->
        <s:SectionExtensions.FooterTemplate>
            <DataTemplate>
                <Border BorderBrush="Black" BorderThickness="0,1,0,0">
                    <TextBlock FontSize="16">
                        <TextBlock.Text>
                            <MultiBinding StringFormat="Page {0} of {1}">
                                <Binding Path="PageNumber" />
                                <Binding Path="TotalPages" />
                            </MultiBinding>
                        </TextBlock.Text>
                    </TextBlock>
                </Border>
            </DataTemplate>
        </s:SectionExtensions.FooterTemplate>

        ...
    </Section>

Other attached properties

The SectionExtensions class defines two other important attached properties:
  • ResetPageNumbers: This property indicates if page numbering should be reset in this section, or continue the numbering started by the previous section. The default value for this property is false.
  • FirstPageNumber: This property indicates the number of the first page of the section. It is only used if ResetPageNumbers is set to true. The default value for this property is 1.

Last edited Dec 31, 2011 at 6:17 AM by fallinor, version 3

Comments

No comments yet.