Atalasoft Knowledge Base

HOWTO: Print a specific Page Range (Windows Forms Controls)



This article is flagged for review/rewrite by support.

This article is recommending using ImageCollection objects which should absolutely be avoided in favor of ImageSource objects.

Please see HOWTO: Use an ImageSource with an AnnotatePrintDocument or ImagePrintDocument

Original Article

Printing images using DotImage (ImagePrintDocument) or DotAnnotate (AnnotatePrintDocument) does not allow for printing of a specific page range other than all of the pages. This article will explain how to work around this, focusing on the AnnotatePrintDocument class, but this can be easily translated to using on ImagePrintDocument.

Since the AnnotatePrintDocument class can only print all the the images it has, we must make an extra effort to enable printing of a page range. The idea here is to simply create an AnnotatePrintDocument with only the pages we want to print in it. Here are the steps we should take:

  1. Create an AnnotatePrintDocument with all images and annotations
  2. Show the PrintDialog to let the user select the print options
  3. Create a new ImageCollection containing only the selected pages, and a new AnnotationController containing the corrasponding Annotation layers
  4. Set the AnnotatePrintDocument to use the new ImageCollection and AnnotationController objects
  5. Start the Printing
  6. Although the most complicated step is creating the new Collection of Images and Annotations, this is still very straight forward. The code listed below can be copied directly into the DotAnnotate Demo:


    // STEP 1: Create annotatePrintDocumentannotatePrintDocument1.SetImagesFromImageCollection(this.annotateViewer1.Images);
    annotatePrintDocument1.Annotations = this.annotateViewer1.Annotations;
    annotatePrintDocument1.ScaleMode = Atalasoft.Imaging.WinControls.PrintScaleMode.FitToEdges;
    annotatePrintDocument1.Center = false;
    // STEP 2: Show the printer dialog.PrintDialog dlg = new PrintDialog();
    dlg.AllowSomePages = true;
    dlg.Document = annotatePrintDocument1;
    dlg.PrinterSettings.FromPage = 1;
    dlg.PrinterSettings.ToPage = this.annotateViewer1.Images.Count;
    dlg.PrinterSettings.MinimumPage = 1;
    dlg.PrinterSettings.MaximumPage = this.annotateViewer1.Images.Count;
    if (dlg.ShowDialog(this) == DialogResult.OK)
    {     // if not printing all pages, create Image Collection     int fromPage = dlg.PrinterSettings.FromPage;
         int toPage = dlg.PrinterSettings.ToPage;
         // STEP 3: Create new ImageCollection and Annotationcontroler
         AnnotationController ann = new AnnotationController();
         int pageNumber = 0;
         ImageCollection printImages = new ImageCollection();
         for(int i = fromPage-1; i < toPage;  i++)     
             printImages.Insert(pageNumber, this.annotateViewer1.Images[i]);
             ann.Layers.Insert(pageNumber, this.annotateViewer1.Annotations.Layers[i]);
         // STEP 4: Set new Images/annotations
         annotatePrintDocument1.SetImagesFromImageCollection( printImages );
         annotatePrintDocument1.Annotations = ann;
         // STEP 5: Print the pages


    ' STEP 1: Create annotatePrintDocument
    annotatePrintDocument1.Annotations = Me.annotateViewer1.Annotations
    annotatePrintDocument1.ScaleMode = Atalasoft.Imaging.WinControls.PrintScaleMode.FitToEdges
    annotatePrintDocument1.Center = False 
    ' STEP 2: Show the printer dialog
    Dim dlg As PrintDialog =  New PrintDialog() 
    dlg.AllowSomePages = True
    dlg.Document = annotatePrintDocument1
    dlg.PrinterSettings.FromPage = 1
    dlg.PrinterSettings.ToPage = Me.annotateViewer1.Images.Count
    dlg.PrinterSettings.MinimumPage = 1
    dlg.PrinterSettings.MaximumPage = Me.annotateViewer1.Images.Count
    If dlg.ShowDialog(Me) = DialogResult.OK Then
        ' if not printing all pages, create Image Collection
        Dim fromPage As Integer =  dlg.PrinterSettings.FromPage
        Dim toPage As Integer =  dlg.PrinterSettings.ToPage
        ' STEP 3: Create new ImageCollection and Annotationcontroler
        Dim ann As AnnotationController =  New AnnotationController()
        Dim pageNumber As Integer =  0
        Dim printImages As ImageCollection =  New ImageCollection()
        Dim i As Integer
        For  i = fromPage-1 To  toPage- 1
            printImages.Insert(pageNumber, Me.annotateViewer1.Images(i))
            ann.Layers.Insert(pageNumber, Me.annotateViewer1.Annotations.Layers(i))
            pageNumber = pageNumber + 1
        ' STEP 4: Set new Images/annotations
        annotatePrintDocument1.Annotations = ann
        ' STEP 5: Print the pages
    End If

    Original Article:
    Q10140 - HOWTO: Print a specific Page Range

Last Modified: 9 Months Ago
Last Modified By: Administrator
Article not rated yet.
Article has been viewed 344 times.
Also In This Category