You can provide header and footers that appears on the printed pages. Using the Header property and Footer property of the PrintInfo class, which may include special control commands, you can specify text and variables, such as page numbers, as well as specify the font settings. The font related commands begin with "f".
The control commands that can be inserted in headers and footers are listed in this table:
Control Character | Full Command | Action in Printed Page Header or Footer |
---|---|---|
/ | / | Inserts a literal forward slash character (/) |
/c | /c | Center justifies the item |
/cl | /cl"n" | Sets the font color for text, with the zero-based index of the color, n, in quotes (n can be 0 or more); see the Colors property |
/dl | /dl | Inserts the date, using the long form |
/ds | /ds | Inserts the date, using the short form |
/f | /f"n" | Recalls the previously saved font settings (see /fs in this table), with the zero-based index, n, in quotes (n can be 0 or more) |
/fb | /fb0 | Turns off bold font type |
/fb1 | Turns on bold font type | |
/fi | /fi0 | Turns off italics font type |
/fi1 | Turns on italics font type | |
/fk | /fk0 | Turns off strikethrough |
/fk1 | Turns on strikethrough | |
/fn | /fn"name" | Sets the name of the font face, with the name of the font in quotes |
/fs | /fs"n" | Saves the font settings for re-use, with the zero-based index of the font settings, n, in quotes (see /f in this table) |
/fu | /fu0 | Turns off underline |
/fu1 | Turns on underline | |
/fz | /fz"n" | Set the size of the font |
/g | /g"n" | Inserts a graphic (image), with the zero-based index of the image, n, in quotes (n can be zero or more); see the Images property |
/l | /l | Left justifies the item (that is the letter l or L, as in Left) |
/n | /n | Inserts a new line |
/p | /p | Inserts a page number |
/pc | /pc | Inserts a page count (the total number of pages in the print job) |
/r | /r | Right justifies the item |
/sn | /sn | Inserts the sheet name |
/tl | /tl | Inserts the time, using the long form |
/ts | /ts | Inserts the time, using the short form |
If you use multiple control characters, do not put spaces between them. The letters can be lower or upper case; all commands and examples are shown here in lower case for simplicity.
Define the headers and footers (set the Header and Footer properties) before printing the sheet (running the PrintSheet method).
You can specify a color for the text from a list of colors if the color is previously defined in the Colors property.
You can specify an image if the image is previously defined in the Images property.
You can add text including the page number and the total number of pages printed.
You can save the font settings to re-use them later in the header or footer.
Here is the result of the example code given below.
This example code prints the sheet with the specified header and footer text.
C# |
Copy Code
|
---|---|
// Create PrintInfo object and set properties. FarPoint.Win.Spread.PrintInfo printset = new FarPoint.Win.Spread.PrintInfo(); printset.Colors = new Color[] {Color.Green, Color.Yellow, Color.Gold, Color.Indigo, Color.Brown}; printset.Images = new Image[] {Image.FromFile("C:\\images\\point.jpg"), Image.FromFile("C:\\images\\logo.gif"), Image.FromFile("C:\\images\\icon.jpg")}; printset.Header = "/fn\"Book Antiqua\" /fz\"14\" Print job for FarPoint Inc./n "; printset.Footer = "/g\"1\"/r/cl\"4\"This is page /p of /pc"; // Set the PrintInfo property for the first sheet. fpSpread1.Sheets[0].PrintInfo = printset; // Print the sheet. fpSpread1.PrintSheet(0); |
VB |
Copy Code
|
---|---|
' Create PrintInfo object and set properties. Dim printset As New FarPoint.Win.Spread.PrintInfo() printset.Colors = New Color() {Color.Green, Color.Yellow, Color.Gold, Color.Indigo, Color.Brown} printset.Images = New Image() {Image.FromFile("D:\images\point.jpg"), Image.FromFile("D:\images\logo.gif"), Image.FromFile("C:\images\icon.jpg")} printset.Header = "/fn""Book Antiqua"" /fz""14"" Print job for FarPoint Inc./n " printset.Footer = "/g""1""/r/cl""4""This is page /p of /pc" ' Set the PrintInfo property for the first sheet. FpSpread1.Sheets(0).PrintInfo = printset ' Print the sheet. FpSpread1.PrintSheet(0) |
This example code prints the sheet with the specified header and footer colors and images.
C# |
Copy Code
|
---|---|
// Create PrintInfo object and set properties. FarPoint.Win.Spread.PrintInfo pi = new FarPoint.Win.Spread.PrintInfo(); pi.Footer = "This is Page /p/nof /pc Pages"; pi.Header = "Print Job For /nFPT Inc."; pi.Colors = new Color[] {Color.Red, Color.Blue}; pi.Images = new Image[] {Image.FromFile("D:\Corporate.jpg"), Image.FromFile("D:\Building.jpg")}; pi.RepeatColEnd = 25; pi.RepeatColStart = 1; pi.RepeatRowEnd = 25; pi.RepeatRowStart = 1; fpSpread1.ActiveSheet.PrintInfo = pi; |
VB |
Copy Code
|
---|---|
' Create PrintInfo object and set properties. Dim pi As New FarPoint.Win.Spread.PrintInfo pi.Footer = "This is Page /p/nof /pc Pages" pi.Header = "Print Job For /nFPT Inc." pi.Colors = New Color() {Color.Red, Color.Blue} pi.Images = New Image() {Image.FromFile("D:\Corporate.jpg"), Image.FromFile("D:\Building.jpg")} pi.RepeatColEnd = 25 pi.RepeatColStart = 1 pi.RepeatRowEnd = 25 pi.RepeatRowStart = 1 FpSpread1.ActiveSheet.PrintInfo = pi |
This example code prints the sheet with the specified header and footer text.
C# |
Copy Code
|
---|---|
// Create PrintInfo object and set properties. FarPoint.Win.Spread.PrintInfo printset = new FarPoint.Win.Spread.PrintInfo(); printset.Header = "/lJobName"; printset.Footer = "/r/p of /pc"; // Create SheetView object and assign it to the first sheet. FarPoint.Win.Spread.SheetView SheetToPrint = new FarPoint.Win.Spread.SheetView(); SheetToPrint.PrintInfo = printset; // Set the PrintInfo property for the first sheet. fpSpread1.Sheets[0] = SheetToPrint; // Print the sheet. fpSpread1.PrintSheet(0); |
VB |
Copy Code
|
---|---|
' Create PrintInfo object and set properties. Dim printset As New FarPoint.Win.Spread.PrintInfo() printset.Header = "/lJobName" printset.Footer = "/r/p of /pc" ' Create SheetView object and assign it to the first sheet. Dim SheetToPrint As New FarPoint.Win.Spread.SheetView() SheetToPrint.PrintInfo = printset FpSpread1.Sheets(0) = SheetToPrint ' Set the PrintInfo property for the first sheet. FpSpread1.Sheets(0).PrintInfo = printset ' Print the sheet. FpSpread1.PrintSheet(0) |
You can also add different headers and footers on the first page, odd pages, and even pages by setting the OddAndEvenPagesHeaderFooter and DifferentFirstPageHeaderFooter properties of PrintInfo class to true.
On setting the OddAndEvenPagesHeaderFooter property to true, Spread.NET allows you to set the different value for header and footer using EvenFooter, FirstFooter, FirstHeader, and other properties.
The following image shows how different headers and footers appear on different printed pages.
Refer to the following code to implement the different header and footer using PrintInfo class.
C# |
Copy Code
|
---|---|
// Different header/footer printing IWorksheet TestActiveSheet = fpSpread1.AsWorkbook().ActiveSheet; TestActiveSheet.Cells["A1"].Value = "Page1"; TestActiveSheet.Cells["A60"].Value = "Page2"; TestActiveSheet.Cells["A100"].Value = "Page3"; fpSpread1.ActiveSheet.PrintInfo.Colors = new System.Drawing.Color[] { System.Drawing.Color.Purple }; fpSpread1.ActiveSheet.PrintInfo.FirstColors = new System.Drawing.Color[] { System.Drawing.Color.Blue }; fpSpread1.ActiveSheet.PrintInfo.EvenColors = new System.Drawing.Color[] { System.Drawing.Color.Red }; fpSpread1.ActiveSheet.PrintInfo.Footer = "/l/cl\"0\"OddLeft/cOddCenter/rOddRight"; // Set DifferentFirstPageHeaderFooter property to true for different first page header footer fpSpread1.ActiveSheet.PrintInfo.DifferentFirstPageHeaderFooter = true; fpSpread1.ActiveSheet.PrintInfo.FirstFooter = "/lFirstLeft/c/cl\"0\"FirstCenter/rFirstRight"; // Set OddAndEvenPagesHeaderFooter property to true for different Odd and Even page headers fpSpread1.ActiveSheet.PrintInfo.OddAndEvenPagesHeaderFooter = true; fpSpread1.ActiveSheet.PrintInfo.EvenFooter = "/lEvenLeft/cEvenCenter/r/cl\"0\"EvenRight"; fpSpread1.Sheets[0].PrintInfo.Preview = true; fpSpread1.Sheets[0].PrintInfo.EnhancePreview = true; fpSpread1.Sheets[0].PrintInfo.ShowColor = true; fpSpread1.PrintSheet(0); |
VB |
Copy Code
|
---|---|
' Different header/footer printing Dim TestActiveSheet As IWorksheet = FpSpread1.AsWorkbook().ActiveSheet TestActiveSheet.Cells("A1").Value = "Page1" TestActiveSheet.Cells("A60").Value = "Page2" TestActiveSheet.Cells("A100").Value = "Page3" FpSpread1.ActiveSheet.PrintInfo.Colors = New Drawing.Color() {Drawing.Color.Purple} FpSpread1.ActiveSheet.PrintInfo.FirstColors = New Drawing.Color() {Drawing.Color.Blue} FpSpread1.ActiveSheet.PrintInfo.EvenColors = New Drawing.Color() {Drawing.Color.Red} FpSpread1.ActiveSheet.PrintInfo.Footer = "/l/cl""0""OddLeft/cOddCenter/rOddRight" ' Set DifferentFirstPageHeaderFooter property to true for different first page header footer FpSpread1.ActiveSheet.PrintInfo.DifferentFirstPageHeaderFooter = True FpSpread1.ActiveSheet.PrintInfo.FirstFooter = "/lFirstLeft/c/cl""0""FirstCenter/rFirstRight" ' Set OddAndEvenPagesHeaderFooter property to true for different Odd and Even page headers FpSpread1.ActiveSheet.PrintInfo.OddAndEvenPagesHeaderFooter = True FpSpread1.ActiveSheet.PrintInfo.EvenFooter = "/lEvenLeft/cEvenCenter/r/cl""0""EvenRight" FpSpread1.Sheets(0).PrintInfo.Preview = True FpSpread1.Sheets(0).PrintInfo.EnhancePreview = True FpSpread1.Sheets(0).PrintInfo.ShowColor = True FpSpread1.PrintSheet(0) |
PageSetup class also supports OddAndEvenPagesHeaderFooter and DifferentFirstPageHeaderFooter properties. However, if you are using the PageSetup class, you should only use the properties included in this class to set the values for headers and footers.
Spread.NET also supports export/import of Excel with the image in the headers and footers of the print page.
The following image shows the print preview of exported Excel with image as a watermark.
Refer to the following code to add image in the header and export the Spread to Excel.
C# |
Copy Code
|
---|---|
IWorksheet TestActiveSheet = fpSpread1.AsWorkbook().ActiveSheet; TestActiveSheet.PageSetup.LeftHeaderPicture.Filename = @"Image.jpg"; TestActiveSheet.PageSetup.LeftHeader = "Left&G"; TestActiveSheet.PageSetup.TopMargin = 1; TestActiveSheet.PageSetup.LeftHeaderPicture.ColorType = PictureColorType.Watermark; TestActiveSheet.PageSetup.LeftHeaderPicture.CropLeft = -40; TestActiveSheet.PageSetup.LeftHeaderPicture.CropTop = 30; fpSpread1.ActiveSheet.PrintInfo.Preview = true; fpSpread1.ActiveSheet.PrintInfo.ShowColor = true; fpSpread1.ActiveSheet.PrintInfo.EnhancePreview = true; fpSpread1.PrintSheet(fpSpread1.ActiveSheet); fpSpread1.SaveExcel(@"excel.xlsx", FarPoint.Excel.ExcelSaveFlags.UseOOXMLFormat); |
VB |
Copy Code
|
---|---|
Dim TestActiveSheet As IWorksheet = FpSpread1.AsWorkbook().ActiveSheet TestActiveSheet.PageSetup.LeftHeaderPicture.Filename = "Image.jpg" TestActiveSheet.PageSetup.LeftHeader = "Left&G" TestActiveSheet.PageSetup.TopMargin = 1 TestActiveSheet.PageSetup.LeftHeaderPicture.ColorType = PictureColorType.Watermark TestActiveSheet.PageSetup.LeftHeaderPicture.CropLeft = -40 TestActiveSheet.PageSetup.LeftHeaderPicture.CropTop = 30 FpSpread1.ActiveSheet.PrintInfo.Preview = True FpSpread1.ActiveSheet.PrintInfo.ShowColor = True FpSpread1.ActiveSheet.PrintInfo.EnhancePreview = True FpSpread1.PrintSheet(FpSpread1.ActiveSheet) FpSpread1.SaveExcel("excel.xlsx", FarPoint.Excel.ExcelSaveFlags.UseOOXMLFormat) |
However, if you have more than one image in the header or footer, only the first image used is exported to Excel in the header or Footer, as Excel supports only one image.
The following image shows the print preview of an exported Excel file where the first used image is placed in the header.
Refer to the following code to add multiple images in the header and export the Spread to Excel.
C# |
Copy Code
|
---|---|
fpSpread1.ActiveSheet.PrintInfo.OddAndEvenPagesHeaderFooter = true; fpSpread1.ActiveSheet.PrintInfo.Margin.Top = 100; fpSpread1.ActiveSheet.PrintInfo.EvenImages = new System.Drawing.Image[] { System.Drawing.Image.FromFile("Image.png"), System.Drawing.Image.FromFile("Image.tiff") }; fpSpread1.ActiveSheet.PrintInfo.EvenHeader = "/l/g\"0\"/g\"1\"EventHeader"; fpSpread1.ActiveSheet.PrintInfo.DifferentFirstPageHeaderFooter = true; fpSpread1.ActiveSheet.PrintInfo.FirstImages = new System.Drawing.Image[] { System.Drawing.Image.FromFile("Image.jpg"), System.Drawing.Image.FromFile("Image.ico") }; fpSpread1.ActiveSheet.PrintInfo.FirstHeader = "/l/g\"0\"/g\"1\"FirstHeader"; fpSpread1.ActiveSheet.PrintInfo.Images = new System.Drawing.Image[] { System.Drawing.Image.FromFile("Image.bmp"), System.Drawing.Image.FromFile("Image.jpeg") }; fpSpread1.ActiveSheet.PrintInfo.Header = "/l/g\"0\"/g\"1\"OddHeader"; fpSpread1.ActiveSheet.PrintInfo.Preview = true; fpSpread1.ActiveSheet.PrintInfo.ShowColor = true; fpSpread1.ActiveSheet.PrintInfo.EnhancePreview = true; // fpSpread1.PrintSheet(fpSpread1.ActiveSheet); fpSpread1.SaveExcel(@"excel_printinfo.xlsx", ExcelSaveFlags.UseOOXMLFormat); |
VB |
Copy Code
|
---|---|
FpSpread1.ActiveSheet.PrintInfo.OddAndEvenPagesHeaderFooter = True FpSpread1.ActiveSheet.PrintInfo.Margin.Top = 100 FpSpread1.ActiveSheet.PrintInfo.EvenImages = New Drawing.Image() {Drawing.Image.FromFile("Image.png"), Drawing.Image.FromFile("Image.tiff")} FpSpread1.ActiveSheet.PrintInfo.EvenHeader = "/l/g""0""/g""1""EventHeader" FpSpread1.ActiveSheet.PrintInfo.DifferentFirstPageHeaderFooter = True FpSpread1.ActiveSheet.PrintInfo.FirstImages = New Drawing.Image() {Drawing.Image.FromFile("Image.jpg"), Drawing.Image.FromFile("Image.ico")} FpSpread1.ActiveSheet.PrintInfo.FirstHeader = "/l/g""0""/g""1""FirstHeader" FpSpread1.ActiveSheet.PrintInfo.Images = New Drawing.Image() {Drawing.Image.FromFile("Image.bmp"), Drawing.Image.FromFile("Image.jpeg")} FpSpread1.ActiveSheet.PrintInfo.Header = "/l/g""0""/g""1""OddHeader" FpSpread1.ActiveSheet.PrintInfo.Preview = True FpSpread1.ActiveSheet.PrintInfo.ShowColor = True FpSpread1.ActiveSheet.PrintInfo.EnhancePreview = True ' FpSpread1.PrintSheet(FpSpread1.ActiveSheet); FpSpread1.SaveExcel("excel_printinfo.xlsx", ExcelSaveFlags.UseOOXMLFormat) |