Home > Back-end >  Split bitmaps into multiple PDF pages
Split bitmaps into multiple PDF pages

Time:03-11

I need to print a panel multiple times (1 to 24 times) into a PDF file. A page can fit about 4 panels.

The problem is, if I try to print it more than 4 times, the 5th bitmap will appear cut off at the bottom of the page instead of going to the next page. I also can't duplicate the pages as the panels have labels with different values.

I've tried condtitions with e.HasMorePages but still nothing.

EXAMPLE

Is there a way to split them in groups into different pages?

Printing code

private void imprimir_btn_Click_1(object sender, EventArgs e)
    {
        System.Drawing.Printing.PrintDocument doc = new System.Drawing.Printing.PrintDocument();
        doc.PrintPage  = new System.Drawing.Printing.PrintPageEventHandler(doc_PrintPage);
        PrintDialog pd = new PrintDialog();
        pd.Document = doc;

        if (pd.ShowDialog() == DialogResult.OK)
        {
            doc.Print();
        }

    }

Bitmap Drawing

 private void doc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {

        int bnds = 55;
        int xcont = 1;

        for (int i = 1; i <= mensalidade_input.Value; i  )
        {
            contrato_lbl.Text = xcont.ToString()   "/"   mensalidade_input.Value;

            Bitmap bmp = new Bitmap(doc_pnl.Width, doc_pnl.Height, doc_pnl.CreateGraphics());
            doc_pnl.DrawToBitmap(bmp, new Rectangle(0, 0, doc_pnl.Width, doc_pnl.Height));
            RectangleF bounds = e.PageSettings.PrintableArea;
            float factor = ((float)bmp.Height / (float)bmp.Width);
            e.Graphics.DrawImage(bmp, bounds.Left, bnds, bounds.Width, factor * bounds.Width);

            bnds = bnds   265;
            xcont = xcont   1;
        }
    }
         

CodePudding user response:

Make current index to global and sum printed image height if it greater than the print area height set HasMorePages to true it will add one more page again call the print page event,

int currentIndex = 1;
    private void doc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {

        int bnds = 55;
        int xcont = 1;

        for (; currentIndex <= mensalidade_input.Value; currentIndex  )
        {
            contrato_lbl.Text = xcont.ToString()   "/"   mensalidade_input.Value;

            Bitmap bmp = new Bitmap(doc_pnl.Width, doc_pnl.Height, doc_pnl.CreateGraphics());
            doc_pnl.DrawToBitmap(bmp, new Rectangle(0, 0, doc_pnl.Width, doc_pnl.Height));
            RectangleF bounds = e.PageSettings.PrintableArea;
            filledHeight  = bmp.Height;
            if (bounds.Height < filledHeight)
            {
                e.HasMorePages = true;
                return;
            }
            float factor = ((float)bmp.Height / (float)bmp.Width);
            e.Graphics.DrawImage(bmp, bounds.Left, bnds, bounds.Width, factor * bounds.Width);

            bnds = bnds   265;
            xcont = xcont   1;
        }
        currentIndex = 1;
    }
  •  Tags:  
  • c#
  • Related