1

I have to generate QR Codes from Excel sheet and then print them (max: 12 on each page). When the last page has 12 QR Codes on a page, it creates a blank page.

If for example: I have 24 QR Codes, it generates 2 pages with 12 codes and 1 blank, if I have less than 12 the printing is OK).

How can I delete or hide or remove that blank page?

My code is:

private void printDocument1_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
    curRow = 0;
    curCopy = 0;
}
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
    int num;
    string str;
    int y = e.MarginBounds.Y;
    Rectangle marginBounds = e.MarginBounds;
    int x = marginBounds.X + 30;
    Pen pen = new Pen(ColorTranslator.FromHtml("#FFBD2D"), 1f);

    int sum = 0;
    foreach (DataRow dr in dt.Rows)
    {
        if(!string.IsNullOrEmpty(dr.Field<string>(1)))
            sum += Convert.ToInt32(dr[4]);
    }

    using (Font font = new Font("Arial", 8f))
    {
        using (StringFormat stringFormat = new StringFormat())
        {
            int num1 = 1;
            StringAlignment stringAlignment = (StringAlignment)num1;
            stringFormat.LineAlignment = (StringAlignment)num1;
            stringFormat.Alignment = stringAlignment;
            int height = (int)font.GetHeight(e.Graphics) + 10;
            for (int i = this.curRow; i < this.dt.Rows.Count; i++)
            {
                DataRow item = this.dt.Rows[i];
                if (!string.IsNullOrEmpty(item.Field<string>(1)))
                {
                    string str1 = item.Field<string>(4);
                    if (str1 == null)
                    {
                        str = null;
                    }
                    else
                    {
                        str = str1.ToString();
                    }
                    if (int.TryParse(str, out num))
                    {
                        int num2 = this.curCopy;
                        while (num2 < num)
                        {
                            Rectangle rectangle = new Rectangle(x, y, 200, 200);
                            Rectangle rectangle1 = new Rectangle(rectangle.X, rectangle.Bottom, rectangle.Width, height);
                            using (Bitmap bitmap = this.GenerateQRCODE(item[1].ToString()))
                            {
                                e.Graphics.DrawImage(bitmap, rectangle);
                            }
                            e.Graphics.DrawLine(pen, x, y, x, y + 200 + 52);
                            e.Graphics.DrawLine(pen, x, y + 200 + 52, x + 200, y + 200 + 52);
                            e.Graphics.DrawLine(pen, x + 200, y + 200 + 52, x + 200, y);
                            e.Graphics.DrawLine(pen, x, y, x + 200, y);
                            e.Graphics.DrawString(item[1].ToString(), font, Brushes.Black, rectangle1, stringFormat);
                            x = rectangle.Right;
                            
                            if (x + rectangle.Width > e.MarginBounds.Right)
                            {
                                marginBounds = e.MarginBounds;
                                x = marginBounds.X + 30;
                                y = rectangle1.Bottom + 30;
                            }

                            //if (sum > 12) //sum > 12
                            //{
                                if (y + rectangle.Height + rectangle1.Height >= e.MarginBounds.Bottom)
                                {
                                    this.curCopy = num2 + 1;
                                    e.HasMorePages = true;
                                    return;
                                }
                                else
                                {
                                    num2++;
                                }
                            //}
                            //else
                            //{
                            //    if (y + rectangle.Height + rectangle1.Height >= e.MarginBounds.Bottom + rectangle.Height + rectangle1.Height)
                            //    {
                            //        this.curCopy = num2 + 1;
                            //        e.HasMorePages = true;
                            //        return;
                            //    }
                            //    else
                            //    {
                            //        num2++;
                            //    }
                            //}
                        }
                    }
                }
                
                this.curRow = i + 1;
                this.curCopy = 0;
            }
        }
    }
    this.refreshprintbtn.Enabled = true;
    this.printdialogbtn.Enabled = true;
}

Edit: I set the printDocument1.DefaultPageSettings.Margins = new Margins(80, 80, 80, 80);.

I'm using this control: https://www.codeproject.com/Articles/13982/Print-Preview-Control-Bar

I'm calling with this method:

private void setupReport()
{
    printDocument1.BeginPrint += printDocument1_BeginPrint;
    printDocument1.PrintPage  += printDocument1_PrintPage;

    printDocument1.DefaultPageSettings.Margins = new Margins(80, 80, 80, 80);

    this.ucFormManagement1.addExtra("LeftMargin", printDocument1.DefaultPageSettings.Margins.Left);
    this.ucFormManagement1.addExtra("RightMargin", printDocument1.DefaultPageSettings.Margins.Right);
    this.ucFormManagement1.addExtra("BottomMargin", printDocument1.DefaultPageSettings.Margins.Bottom);
    this.ucFormManagement1.addExtra("TopMargin", printDocument1.DefaultPageSettings.Margins.Top);
    this.ucFormManagement1.addExtra("Orientation", printDocument1.DefaultPageSettings.Landscape);

    this.preview = new System.Windows.Forms.PrintPreviewControl();
    this.preview.Document = this.printDocument1;
    this.preview.Dock = System.Windows.Forms.DockStyle.Fill;
    this.preview.Location = new System.Drawing.Point(0, 25);
    this.preview.Name = "printPreviewControl1";
    
    this.ucPrintBar1.PreviewControl = this.preview;
    this.tabPage3.Controls.Add(this.preview);
    this.ucPrintBar1.loadDefaults();
}
  • You get a new page if you set HasMorePages to true. Therefore debug and find out why it's being set to true when you think it shouldn't be. – ProgrammingLlama Jul 02 '22 at 11:47
  • @DiplomacyNotWar I tried to set bottom margin higher, but without luck : `if (y + rectangle.Height + rectangle1.Height >= e.MarginBounds.Bottom - 70)`. If I change to 71, I will get only 9 codes. – Molnar Istvan Jul 02 '22 at 16:38
  • 1
    can you check default printer setting for the page's top/bottom margins and paper size, probably reason a single page size not calculated correctly that causes another empty page. re-calculate your single page's Y point with margins besure it is fitting on one page's size. – Mustafa Salih ASLIM Jul 06 '22 at 17:11
  • The answer is here, including the tips about Variable naming. https://stackoverflow.com/questions/28624951/printdocument-adds-blank-page – Jeremy Thompson Jul 12 '22 at 05:18
  • Try something: `if (y + rectangle.Height + rectangle1.Height >= e.MarginBounds.Bottom && (curRow < dt.Rows.Count || curCopy < num)) { request new page }`. In other words, make sure first there are still rows and/or copies left to print. – dr.null Jul 12 '22 at 22:03

1 Answers1

0

You need an else condition for if (int.TryParse(str, out num)) and set e.HasMorePages to false in it.

vdrog
  • 61
  • 1
  • 7
  • it is still not working with this code. I will try your code and recalculate the Y coordinates and the single page margins (based on Mustafas comment.) – Molnar Istvan Jul 11 '22 at 17:13