none
Print Page 1 to 5 RRS feed

  • Question

  • Hi

    I have a document around 20 pages I  want to take print 1st to 5th page

    how can I  do that

    pls check my code below

     



            private void tsbPrint_Click(object sender, EventArgs e)
            {
                try
                {
                  
                    pd.AllowCurrentPage = true;
                    pd.UseEXDialog = true;
                    pd.Document = this.document;
                    pd.AllowSomePages = true;


                    if (pd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        document.Print();
                    }

                    //this.document.Print();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Could not print document\n" + ex.Message);
                }

            }
    Tuesday, November 13, 2007 12:11 PM

All replies

  • It would be nice if you said what class pd was.  Sigh.

     

    I'll pull some PrintDialog code from my stuff, this should help you. I'm definitely letting the user choose what pages to print and observing those values in the result. 

     

     It is probably a bit easier in your case; in mine I have to fake some of the printing of a document stuff because I have separate image files that are being printed "as if they are a document". 

     

    In your case, I think you might just need to set your print document's page settings to the PrinterSettings that you got from the dialog or, if you are not showing the user a dialog to pick these page ranges, you might need to create a PrinterSettings object, set it up with appropriate values, and then hand that PrinterSettings object to your print document.

     

    I have highlighted the most relevant lines of code. Everything else is a consequence of gathering the user's page range from the dialog and validating it, etc.

     

    HTH,

     

    >L<  

     

    Code Block

       Friend WithEvents PrintDoc As System.Drawing.Printing.PrintDocument
       Friend WithEvents PrintDialog As System.Windows.Forms.PrintDialog

     

       Private Sub btnPrint_OnClick( _

         ByVal sender As System.Object, ByVal e As System.EventArgs) _

          Handles btnPrint.Click

          With PrintDialog
             .ShowHelp = True
             .AllowSomePages = True
             .AllowCurrentPage = False
             Dim result As DialogResult = .ShowDialog()
             If (result = Windows.Forms.DialogResult.OK) Then
                Me.iPrintPageTo = Me.iPageCount
                If .PrinterSettings.FromPage > 0 AndAlso _
                   .PrinterSettings.FromPage <= Me.iPrintPageTo Then
                   Me.iPrintPageFrom = .PrinterSettings.FromPage
                Else
                   Me.iPrintPageFrom = 1
                End If
                If .PrinterSettings.ToPage > 0 AndAlso _
                   .PrinterSettings.ToPage < Me.iPrintPageTo Then
                   Me.iPrintPageTo = .PrinterSettings.ToPage
                End If
                .PrinterSettings.FromPage = Me.iPrintPageFrom
                .PrinterSettings.ToPage = Me.iPrintPageTo
                Me.PrintDoc.PrinterSettings = .PrinterSettings
                Me.iCurrentPrintingPage = Me.iPrintPageFrom
                Me.PrintDoc.Print()

             Else
                Me.iPrintPageTo = 0
                Me.iPrintPageFrom = 0
                Me.iCurrentPrintingPage = 0
             End If
          End With

       End Sub

     

      Private Sub PrintDoc_PrintPage( _

        ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)   
         Handles PrintPage.PrintPage
          e.Graphics.DrawImage( _

            Image.FromFile(Me.sImageFiles(Me.iCurrentPrintingPage - 1)), _
                           e.Graphics.VisibleClipBounds)
          ' Indicate that this is the last page to print.
          e.HasMorePages = (Me.iCurrentPrintingPage < Me.iPrintPageTo)
          Me.iCurrentPrintingPage += 1
       End Sub

     

     

    Wednesday, November 14, 2007 6:14 AM
  • Hi,

           Thanks your valid replay but I try to implement your code but in my application this also makes wrong out put, I think my code have some problem (in C#.net )  can you check what I am write

     

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Text;

    using System.Windows.Forms;

    using System.Drawing.Printing;

    namespace TestPrint

    {

    public partial class Form2 : Form

    {

    private int iCurrentPrintingPage ;

    private int iPrintPageTo;

    private int iPageCount;

    private int iPrintPageFrom;

    private int noOfItems;

    private int currentItem;

     

    public Form2()

    {

       InitializeComponent();

    this.printDocument1.PrintPage+=new System.Drawing.Printing.PrintPageEventHandler  (printDocument1_PrintPage);

    }

    private void button1_Click(object sender, EventArgs e)

    {

    pd.AllowSomePages = true;

    pd.UseEXDialog = false;

    pd.PrinterSettings.ToPage = iPageCount;

    pd.PrinterSettings.FromPage = 1;

    if (pd.ShowDialog() == System.Windows.Forms.DialogResult.OK)

    {

    this.iPrintPageTo = this.iPageCount;

    if (pd.PrinterSettings.FromPage > 0 && pd.PrinterSettings.FromPage <= this.iPrintPageTo)

    {

    this.iPrintPageFrom = pd.PrinterSettings.FromPage;

    }

    else

    {

    this.iPrintPageFrom = 1;

    }

    if (pd.PrinterSettings.ToPage > 0 && pd.PrinterSettings.ToPage < this.iPrintPageTo)

    {

    this.iPrintPageTo = pd.PrinterSettings.ToPage;

    }

    pd.PrinterSettings.FromPage = this.iPrintPageFrom;

    pd.PrinterSettings.ToPage = this.iPrintPageTo;

    this.printDocument1.PrinterSettings = pd.PrinterSettings;

    this.currentItem = this.iPrintPageFrom;

    this.printDocument1.Print();}

    else

    {

    this.iPrintPageTo = 0;

    this.iPrintPageFrom = 0;

    this.iCurrentPrintingPage = 0;

    }

    }

    private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)

    {

    //e.Graphics.DrawImage(Image.FromFile(this.sImageFiles(this.iCurrentPrintingPage - 1)), e.Graphics.VisibleClipBounds);

    // Indicate that this is the last page to print.

    String fontName = "Arial";

    Font fontHeader = new Font(fontName, 8, System.Drawing.FontStyle.Bold);

    Font fontNormal = new Font(fontName, 8, System.Drawing.FontStyle.Regular);

    float categoryHeight = fontHeader.GetHeight(e.Graphics);

    float itemHeight = fontNormal.GetHeight(e.Graphics);

    Brush normalColour = Brushes.Black;

    Brush urgentColour = Brushes.Red;

    Pen thePen = new Pen(Brushes.Black);

    float leftMargin = e.MarginBounds.Left;

    float topMargin = e.MarginBounds.Top;

    float printWidth = e.MarginBounds.Width;

    float printHeight = e.MarginBounds.Height;

    float rightMargin = leftMargin + printWidth;

    float currentPosition = topMargin;

    float numberWidth = 70;

    float lineWidth = printWidth - numberWidth;

    float lineLeft = leftMargin;

    float numberLeft = leftMargin + lineWidth;

    while ((this.currentItem < this.noOfItems)&& (currentPosition < printHeight + topMargin))

    {

    string line = "This is line number";

    string lineNo = this.currentItem.ToString();

    Brush theBrush = normalColour;

    Font theFont = fontNormal;

    if ((this.currentItem % 10) == 0)

    {

    theBrush = urgentColour;

    theFont = fontHeader;

    e.Graphics.DrawLine(thePen

    , leftMargin

    , currentPosition

    , rightMargin

    , currentPosition);

    }

    //Just store the line height until we've finished the entire line.

    float height = PrintBar.Util.printLine(e

    , line

    , theFont

    , lineLeft

    , currentPosition

    , lineWidth

    , false

    , theBrush);

    PrintBar.Util.printLine(e

    , lineNo

    , theFont

    , numberLeft

    , currentPosition

    , numberWidth

    , false

    , theBrush);

    currentPosition += height;

    this.currentItem++;

    }

    e.HasMorePages = (this.currentItem < this.noOfItems);

    this.iPageCount += 1;

    }

    private void Form2_Load(object sender, EventArgs e)

    {

    this.noOfItems = 500;

    }

    }

    }

     

     

    Thanks and regards

    Anish MM
    Saturday, November 17, 2007 5:52 AM
  • When you say "wrong output" what do you mean, exactly?  Do you get an error, or what?

     

    >L<

    Saturday, November 17, 2007 8:13 AM
  • Hi thanks

     

    Actually I wand page From and To printing style, ie. ( I have 7 pages I wand  1 to 3 or 3  to 7).

     

    But your code print all pages 1 to 7

     

    regards

     

    anish mm

     

    Saturday, November 17, 2007 8:17 AM
  • No, my code does not "print all pages 1 to 7".  As written, my code prints all pages selected by the user in a dialog. 

     

    After the dialog is shown, my code shows you the items you need to *adjust* in order to respect the user's choices in the dialog.  By itself, it doesn't limit the pages, but you can use that code to limit the pages.

     

    I am not going to run your code and debug it.  However I do see that you say:

     

    e.HasMorePages = (this.currentItem < this.noOfItems);

     

    ... my code has:

     

    e.HasMorePages = (Me.iCurrentPrintingPage < Me.iPrintPageTo)

    ... do you see the difference?  Your number of items is probably the full page amount, and you have not (as far as I can see) figured out how many of your 500 items will fit on the appropriate pages.  So how exactly did you expect this to work?

     

    >L<

    Saturday, November 17, 2007 3:51 PM