預覽列印:
實際列印:
private static int RowPos; // Position of currently printing row
private static bool NewPage; // Indicates if a new page reached
private static int PageNo; // Number of pages to print
private static int RowsPerPage; // Number of Rows per Page 一頁幾行
static double LastSheet;//判斷領單是否與上一筆相同
static string[] LvNumber = new string[] { "1", "1", "1", "1", "1", "1", "2", "2", "2", "2", "2" };
public static void Print_DataGridView()
{
PrintPreviewDialog ppvw;
RowsPerPage = 0;
PrintDocument PD = new PrintDocument();
LastSheet = Convert.ToDouble(LvNumber[0]);
PD.PrinterSettings.PrinterName = "Microsoft Print to PDF";
// Showing the Print Preview Page
PD.BeginPrint += new System.Drawing.Printing.PrintEventHandler(PrintDoc_BeginPrint);
PD.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(PrintDoc_PrintPage);
ppvw = new PrintPreviewDialog();
ppvw.Document = PD;
ppvw.ShowDialog();
PD.BeginPrint -= new System.Drawing.Printing.PrintEventHandler(PrintDoc_BeginPrint);
PD.PrintPage -= new System.Drawing.Printing.PrintPageEventHandler(PrintDoc_PrintPage);
}
private static void PrintDoc_BeginPrint(object sender,System.Drawing.Printing.PrintEventArgs e)
{
PageNo = 1;
NewPage = true;
RowPos = 0;
}
private static void PrintDoc_PrintPage(object sender,System.Drawing.Printing.PrintPageEventArgs e)
{
e.HasMorePages = false;
while (RowPos <= LvNumber.Length - 1)
{
////////換頁!!!!!
if (LastSheet != Convert.ToDouble(LvNumber[RowPos]))
{
DrawFooter( e);
PageNo++;
NewPage = true;
e.HasMorePages = true;
LastSheet = Convert.ToDouble(LvNumber[RowPos]);
return;
}
else
{
if (NewPage)
{
string StrLeftTitle = "專案編號";
e.Graphics.DrawString(StrLeftTitle, new Font("標楷體", 12, FontStyle.Bold),Brushes.Black, e.MarginBounds.Left - 70, e.MarginBounds.Top -e.Graphics.MeasureString(StrLeftTitle, new Font("標楷體", 12, FontStyle.Bold), e.MarginBounds.Width).Height - 13);
NewPage = false;
}
e.Graphics.DrawString(LvNumber[RowPos].ToString(), new Font("標楷體", 12, FontStyle.Bold),Brushes.Black, e.MarginBounds.Left + (RowPos * 10), e.MarginBounds.Top -e.Graphics.MeasureString(LvNumber[RowPos].ToString(), new Font("標楷體", 12, FontStyle.Bold), e.MarginBounds.Width).Height + 50);
}
LastSheet = Convert.ToDouble(LvNumber[RowPos]);
e.HasMorePages = false;
RowPos++;
}
DrawFooter(e);
e.HasMorePages = false;
}
private static void DrawFooter(System.Drawing.Printing.PrintPageEventArgs e)
{
// Detemining rows number to print
string PageNum = "";
// Writing the Page Number on the Bottom of Page
PageNum = PageNo.ToString();
e.Graphics.DrawString(PageNum, new Font("標楷體", 12, FontStyle.Bold), Brushes.Black,e.MarginBounds.Left + (e.MarginBounds.Width -e.Graphics.MeasureString(PageNum, new Font("標楷體", 12, FontStyle.Bold),e.MarginBounds.Width).Width) / 2, e.MarginBounds.Top +e.MarginBounds.Height);
}
我要預覽列印的結果,但實際印卻不一樣,不知道問題在哪?
做個小實驗
不要預覽列印(先 remark 掉)
用 PD.print();
直接列印出來
結果有兩種可能
1.列印結果錯誤(多一頁):那就是我猜錯原因
2.列印結果正確,那就是程式原因,我猜是唯一沒有設定初值的LastSheet
的原因,在PrintDoc_BeginPrint
裡設初值試試看
每頁要印的列數,試著減少一列看看,比如,您本來預計每貢20列,就把它改成19列,看看有效否?