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();
}