我想使用PrintPreviewDialog打印所有项目的条形码。书面代码可以正常工作,但是问题出在第一页结尾,我叫e.HasMorePages = true
,它创建第二页,但不能打印第二页。这意味着第二页为空/空白。如您在下面的图片中看到的,最后一行条形码自动被裁剪,不在第二页上打印剩余的条形码。请帮助如何在第二页上打印剩余的条形码。
如上图所示,最后一行自动被裁剪,第二页不打印,页面为空。
PrintPreviewDialog代码;
pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(this.PrintBarcodeEvent_PrintPage);
System.Windows.Forms.PrintDialog pdd = new System.Windows.Forms.PrintDialog();
pdd.Document = pd;
System.Windows.Forms.DialogResult result = pdd.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK)
{
System.Windows.Forms.PrintPreviewDialog pp = new System.Windows.Forms.PrintPreviewDialog();
pp.Document = pd;
result = pp.ShowDialog();
//pd.Print();
}
事件:
// The PrintPage event is raised for each page to be printed.
private void PrintBarcodeEvent_PrintPage(object sender, PrintPageEventArgs e)
{
int startX = 5;
int startY = 5;
Database db = new Database();
db.DBOpen();
int NBbarcode_perLine = 5;
int numbarcode = 0;
int barcodePerPage = 35;
int countBarcodePerPage = 0;
for (int i = 0; i < listTobePrint.Count; i++)
{
String code = listTobePrint[i].Code;
String name = db.GetByValue(Database.TABLE_ITEMS, Database.CODE_ITEMS, code, 2);
String price = db.GetByValueForInt(Database.TABLE_ITEMS, Database.CODE_ITEMS, code, 8);
Font printFont = new Font("Arial", 10.0f);
e.Graphics.DrawString("Phulkari by VIRSA", printFont, System.Drawing.Brushes.Black,
startX, startY, new StringFormat());
int x2 = startX + 3;
int y2 = startY + 15;
e.Graphics.DrawImage(Util.ImageWpfToGDI(Util.GenerateBarcode(code)), x2, y2, 100, 50);
int x3 = startX;
int y3 = y2 + 50;
e.Graphics.DrawString(code, printFont, System.Drawing.Brushes.Black,
x3, y3, new StringFormat());
int x4 = startX;
int y4 = y3 + 15;
e.Graphics.DrawString(name, printFont, System.Drawing.Brushes.Black,
x4, y4, new StringFormat());
int x5 = startX;
int y5 = y4 + 15;
e.Graphics.DrawString("Rs." + price, printFont, System.Drawing.Brushes.Black,
x5, y5, new StringFormat());
numbarcode++;
countBarcodePerPage++;
if (numbarcode < NBbarcode_perLine)
startX += 150;
else
{
startX = 5;
startY += 150; // space between 2 barcode in vertical (upper left). you have to adjust)
numbarcode = 0;
}
if (countBarcodePerPage >= barcodePerPage)
{
//MessageBox.Show(countBarcodePerPage.ToString());
e.HasMorePages = true;
//startX = 5;
//startY = 5;
} else
{
e.HasMorePages = false;
}
}
db.DBClose();
listTobePrint.Clear();
}
答案 0 :(得分:0)
您必须创建全局变量countbarcode(不是PrintPage本地变量)或静态并初始化Tozéro。 每次将e.HasMorePages设置为true时,都会调用事件PrintPage 对于测试而言,这不是问题,但我认为您应该将db.open放在事件BeginPrint中,将db.close放在事件EndPrint中。每次打印页面时都会打开和关闭数据库。 (也许它并不是很重要,但是您避免重新创建相同的变量db并浪费内存)
private void PrintBarcodeEvent_PrintPage(object sender, PrintPageEventArgs e)
{
int startX = 5;
int startY = 5;
Database db = new Database();
db.DBOpen();
int NBbarcode_perLine = 5;
int numbarcode = 0;
int barcodePerPage = 35;
int countBarcodePerPage = 0;
int totalcodebar = listTobePrint.Count;
for (int i = 0; i < barcodePerPage; i++)
{
String code = listTobePrint[countbarcode].Code;
String name = db.GetByValue(Database.TABLE_ITEMS, Database.CODE_ITEMS, code, 2);
String price = db.GetByValueForInt(Database.TABLE_ITEMS, Database.CODE_ITEMS, code, 8);
Font printFont = new Font("Arial", 10.0f);
e.Graphics.DrawString("Phulkari by VIRSA", printFont, System.Drawing.Brushes.Black,
startX, startY, new StringFormat());
int x2 = startX + 3;
int y2 = startY + 15;
e.Graphics.DrawImage(Util.ImageWpfToGDI(Util.GenerateBarcode(code)), x2, y2, 100, 50);
int x3 = startX;
int y3 = y2 + 50;
e.Graphics.DrawString(code, printFont, System.Drawing.Brushes.Black,
x3, y3, new StringFormat());
int x4 = startX;
int y4 = y3 + 15;
e.Graphics.DrawString(name, printFont, System.Drawing.Brushes.Black,
x4, y4, new StringFormat());
int x5 = startX;
int y5 = y4 + 15;
e.Graphics.DrawString("Rs." + price, printFont, System.Drawing.Brushes.Black,
x5, y5, new StringFormat());
numbarcode++;
countbarcode++;
if (numbarcode < NBbarcode_perLine)
startX += 150;
else
{
startX = 5;
startY += 150; // space between 2 barcode in vertical (upper left). you have to adjust)
numbarcode = 0;
}
if (countbarcode == totalcodebar) break;
if (i == barcodePerPage - 1)
{
db.DBClose();
e.HasMorePages = true;
return;
}
}
e.HasMorePages = false;
db.DBClose();
listTobePrint.Clear();
}