c# winform 自定義模板 printDocument 自定義分頁打印

//按鈕事件

        PrintPreviewDialog ppvw = new PrintPreviewDialog();
            ppvw.PrintPreviewControl.Zoom = 1.0; //顯示比例爲100%
           // System.Drawing.Printing.PrintDocument printDoc = new System.Drawing.Printing.PrintDocument();
            PrintDialog MyDlg = new PrintDialog();
            MyDlg.Document = this.printDocument1;
            this.printDocument1.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("A4", 850, 1000);
            this.printDocument1.DefaultPageSettings.Margins = new System.Drawing.Printing.Margins(60, 60, 60, 60); //設置邊距             
            ppvw.Document = this.printDocument1;   //設置要打印的文檔 
            ((Form)ppvw).WindowState = FormWindowState.Maximized; //最大化  
            this.printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printDocument_PrintPage); //打印事件 
      
            ppvw.Document.DefaultPageSettings.Landscape = true;    // 設置打印爲橫向               
            ppvw.ShowDialog(); //打開預覽

 

  private int currentPageIndex = 0;
        private int rowCount = 0;
        private int pageCount = 0;

        private void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            Font fntTxt = new Font("宋體", 11, FontStyle.Regular);    //正文文字
            Brush brush = new SolidBrush(Color.Black);//畫刷 
            Pen pen = new Pen(Color.Black);           //線條顏色
            pageCount = 3;     //定義頁數

            if (currentPageIndex == 0)   //當爲第一頁時
            {
                e.Graphics.DrawString("111111111111111111111111111", fntTxt, brush, new Point(0, 0));
                e.HasMorePages = true;
            }
            else if (currentPageIndex == 1)   //當爲第二頁時
            {
                e.Graphics.DrawString("222222222222222222222222222", fntTxt, brush, new Point(0, 120));
            }
            else if (currentPageIndex == 2)   //當爲第三頁時
            {
                e.Graphics.DrawString("333333333333333333333333333", fntTxt, brush, new Point(0, 240));
            }

            currentPageIndex++;      //加新頁
            if (currentPageIndex < pageCount)
            {
                e.HasMorePages = true;  //如果小於定義頁 那麼增加新的頁數
            }
            else
            {
                e.HasMorePages = false; //停止增加新的頁數
                currentPageIndex = 0;
            }
        }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章