Skip to main content Skip to footer

Printing Specific Pages in C1Report

Sometimes users have a requirement when they would like to print only a range of pages or print specific pages only. Suppose we have a report containing 5 pages and we'd like to preview/print only the first 3 pages or only the last 3 pages or just the 1st, 3rd and 5th pages. In this blog, we'll cover the following 2 scenarios using C1PrintDocument and C1PrintPreviewDialog controls :

  • Print a range of pages (2-4)
  • Print specific pages (1,3,5)

Populating C1ComboBox and C1ListBox

For the purpose of selecting a range of pages or selecting specific pages, we'll take two C1ComboBoxes and a C1ListBox which are populated with the number of pages in C1Report. We can populate them based on the value returned by the GetPageCount() method of C1Report as below :

c1Report1.Load("../../CommonTasks.xml", "01: Alternating Background (Greenbar report)");  
c1Report1.Render();  

int pages = c1Report1.GetPageCount();  
c1Combo1.DataMode = C1.Win.C1List.DataModeEnum.AddItem;  
c1Combo2.DataMode = C1.Win.C1List.DataModeEnum.AddItem;  
c1List1.DataMode = C1.Win.C1List.DataModeEnum.AddItem;  
c1List1.SelectionMode = C1.Win.C1List.SelectionModeEnum.CheckBox;  

for (int i = 0; i < pages; i++)  
{  
   c1Combo1.AddItem((i + 1).ToString());  
   c1Combo2.AddItem((i + 1).ToString());  
   c1List1.AddItem((i + 1).ToString());  
}

Printing Range of Pages

First of all we'll declare an ArrayList that'll contain the range of pages to be previewed/printed. Next, we add the pages to be previwed/printed to this ArrayList using the GetPageImage() method of C1Report.

int fromPage = Convert.ToInt32(c1Combo1.Text);  
int toPage = Convert.ToInt32(c1Combo2.Text);  
int pageCount = toPage - fromPage;  

ArrayList Pages = new ArrayList();  

for (int i = fromPage - 1; i < toPage; i++)  
{  
   Pages.Add(c1Report1.GetPageImage(i));  
}  
RenderPrintPreviewDialog(Pages);

The RenderPrintPreviewDialog() method is explained later in this article.

Printing Specific Pages

To print specific pages, we'll use the same logic as used earlier. The only difference being the index passed to the GetPageImage() method, depending on the items selected in C1ListBox.

SelectedRowCollection items = c1List1.SelectedIndices;  
if (items.Count > 0)  
{  
   ArrayList Pages = new ArrayList();  
   foreach (var item in items)  
   {  
      Pages.Add(c1Report1.GetPageImage(Convert.ToInt32(item)));  
   }  
   RenderPrintPreviewDialog(Pages);  
}

The RenderPrintPreviewDialog() Method

The RenderPrintPreviewDialog() method creates a C1PrintDocument which contains the pages added to the ArrayList earlier. The pages are added to C1PrintDocument as images using the RenderImage class of C1Report. We can preview the pages we selected in C1PrintPreviewDialog control by setting its document property to the generated C1PrintDocument.

public void RenderPrintPreviewDialog(ArrayList pages)  
{  
   C1PrintDocument doc = new C1PrintDocument();  
   doc.PageLayout.PageSettings.LeftMargin = "0.0in";  
   doc.PageLayout.PageSettings.TopMargin = "0.0in";  
   //Add Page Images to C1PrintDocument  
   foreach (Metafile pageImage in pages)  
   {  
      RenderImage renderImage = new RenderImage(pageImage);  
      doc.Body.Children.Add(renderImage);  
      doc.Reflow();  
   }  

   c1PrintPreviewDialog1 = new C1.Win.C1Preview.C1PrintPreviewDialog();  
   c1PrintPreviewDialog1.Width = 700;  
   c1PrintPreviewDialog1.Height = 700;  
   c1PrintPreviewDialog1.Show();  
   c1PrintPreviewDialog1.Document = doc;  
}

Click on the image to see the working demonstration.

Conclusion

Hope this would be useful for users who wish to choose the pages they want to print. Refer the attached samples for complete implementation. Download C# Sample Download VB Sample

MESCIUS inc.

comments powered by Disqus