Skip to content

Print Selected Pages Using PrintDialog

Ashwini Bagga

Asked Jul 8, 2008, 3:36 AM EDT4 answers
Share
LinkedInX

I have created a Windows Application. Printing works fine with the help of printDocument and printDialog.

The problem appears when I print a selected range of pages. In that case nothing happens. If I choose the default option of "All" in the print dialog then it prints all the pages successfully.

Where is the problem and how it could be resolved.

Ashwini Bagga

4 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    what u say is not clear,can you show the code ?.i think many people don't know what u want~~~~
    0
  • 0

    Let me make the problem more clearer.

    I have created a Windows Application in C# 2005. For the purpose of printing I have used printDocument and printDialog, printPreview Controls. When I select the default option of "All" pages and click on print in the print dialog box,  then it prints all the pages without any problem.

    Now the issue arises when I go for the printing of selected pages. You know in the print dialog box there are a few options which a user can select to print some specific pages. For example: Selection, Current Page, Pages (with a text box where user can enter a range of pages to print). I have enabled these options in the printDialog box. Further when I use any of these options and issue a print command, then nothing gets printed. I am unable to trap the problem.

    Where I should work out to resolve it. I hope now the description of the problem would be more clearer and understandable. Please help me out to get the proper solution of it.

    Regards!

    0
  • 0

    i think u forgot to delegate the PrintPage event,so while u want to print the selected page but nothing. this is a simple example  i hope it can help u,maybe it has some bug but i think u can handle :

    C#
    //////////////////////////////////
    using System;
    using System.Drawing.Printing;
    using System.Windows.Forms;
    using System.IO;
    namespace EDImageSystem
    {
     /// <summary>
     /// PrintService
     /// </summary>
     public class PrintService
     {
      public PrintService()
      {
       this.docToPrint.PrintPage+=new PrintPageEventHandler(docToPrint_PrintPage);
      }
      // Declare the PrintDocument object.
      private System.Drawing.Printing.PrintDocument docToPrint =
       new System.Drawing.Printing.PrintDocument();//initialize PrintDocument
      private System.IO.Stream streamToPrint;
      string streamType;
      // This method will set properties on the PrintDialog object and
      // then display the dialog.
      public void StartPrint(Stream streamToPrint,string streamType)
      {
       this.streamToPrint=streamToPrint;
       this.streamType=streamType;
       // Allow the user to choose the page range he or she would
       // like to print.
       System.Windows.Forms.PrintDialog PrintDialog1=new PrintDialog ();//initialize a PrintDialog
       PrintDialog1.AllowSomePages = true;
       // Show the help button.
       PrintDialog1.ShowHelp = true;
       // Set the Document property to the PrintDocument for
       // which the PrintPage Event has been handled. To display the
       // dialog, either this property or the PrinterSettings property
       // must be set
       PrintDialog1.Document = docToPrint;

       DialogResult result = PrintDialog1.ShowDialog();/

       // If the result is OK then print the document.
       if (result==DialogResult.OK)
       {
        docToPrint.Print();
       }

      }

    // The PrintDialog will print the document
      // by handling the document’s PrintPage event.
      private void docToPrint_PrintPage(object sender,
       System.Drawing.Printing.PrintPageEventArgs e)
      {

       // Insert code to render the page here.
       // This code will be called when the control is drawn.

       // The following code will render a simple
       // message on the printed document
       switch(this.streamType)
       {
        case "txt":
         string text = null;
         System.Drawing.Font printFont = new System.Drawing.Font
          ("Arial", 35, System.Drawing.FontStyle.Regular);

         // Draw the content.
         System.IO.StreamReader streamReader=new StreamReader(this.streamToPrint);
         text=streamReader.ReadToEnd();
         e.Graphics.DrawString(text,printFont,System.Drawing.Brushes.Black,e.MarginBounds.X,e.MarginBounds.Y);
         break;
        case "image":
         System.Drawing.Image image=System.Drawing.Image.FromStream(this.streamToPrint);
         int x=e.MarginBounds.X;
         int y=e.MarginBounds.Y;
         int width=image.Width;
         int height=image.Height;
         if((width/e.MarginBounds.Width)>(height/e.MarginBounds.Height))
         {
          width=e.MarginBounds.Width;
          height=image.Height*e.MarginBounds.Width/image.Width;
         }
         else
         {
          height=e.MarginBounds.Height;
          width=image.Width*e.MarginBounds.Height/image.Height;
         }
         System.Drawing.Rectangle destRect=new System.Drawing.Rectangle(x,y,width,height);
         e.Graphics.DrawImage(image,destRect,0,0,image.Width,image.Height,System.Drawing.GraphicsUnit.Pixel);
         break;
        default:
         break;
       }
      
      }

     }
    }

    0
  • 0
    Dear Friend ,
                                 i am create simple print document project in c#.i have done all process,but does not work for some pages(Between pages EX:5-8)print.i am try above code its not work it.please help me .how can  i print somepages in c#... help me friends....
    0