Consecutive Page Numbers For Merged Section Reports

When creating professional reports, page numbers are considered to be an important element The section based reports of ActiveReports 7 does have a ReportInfo control available which can be used to automatically write page number on the reports. But there are scenarios when the ReportInfo cannot be used. For example if we are creating multiple reports and merging them together to create a single report, the ReportInfo control cannot be used to write continuous page numbers on the merged report since it works on individual report level. Similarly this holds true for reports which uses subreports within it. This does not mean that there is no way to write page numbers on such special reports. The amazing flexibility of ActiveReports allow to make complete use of the code behind and provides a DrawText method which can be used to write any text on the report canvas regardless of the report structure. In this blog, I will throw some light on how you can use this method to write page numbers on two reports which are merged together. Let us see how the completed report looks like:

In this example, we are iterating through all the report pages after the report is merged together. This is done to get the page dimensions and the information about the font which we are using in the report. Following code block shows the implementation:

private void Form1_Load(object sender, EventArgs e)  
    SectionReport1 rpt = new SectionReport1();  
    SectionReport2 rpt2 = new SectionReport2();  
    //Adding the pages of rpt2 to rpt  
    //Using DrawText method to write the page number on the merged report  
    int i = 0;  
    int totalpages = rpt.Document.Pages.Count;  
    foreach (GrapeCity.ActiveReports.Document.Section.Page p in rpt.Document.Pages)  
       i += 1;  
       string str;  
       str = String.Format("Page {0} of " + totalpages.ToString(), i);  
       SizeF size = new SizeF();  
       Font tempfont = p.Font;  
       Font ft = new Font("Arial", 9f);  
       p.Font = ft;  
       size = p.MeasureParagraphHeight(str, p.Width, p.Font, StringFormat.GenericTypographic);  
       Single x;  
       x = (p.Width - size.Width) / 2;  
       Single y;  
       y = (p.Height - p.Margins.Bottom);  
       p.DrawText(str, x, y, size.Width, size.Height);  
       p.Font = tempfont;  
       ft = null;  
       tempfont = null;  
    viewer1.Document = rpt.Document;  

So we are good to go. This example provides everything we need to know about the DrawText method. Since we now know how it works, we can implement whatever scenario we like to. A sample application in both C# and VB.NET can be downloaded from the links below. Download Sample C# Download Sample VB


GrapeCity Developer Tools
comments powered by Disqus