Skip to main content Skip to footer

Print Multiple Sheets With Sequential Numbering

Spread for Winforms provide lot of customization options for printing. You can print a spreadsheet, or parts of a spreadsheet, or customize printing as per user requirements. Printing operation is done with the PrintSheet method of FpSpread class. Many of the options available for customizing the printing are in the PrintInfo class. The link here describes various customization that can be done during printing. Each sheet needs to be assigned its own PrintInfo object, as it specifies the various print settings for that specific sheet. The PrintSheet method used to print sheets in Spread accepts sheet index as an argument . When we pass a specific sheet index then it prints the specific sheet; however if we pass -1, then it prints all the sheets in Spread. Printing of each sheet in Spread is a separate print job i.e. executed on a new thread other than the main application thread. Each sheet might be printed to one page or multiple pages depending on the content of the sheet. You can print the page numbers in the footer of each printed page. Since, every print job is executed on a separate thread so the page numbering for each individual sheet starts at 1. In some scenarios you might want to print sequential numbering for all the sheets in Spread i.e. the page numbers printed in the footer should be sequential even when all the sheets are printed collectively and they should not print page numbers starting at 1 whenever a new sheet starts printing. You can handle the same using the PrintHeaderFooterArea event which is raised every time the header or footer for a page is printed. You can declare an explicit variable and increase its count in this event depending on whether the header or footer is printed and print this explicit variable as the page number in the footer. Page numbers can also be printed in the format of type Page 1 of TotalPageCount. In this case you would need to find out the total number of printed pages explicitly and define your footer string accordingly. The total page count can be calculated using the GetPrintPageCount method that returns the printed page count for each sheet. You can call this method for each sheet in Spread, and then sum up the values returned by this method for each sheet to get the total page count and print the same in the footer of the printed page. Here is the code to implement the approach defined above:



void fpSpread1_PrintHeaderFooterArea(object sender, FarPoint.Win.Spread.PrintHeaderFooterAreaEventArgs e)  
{  
  if (!e.IsHeader)  
  {  
    pn = pn + 1;  
    pi.Footer = "Page " + pn.ToString() + "of " + totalcount.ToString();  
  }  
}  

private void button1_Click(object sender, EventArgs e)  
{  
  pn = 0;  
  if (totalcount == 0)  
  {  
    for (int s = 0; s < fpSpread1.Sheets.Count; s++)  
     {  
       totalcount += fpSpread1.GetPrintPageCount(s);  
     }  
  }  
  pi = new FarPoint.Win.Spread.PrintInfo();  
  pi.Footer = "Page "+pn.ToString() +"of " +totalcount.ToString();  
  for (int s = 0; s < fpSpread1.Sheets.Count; s++)  
  {  
    fpSpread1.Sheets[ s ].PrintInfo = pi;  
  }  
  fpSpread1.PrintSheet(-1);  
}  


Attached samples show the complete implementation. Download Sample C# Download Sample VB

MESCIUS inc.

comments powered by Disqus