Spread WPF 18
Features / Print / Header and Footer Settings
In This Topic
    Header and Footer Settings
    In This Topic

    Spread for WPF allows you to add headers and footers to display information at the top or bottom of each page in a worksheet. You can define scaling and alignment settings for the headers and footers, as well as apply different headers and footers to the first, odd, and even pages.

    You can use the following special formatting codes for header and footer properties such as LeftHeader, CenterHeader, RightHeader, LeftFooter, CenterFooter, and RightFooter.

    Format code Description
    &L Aligns the string to the left.
    &C Centers the text.
    &R Aligns the string to the right.
    &E Prints the string with double underlining.
    &X Prints superscripts.
    &Y Prints subscripts.
    &B Prints the string in bold.
    &I Prints the string in italics.
    &U Prints the string with an underline.
    &S Prints the string with a strikethrough.
    &"font name" Prints characters in the specified font. Make sure to enclose the font name in double quotation marks (" ").
    &nn Prints characters at the specified font size. nn is a two-digit number that indicates the number of points.
    &color Prints characters in a specified color, with the user specifying a hexadecimal color value.
    &"+" Prints text in the Heading font of the current theme. Make sure to enclose the font name in double quotation marks (" ").
    &"-" Prints text in the Body font of the current theme. Make sure to enclose the font name in double quotation marks (" ").
    &K xx . S nnn 

    Prints text in the specified color of the current theme.

    • xx is a two digit number between 1 and 12 that specifies the theme color to use.
    • S nnn specifies the shade (tint) of the theme color. To make the shade lighter, specify S as +; to make it darker, specify S as -.
    • nnn is a three-digit integer that specifies the percentage from 0 to 100.

    If the value you specify for the theme color or shade exceeds this limit, the closest valid value will be used.

    &D Prints the current date.
    &T Prints the current time.
    &F Prints the name of the file.
    &A Prints the sheet header names.
    &P Prints page numbers.
    &P+number Prints the page number plus the specified <number>.
    &P-number Prints the page number minus the specified <number>.
    && Prints an ampersand (&).
    &N Prints all page numbers in the file.
    &Z Prints the file path.
    &G Insert an image. Set the image files in the LeftImage , CenterImage , and RightImage properties to display the position. If you reference an image file that is contained in an assembly with a URI, use an absolute package URI.

    Add Text or Images to Headers and Footers

    You can add text or images to headers and footers based on your needs using the following properties of the IPageSetup interface:

    The following example code adds text and images in the page header and footer.

    Copy Code
    for (int i = 0; i < spreadSheet1.Workbook.Worksheets.Count - 1; i++)
    {
      // Set the left footer to print date and time.
      spreadSheet1.Workbook.Worksheets[i].PageSetup.LeftFooter = "Printing date and time: &D(&T)";
      // Set the CenterHeaderPicture to a logo.
      spreadSheet1.Workbook.Worksheets[i].PageSetup.CenterHeaderPicture.Filename = @"Images/GrapecityLogo.png";
      spreadSheet1.Workbook.Worksheets[i].PageSetup.CenterHeader = "&G";
                   
      // Set the page number in the RightHeader.
      spreadSheet1.Workbook.Worksheets[i].PageSetup.RightHeader = "&B &P / &N page";
    }
    
    Copy Code
    For i As Integer = 0 To spreadSheet1.Workbook.Worksheets.Count - 1 - 1
      ' Set the left footer to print date and time.
      spreadSheet1.Workbook.Worksheets(i).PageSetup.LeftFooter = "Printing date and time: &D(&T)"
      ' Set the CenterHeaderPicture to a logo.
      spreadSheet1.Workbook.Worksheets(i).PageSetup.CenterHeaderPicture.Filename = "Images/GrapecityLogo.png"
      spreadSheet1.Workbook.Worksheets(i).PageSetup.CenterHeader = "&G"
      ' Set the page number in the RightHeader.
      spreadSheet1.Workbook.Worksheets(i).PageSetup.RightHeader = "&B &P / &N page"
    Next
    

    Set Header and Footer Scaling and Alignment

    You can specify whether the headers and footers use the same scaling and alignment as the worksheet. Use the ScaleWithDocHeaderFooter and AlignMarginsHeaderFooter properties of the IPageSetUp interface to set the scaling and alignment settings for the page header and footer. You can set these property values to true if the header and footer should scale with the document when its size changes, or align with the margins set in the page setup options; otherwise, set them to false.

    The following example code sets the scaling and alignment for the page header and footer.

    Copy Code
    // Set the scale and alignment of the page header and footer.
    spreadSheet1.Workbook.Worksheets[0].PageSetup.AlignMarginsHeaderFooter = false;
    spreadSheet1.Workbook.Worksheets[0].PageSetup.ScaleWithDocHeaderFooter = false;
    
    Copy Code
    ' Set the scale and alignment of the page header and footer.
    spreadSheet1.Workbook.Worksheets(0).PageSetup.AlignMarginsHeaderFooter = False
    spreadSheet1.Workbook.Worksheets(0).PageSetup.ScaleWithDocHeaderFooter = False
    

    Configure Headers and Footers for Odd and Even Pages

    You can apply different headers and footers for the odd and even pages. To apply different headers and footers on odd and even pages, set the OddAndEvenPagesHeaderFooter property of the IPageSetUp interface to true.

    The following example code sets different headers and footers for odd and even pages.

    Copy Code
    for (int i = 0; i < spreadSheet1.Workbook.Worksheets.Count - 1; i++)
    {
       // Set the header and footer for odd and even pages.
       spreadSheet1.Workbook.Worksheets[i].PageSetup.OddAndEvenPagesHeaderFooter = true;
       spreadSheet1.Workbook.Worksheets[i].PageSetup.EvenPage.CenterHeader = "Even Pages";
       spreadSheet1.Workbook.Worksheets[i].PageSetup.EvenPage.RightFooter = "&B &P / &N page";
       spreadSheet1.Workbook.Worksheets[i].PageSetup.RightFooter = "&B &P / &N page";
       spreadSheet1.Workbook.Worksheets[i].PageSetup.CenterHeader = "Odd Page";
    }
    
    Copy Code
    For i As Integer = 0 To spreadSheet1.Workbook.Worksheets.Count - 1 - 1
        ' Set the header and footer for odd and even pages.
        spreadSheet1.Workbook.Worksheets(i).PageSetup.OddAndEvenPagesHeaderFooter = True
        spreadSheet1.Workbook.Worksheets(i).PageSetup.EvenPage.CenterHeader = "Even Pages"
        spreadSheet1.Workbook.Worksheets(i).PageSetup.EvenPage.RightFooter = "&B &P / &N page"
        spreadSheet1.Workbook.Worksheets(i).PageSetup.RightFooter = "&B &P / &N page"
        spreadSheet1.Workbook.Worksheets(i).PageSetup.CenterHeader = "Odd Page"
    Next
    

    Set a Header and Footer for the First Page

    You can set a unique header and footer for the first page by setting the DifferentFirstPageHeaderFooter property of the IPageSetUp interface to true.

    The following example code specifies a different header and footer for the first page.

    Copy Code
    // Set the header and footer for the first page.                 
    spreadSheet1.Workbook.Worksheets[0].PageSetup.DifferentFirstPageHeaderFooter = true;
    spreadSheet1.Workbook.Worksheets[0].PageSetup.FirstPage.RightHeader = "Print Date and Time: &D(&T)";
    spreadSheet1.Workbook.Worksheets[0].PageSetup.FirstPage.RightFooter = "&B &D (&T)";
    
    Copy Code
    ' Set the header and footer for the first page.                 
    spreadSheet1.Workbook.Worksheets(0).PageSetup.DifferentFirstPageHeaderFooter = True
    spreadSheet1.Workbook.Worksheets(0).PageSetup.FirstPage.RightHeader = "Print Date and Time: &D(&T)"
    spreadSheet1.Workbook.Worksheets(0).PageSetup.FirstPage.RightFooter = "&B &D (&T)"