Leandro
Leandro

Reputation: 115

How to print two bmp into two different pages?

The way I'm trying is generating two overlapping images.

I could make two different buttons to print two separate bmp, but I need the two bmp are printed on the same page as the user can choose the PDF printer and generate a two-page file.

void Imprimir()
{
    PrintDocument pd = new PrintDocument();
    pd.DocumentName = "Relatório SisIndice";
    pd.PrintPage += new PrintPageEventHandler(doc_PrintPage);
    pd.DefaultPageSettings.Landscape = true;
    PrintDialog printDialog1 = new PrintDialog();
    printDialog1.Document = pd;
    DialogResult result = printDialog1.ShowDialog();
    if (result == DialogResult.OK)
    {    
        pd.Print();
    }
}

private void doc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
    Bitmap bmp = new Bitmap(pnlPrint.Width, pnlPrint.Height, pnlPrint.CreateGraphics());
    pnlPrint.DrawToBitmap(bmp, new Rectangle(0, 0, pnlPrint.Width, pnlPrint.Height));
    RectangleF bounds = e.PageSettings.PrintableArea;
    float factor = ((float)bmp.Height / (float)bmp.Width);
    e.Graphics.DrawImage(bmp, bounds.Left, bounds.Top, 1118, 855);
    e.HasMorePages = true;
    Bitmap bmp1 = new Bitmap(dgvDetGraf.Width, dgvDetGraf.Height, dgvDetGraf.CreateGraphics());
    dgvDetGraf.DrawToBitmap(bmp1, new Rectangle(0, 1800, dgvDetGraf.Width, dgvDetGraf.Height));
    RectangleF bounds1 = e.PageSettings.PrintableArea;
    e.Graphics.DrawImage(bmp1, bounds1.Left, bounds1.Top, 894, 684);
    e.HasMorePages = false;

}

Upvotes: 0

Views: 792

Answers (1)

rene
rene

Reputation: 42414

You're close but you missed that the PrintPage event fires for a single page. In that event you have to do all the painting/drawing on its Graphics object. When there are no more pages you set HasMorePages to false.

So instead of drawing all those bitmaps at once you have to keep track if you are on page 1 or page 2 and based on that Draw the bitmap that you want on that page.

I've chosen the simplest solution that might work that involves a page variable to keep track of the page we're on.

int page = 0; 

void Imprimir()
{
    // snip irrelevant stuf
    if (result == DialogResult.OK)
    {   
        // reset our state to page 1
        page = 1;
        pd.Print();
    }
}

private void doc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
    // this gets called twice, the page variable
    // keeps track of what to do (it keeps the State)
    switch(page)
    {
        // do this for page 1
        case 1:
            Bitmap bmp = new Bitmap(pnlPrint.Width, pnlPrint.Height, pnlPrint.CreateGraphics());
            pnlPrint.DrawToBitmap(bmp, new Rectangle(0, 0, pnlPrint.Width, pnlPrint.Height));
            RectangleF bounds = e.PageSettings.PrintableArea;
            float factor = ((float)bmp.Height / (float)bmp.Width);
            e.Graphics.DrawImage(bmp, bounds.Left, bounds.Top, 1118, 855);
            e.HasMorePages = true;
            break;
        // do this for page 2
        case 2:
            Bitmap bmp1 = new Bitmap(dgvDetGraf.Width, dgvDetGraf.Height, dgvDetGraf.CreateGraphics());
            dgvDetGraf.DrawToBitmap(bmp1, new Rectangle(0, 1800, dgvDetGraf.Width, dgvDetGraf.Height));
            RectangleF bounds1 = e.PageSettings.PrintableArea;
            e.Graphics.DrawImage(bmp1, bounds1.Left, bounds1.Top, 894, 684);
            e.HasMorePages = false;
            break;
    }
    // don't forget to go the next state ... 
    page++; // increase page counter
}

Upvotes: 1

Related Questions