iT邦幫忙

0

C# 為什麼預覽列印顯示地跟實際列印的不一樣?第一頁多了個有頁碼的空白頁

JUI 2021-11-04 23:48:242903 瀏覽

預覽列印:
https://ithelp.ithome.com.tw/upload/images/20211105/20143710fKoIMwRdVm.png
實際列印:
https://ithelp.ithome.com.tw/upload/images/20211105/20143710FlBjHmCHVA.png

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);
}

我要預覽列印的結果,但實際印卻不一樣,不知道問題在哪?

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
2
海綿寶寶
iT邦大神 1 級 ‧ 2021-11-05 09:50:19
最佳解答

做個小實驗
不要預覽列印(先 remark 掉)
PD.print(); 直接列印出來

結果有兩種可能
1.列印結果錯誤(多一頁):那就是我猜錯原因
2.列印結果正確,那就是程式原因,我猜是唯一沒有設定初值的LastSheet的原因,在PrintDoc_BeginPrint裡設初值試試看

JUI iT邦新手 5 級 ‧ 2021-11-05 10:17:26 檢舉

2.沒有設定初值的LastSheet的原因,在PrintDoc_BeginPrint裡設初值試試看 這就是解答 感謝!!!
在PrintDoc_BeginPrint裡放入
LastSheet = Convert.ToDouble(LvNumber[RowPos])
初值就解決了!

JUI iT邦新手 5 級 ‧ 2021-11-05 10:18:16 檢舉

感謝,工作上卡關~

恭喜問題解決

0
ckp6250
iT邦好手 1 級 ‧ 2021-11-05 07:44:44

每頁要印的列數,試著減少一列看看,比如,您本來預計每貢20列,就把它改成19列,看看有效否?

JUI iT邦新手 5 級 ‧ 2021-11-05 09:49:24 檢舉

我沒有列印欄位,只有列印數字

1
緯大啊緯大人
iT邦研究生 1 級 ‧ 2021-11-05 09:00:14

聽你的敘述感覺跟程式無關
頁首頁尾的選項有取消勾選嗎?
Chrome的如下:

https://ithelp.ithome.com.tw/upload/images/20211105/20135501X4A6L5cX3u.png

我要發表回答

立即登入回答