我是一名做 C# 專案的 PHP 開發人員。我正忙于一個 C# winform 專案。
在列印檔案時,我需要添加一個內容與第一頁不同的頁面。
說清楚。我需要兩頁,每一頁都有自己的內容。
目前它正在按預期列印 2 頁,但兩頁上的內容完全相同,這是我目前擁有的示例。
int currentpage = 0;
int numofpages = 2;
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
float pageHeight = e.MarginBounds.Height;
Bitmap bmp = Properties.Resources.someImage;
Image newImage = bmp;
e.Graphics.DrawImage(newImage, 20, 20);
e.Graphics.DrawString("More content", new Font("Verdana", 10, FontStyle.Bold), Brushes.Black, 600, 350);
currentpage ;
if (currentpage < numofpages)
{
e.HasMorePages = true;
Bitmap bmp = Properties.Resources.someOtherImage;
Image newImage = bmp;
e.Graphics.DrawImage(newImage, 20, 20);
e.Graphics.DrawString("Other content", new Font("Verdana", 10, FontStyle.Bold), Brushes.Black, 600, 350);
}
else
{
e.HasMorePages = false;
}
}
有沒有辦法用自己的內容創建第二個頁面?
我目前唯一的選擇是創建第二個功能printDocument2_PrintPage_1,但它對最終用戶不友好。
uj5u.com熱心網友回復:
正如我在評論中所說,您似乎正在嘗試在對事件處理程式的單個回呼期間呈現兩個頁面的內容。您應該改為執行以下操作:
int currentpage = 0;
int numofpages = 2;
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
currentpage ;
if(currentPage==1)
{
Bitmap bmp = Properties.Resources.someImage;
Image newImage = bmp;
e.Graphics.DrawImage(newImage, 20, 20);
e.Graphics.DrawString("More content", new Font("Verdana", 10,
FontStyle.Bold), Brushes.Black, 600, 350);
}
else if(currentPage == 2)
{
Bitmap bmp = Properties.Resources.someOtherImage;
Image newImage = bmp;
e.Graphics.DrawImage(newImage, 20, 20);
e.Graphics.DrawString("Other content", new Font("Verdana", 10,
FontStyle.Bold), Brushes.Black, 600, 350);
}
e.HasMorePages = currentPage < numofpages;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/522132.html
標籤:C#表格打印文件
