我正在测试一个"打印预览"按钮,并且效果很好。关闭其PrintPreview窗口后,我再次单击了"打印预览"按钮,这次显示了一个空白页!我不知道为什么会发生这种情况。已经搜索了这个网站和许多其他网站,所以我发布了这个问题。任何想法都将不胜感激。
这是相关的代码:
private void buttonPrintPreview_Click(object sender, EventArgs e)
{
page = 0;
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.ShowDialog();
}
private void buttonPrint_Click(object sender, EventArgs e)
{
page = 0;
printDialog1.Document = printDocument1;
if (printDialog1.ShowDialog() == DialogResult.OK)
{
printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
printDocument1.DefaultPageSettings.Landscape = false;
printDocument1.Print();
//printDocument1.Dispose();
}
}
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
Graphics g = e.Graphics;
//define margins
float leftMargin = 70.0f; //good room for hole punches!
float topMargin = 20.0f;
float lineInc = 20.0f;
//--------------------------------------------
string eiNum = string.Empty;
string eiDesc = string.Empty;
string partNum = string.Empty;
string partDesc = string.Empty;
string price = string.Empty;
string partType = string.Empty;
string partQty = string.Empty;
string thisEndItem = string.Empty;
string textToPrint = string.Empty;
Font printFontArial10 = new Font("Arial", 10, FontStyle.Regular);
Font printFontArial10Bold = new Font("Arial", 10, FontStyle.Bold);
Font printFontArial14 = new Font("Arial", 14, FontStyle.Bold);
Font printFontCour8 = new Font("Courier New", 8, FontStyle.Regular);
if (page == 0 && counter == 0)
{
// draw image/logo
Image Logo = Image.FromFile(Settings.Default.LogoPath);
g.DrawImage(Logo, leftMargin, 35f);
// draw title
textToPrint = "TIW Purchasing - Master Buy List";
e.Graphics.DrawString(textToPrint, printFontArial14, Brushes.Black, leftMargin + 160f, 54f);
// date
DateTime thisDay = DateTime.Today;
textToPrint = thisDay.ToString("d");
e.Graphics.DrawString(textToPrint, printFontArial10, Brushes.Black, leftMargin + 280f, 76f);
eiNum = listView1.Items[0].Text;
eiDesc = listView1.Items[0].SubItems[1].Text;
if (eiDesc.Length > 80)
eiDesc = eiDesc.Substring(0, 80) + "...";
textToPrint = eiNum + " - " + eiDesc;
e.Graphics.DrawString(textToPrint, printFontArial10Bold, Brushes.Black, leftMargin, topMargin + 90);
}
else if (page > 0)
{
double remainder = counter % amtperpage;
if (remainder == 0) //---means we're at the top of the page
{
//title & version
textToPrint = "TIW Purchasing - Master Buy List";
e.Graphics.DrawString(textToPrint, printFontArial10Bold, Brushes.Black, leftMargin + 120, 54f);
textToPrint = "(continued from page " + page + ")";
e.Graphics.DrawString(textToPrint, printFontArial10, Brushes.Black, leftMargin + 400, 54f);
}
}
if (page == 0)
lineInc = 90;
else
lineInc = 78;
int stop = counter + amtperpage;
if (stop > listView1.Items.Count)
stop = listView1.Items.Count;
while (counter < stop)
{
thisEndItem = listView1.Items[counter].SubItems[0].Text;
partNum = listView1.Items[counter].SubItems[2].Text;
partDesc = listView1.Items[counter].SubItems[3].Text;
price = listView1.Items[counter].SubItems[4].Text;
partType = listView1.Items[counter].SubItems[5].Text;
partQty = listView1.Items[counter].SubItems[6].Text;
if (thisEndItem == eiNum) //---still working on the same end item
{
lineInc += 12;
textToPrint = partNum;
e.Graphics.DrawString(textToPrint, printFontArial10, Brushes.Black, leftMargin + 10, topMargin + lineInc);
if (partDesc.Length > 70)
partDesc = partDesc.Substring(0, 70) + "...";
textToPrint = partDesc;
e.Graphics.DrawString(textToPrint, printFontArial10, Brushes.Black, leftMargin + 70, topMargin + lineInc);
textToPrint = price;
e.Graphics.DrawString(textToPrint, printFontArial10, Brushes.Black, leftMargin + 600, topMargin + lineInc);
textToPrint = partType;
e.Graphics.DrawString(textToPrint, printFontArial10, Brushes.Black, leftMargin + 630, topMargin + lineInc);
textToPrint = partQty;
e.Graphics.DrawString(textToPrint, printFontArial10, Brushes.Black, leftMargin + 670, topMargin + lineInc);
}
else //---starting a new end item
{
lineInc += 16;
eiNum = listView1.Items[counter].Text;
eiDesc = listView1.Items[counter].SubItems[1].Text;
if (eiDesc.Length > 80)
eiDesc = eiDesc.Substring(0, 80) + "...";
textToPrint = eiNum + " - " + eiDesc;
e.Graphics.DrawString(textToPrint, printFontArial10Bold, Brushes.Black, leftMargin, topMargin + lineInc);
}
counter++;
}
//---footer-------------------------------
DateTime dateTime = DateTime.Now;
textToPrint = "eView " + EViewMethods.eviewVersion + " " + Environment.UserName + " " + String.Format("{0:F}", dateTime);
e.Graphics.DrawString(textToPrint, printFontCour8, Brushes.Black, leftMargin, 1060f);
printpagenum = page + 1;
textToPrint = printpagenum.ToString();
e.Graphics.DrawString(textToPrint, printFontArial10Bold, Brushes.Black, leftMargin + 740, 1060f);
//----------------------------------------
page++;
e.HasMorePages = counter < listView1.Items.Count;
}
这是校正的代码:
private void buttonPrintPreview_Click(object sender, EventArgs e)
{
page = 0;
counter = 0;
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.ShowDialog();
}
private void buttonPrint_Click(object sender, EventArgs e)
{
page = 0;
counter = 0;
printDialog1.Document = printDocument1;
if (printDialog1.ShowDialog() == DialogResult.OK)
{
printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
printDocument1.DefaultPageSettings.Landscape = false;
printDocument1.Print();
//printDocument1.Dispose();
}
}
public Form1()
{
InitializeComponent();
printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
}