PrintPreview在C#Windows应用程序中显示结果时打印空白 [英] Printing Is Coming Blank while PrintPreview Shows The Result In C# Windows Application
本文介绍了PrintPreview在C#Windows应用程序中显示结果时打印空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个Windows应用程序,它在PrintPreview的2个页面中显示数据,但是当我在PrintPreview中单击打印图标按钮时它打印空白页?
下面是我的代码:
// 打印文档的页面。
private int NextPageNum = 0 跨度>;
// 事件'PrintPage'的结果
< span class =code-keyword> private void prnDocument_PrintPage( object sender,System。 Drawing.Printing.PrintPageEventArgs e)
{
float pageWidth = e.PageSettings.PrintableArea.Width;
float pageHeight = e.PageSettings.PrintableArea.Height;
leftMargin =( int )e.MarginBounds.Left;
rightMargin =( int )e.MarginBounds.Right;
topMargin =( int )e.MarginBounds.Top;
bottomMargin =( int )e.MarginBounds.Bottom;
InvoiceWidth =( int )e.MarginBounds.Width;
InvoiceHeight =( int )e.MarginBounds.Height;
switch (NextPageNum)
{
case 0 :
RectangleF rectF1 = new RectangleF( 90 , 45 ,pageWidth-85,pageHeight-35);
Pen rectPen = new Pen(System.Drawing.Color.Black, 1 );
e.Graphics.DrawRectangle(rectPen,Rectangle.Round(rectF1));
RectangleF rectF2 = new RectangleF( 95 , 230 ,pageWidth - 95 ,pageHeight - 225 );
// Pen rectPen = new Pen(System.Drawing.Color.Black,2);
e.Graphics.DrawRectangle(rectPen,Rectangle.Round(rectF2));
SetInvoiceHead(e.Graphics); // 绘制发票头
SetOrderData(e.Graphics); // 绘制订单数据
SetInvoiceData(e.Graphics,e); // 提取发票数据
break 跨度>;
case 1 :
ImageShower(e.Graphics);
break ;
}
// 下次打印下一页。
NextPageNum + = 1 ;
// 如果我们尚未打印第2页,我们会有更多页面。
e.HasMorePages =(NextPageNum< 2);
}
private void prnDocument_BeginPrint( object sender,PrintEventArgs e)
{
/ / 从第0页开始。
NextPageNum = 0 ;
}
private void btnPreview_Click( object sender,EventArgs e)
{
NextPageNum = 0 跨度>;
retriveData();
foreBrush = FontforeColor();
prnPreview.Document = prnDocument;
((Form)prnPreview).WindowState = FormWindowState.Maximized;
// 显示100%预览
prnPreview.PrintPreviewControl.StartPage = < span class =code-digit> 0 ;
prnPreview.PrintPreviewControl.Zoom = 1 。 0 ;
prnPreview.PrintPreviewControl.Columns = 1 ;
prnPreview.ShowDialog();
}
private void btnPrint_Click( object sender,EventArgs e)
{
尝试
{
prnDocument.Print();
}
catch (例外e)
{
MessageBox.Show(e.ToString());
}
}
解决方案
我找到了它...我已在 end 中提及 prnDocument_PrintPage,然后它 打印数据 PrintPreview ..
// 如果我们没有更多页面,请重置 下一个时间我们 print 。
if (NextPageNum> 1 )NextPageNum = 0 跨度>;
I have a Windows Application That Showsn Data in 2 Pages in PrintPreview, but It Is Printing Blank Pages When I Click The Print Icon Button In PrintPreview ?
Below Is My Code :
// Print the document's pages.
private int NextPageNum = 0;
// Result of the Event 'PrintPage'
private void prnDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
float pageWidth = e.PageSettings.PrintableArea.Width;
float pageHeight = e.PageSettings.PrintableArea.Height;
leftMargin = (int)e.MarginBounds.Left;
rightMargin = (int)e.MarginBounds.Right;
topMargin = (int)e.MarginBounds.Top;
bottomMargin = (int)e.MarginBounds.Bottom;
InvoiceWidth = (int)e.MarginBounds.Width;
InvoiceHeight = (int)e.MarginBounds.Height;
switch (NextPageNum)
{
case 0:
RectangleF rectF1 = new RectangleF(90, 45, pageWidth-85, pageHeight-35);
Pen rectPen = new Pen(System.Drawing.Color.Black, 1);
e.Graphics.DrawRectangle(rectPen, Rectangle.Round(rectF1));
RectangleF rectF2 = new RectangleF(95, 230, pageWidth - 95, pageHeight - 225);
//Pen rectPen = new Pen(System.Drawing.Color.Black, 2);
e.Graphics.DrawRectangle(rectPen, Rectangle.Round(rectF2));
SetInvoiceHead(e.Graphics); // Draw Invoice Head
SetOrderData(e.Graphics); // Draw Order Data
SetInvoiceData(e.Graphics, e); // Draw Invoice Data
break;
case 1:
ImageShower(e.Graphics);
break;
}
// Next time print the next page.
NextPageNum += 1;
// We have more pages if we have not yet printed page 2.
e.HasMorePages = (NextPageNum <2);
}
private void prnDocument_BeginPrint(object sender, PrintEventArgs e)
{
// Start with page 0.
NextPageNum = 0;
}
private void btnPreview_Click(object sender, EventArgs e)
{
NextPageNum = 0;
retriveData();
foreBrush = FontforeColor();
prnPreview.Document = prnDocument;
((Form)prnPreview).WindowState = FormWindowState.Maximized;
// To Display 100% Preview
prnPreview.PrintPreviewControl.StartPage = 0;
prnPreview.PrintPreviewControl.Zoom = 1.0;
prnPreview.PrintPreviewControl.Columns = 1;
prnPreview.ShowDialog();
}
private void btnPrint_Click(object sender, EventArgs e)
{
try
{
prnDocument.Print();
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
解决方案
i had found it...i had mentioned as below at the end of prnDocument_PrintPage, then it is printing data as in PrintPreview.. // If we have no more pages, reset for the next time we print. if (NextPageNum > 1) NextPageNum = 0;
这篇关于PrintPreview在C#Windows应用程序中显示结果时打印空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文