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

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace JzyyTiJian.WinMain
{
    public partial class Form7 : Form
    {
        public Form7()
        {
            InitializeComponent();
        }
        private int currentPageIndex = 0;
        private int rowCount = 0;
        private int pageCount = 3;
        private void Form7_Load(object sender, EventArgs e)
        {
            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 = printDoc;
            printDoc.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("A4", 1123, 794);
            printDoc.DefaultPageSettings.Margins = new System.Drawing.Printing.Margins(60, 60, 60, 60); //設置邊距             
            ppvw.Document = printDoc;   //設置要打印的文檔 
            ((Form)ppvw).WindowState = FormWindowState.Maximized; //最大化  
            printDoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printDoc_PrintPage); //打印事件 
            //printDoc.EndPrint += new System.Drawing.Printing.PrintEventHandler(printDoc_EndPrint);
            ppvw.Document.DefaultPageSettings.Landscape = true;    // 設置打印爲橫向               
            ppvw.ShowDialog(); //打開預覽
        }

        private void printDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            Font titleFont = new Font("宋體", 12, System.Drawing.FontStyle.Regular);//標題字體
            System.Drawing.Brush brush = new SolidBrush(System.Drawing.Color.Black);//畫刷   
           
            if (currentPageIndex == 0)   //當爲第一頁時
            {
                e.Graphics.DrawString("111111111111111111111111111", titleFont, brush, new Point(0, 0));

            }
            else if (currentPageIndex == 1)   //當爲第二頁時
            {
                e.Graphics.DrawString("22222222222222222", titleFont, brush, new Point(0, 0));

            }
            else if (currentPageIndex == 2)
            {


                ThirdPage(e,currentPageIndex);


            }
            ThirdPage(e, 0);





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

        }

        public void ThirdPage(System.Drawing.Printing.PrintPageEventArgs e, int PageIndex)
        {
            Font titleFont = new Font("宋體", 12, System.Drawing.FontStyle.Regular);//標題字體
            System.Drawing.Brush brush = new SolidBrush(System.Drawing.Color.Black);//畫刷   

            if (PageIndex > 0)
            {
                if (currentPageIndex == 2)   //當爲第3頁時
                {
                    e.Graphics.DrawString("3333333333", titleFont, brush, new Point(0, 0));

                }
                pageCount = pageCount + 3;
            }
            else if (currentPageIndex == 3)   //當爲第4頁時
            {
                e.Graphics.DrawString("444444444", titleFont, brush, new Point(0, 0));

            }
            else if (currentPageIndex == 4)   //當爲第5頁時
            {
                e.Graphics.DrawString("5555555555555555", titleFont, brush, new Point(0, 0));

            }
            else if (currentPageIndex == 5)   //當爲第5頁時
            {
                e.Graphics.DrawString("666666666666666", titleFont, brush, new Point(0, 0));

            }
        
        }
    }
}

 

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