petraqJdo
petraqJdo

Reputation: 13

How to print the bitmap on two pages?

i am using the bitmap method to print the DataGridView. How to split the bitmap on two pages if the this.dgvTheory.Height is bigger than 500?Everything should stay on the first page,except the bitmap which can go on second page if it's big.

Edited: The rest of the image should go on the 2nd page.

Thanks in advance.

private void button1_Click(object sender, EventArgs e)
{
    int height = dgvTheory.Height;
    dgvHistTheoCust.Height = dgvTheory.RowCount * dgvTheory.RowTemplate.Height;
    bitmap = new Bitmap(960, this.dgvTheory.Height);
    dgvTheory.DrawToBitmap(bitmap, new Rectangle(0, 0, 960, this.dgvTheory.Height));
    dgvTheory.Height = height;
    printPreviewDialog1.Document = printDocument1;
    printPreviewDialog1.ShowDialog();
}

   private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        this.printDocument1.DefaultPageSettings.Landscape = true;
        using (Font fnt1 = new Font("Segoe UI", 12f, FontStyle.Regular))
        {
            using (Font fnt2 = new Font("Arial", 13f, FontStyle.Bold))
            {
                e.Graphics.DrawString("just a text", fnt2, Brushes.Black, new RectangleF(new PointF(500f, 10f), new SizeF(300f, 20f)));
                e.Graphics.DrawRectangle(Pens.Black, 50, 5, 1000, 30);
                e.Graphics.DrawRectangle(Pens.Black, 50, 35, 1000, 30);
                e.Graphics.DrawString("just a test", fnt2, Brushes.Black, new RectangleF(new PointF(280f, 40f), new SizeF(700f, 20f)));
                e.Graphics.DrawRectangle(Pens.Black, 50, 70, 300, 20);
                e.Graphics.DrawString("some data :", fnt1, Brushes.Black, new RectangleF(new PointF(55f, 70f), new SizeF(500f, 20f)));
                e.Graphics.DrawRectangle(Pens.Black, 50, 90, 300, 20);
                e.Graphics.DrawString("some data :", fnt1, Brushes.Black, new RectangleF(new PointF(55f, 90f), new SizeF(300f, 20f)));
                e.Graphics.DrawRectangle(Pens.Black, 350, 70, 700, 20);
                e.Graphics.DrawString("str", fnt1, Brushes.Black, new RectangleF(new PointF(600f, 70f), new SizeF(300f, 20f)));
                e.Graphics.DrawRectangle(Pens.Black, 350, 90, 700, 20);
                e.Graphics.DrawString("str", fnt1, Brushes.Black, new RectangleF(new PointF(600f, 90f), new SizeF(300f, 20f)));
                e.Graphics.DrawRectangle(Pens.Black, 50, 110, 300, 20);
                e.Graphics.DrawRectangle(Pens.Black, 350, 110, 700, 20);
                e.Graphics.DrawString("some data :", fnt1, Brushes.Black, new RectangleF(new PointF(55, 110), new SizeF(300f, 20f)));
                e.Graphics.DrawString("str", fnt1, Brushes.Black, new RectangleF(new PointF(600, 110), new SizeF(300f, 20f)));
                e.Graphics.DrawImage(bitmap, 50, 135);
                e.Graphics.DrawRectangle(Pens.Black, 50, 640, 300, 20);
                e.Graphics.DrawString("some data :", fnt1, Brushes.Black, new RectangleF(new PointF(50, 640), new SizeF(300f, 20f)));
                e.Graphics.DrawRectangle(Pens.Black, 350, 640, 700, 20);
                e.Graphics.DrawString("int", fnt1, Brushes.Black, new RectangleF(new PointF(700, 640), new SizeF(300f, 20f)));
                e.Graphics.DrawString("some data....", fnt1, Brushes.Black, new RectangleF(new PointF(50, 660), new SizeF(1000f, 70f)));
                e.Graphics.DrawString("my sign goes here", fnt1, Brushes.Black, new RectangleF(new PointF(650, 730), new SizeF(300f, 20f)));                   
            }
        }
    }

Upvotes: 1

Views: 458

Answers (1)

TaW
TaW

Reputation: 54453

Printing on multiple page is controlled by two indicators:

  • the internal flag e.HasMorePages tells the system to re-enter the PrintPage method once more after you return from it.

  • some external flag you need to set up tells you which page and/or which content you are supposed to print next.

The internal flag is rather simple but you need to know that it wil be reset whenever you enter the method!

Your external flag can be anything you want, a bool or a number or it can even be implicit, like the state of a queue.

In your case a simple bool will be enough, but for more complex print jobs one may want e.g. an index into a list of items to print..

Let's call your flag printImageOnPageTwo. Declare it at class level:

bool printImageOnPageTwo = false;

Now you can check for it to know if you should enter the part that prints page one or skip it and print the image to the top of page two..:

private void printDocument1_PrintPage(object sender,
                                      System.Drawing.Printing.PrintPageEventArgs e)
{
   this.printDocument1.DefaultPageSettings.Landscape = true;

   if (!printImageOnPageTwo )
    using (Font fnt1 = new Font("Segoe UI", 12f, FontStyle.Regular))
    using (Font fnt2 = new Font("Arial", 13f, FontStyle.Bold))
    {
        e.Graphics.DrawString("just a text", fnt2,.....);
        ...
        ...

        if (dgvTheory.Height <= than 500)
            e.Graphics.DrawImage(...., y_InTheMiddle)
        else
        {
           printImageOnPageTwo = true:   // set our flag
           e.HasMorePages = true;        // set the system flag
           return;                       // quit the routine
        }
    }  // end if !printImageOnPageTwo 
    else  e.Graphics.DrawImage(...., y_AtTheTop);
    printImageOnPageTwo = false;         // reset our flag
}

Upvotes: 1

Related Questions