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

            }
        
        }
    }
}

 

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