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

using System.Drawing.Drawing2D;
using System.Drawing.Text;

namespace PrintMultiplePages
{
    public partial class frm95PrintMultiplePages : Form
    {
        public frm95PrintMultiplePages()
        {
            InitializeComponent();
        }

        // Display a print preview.
        private void previewButton_Click(object sender, EventArgs e)
        {
            shapesPrintPreviewDialog.ShowDialog();
        }

        // Print.
        private void printButton_Click(object sender, EventArgs e)
        {
            shapesPrintDocument.Print();
        }

        // Print the document's pages.
        private int NextPageNum = 0;
        private void shapesPrintDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            e.Graphics.TextRenderingHint = TextRenderingHint.AntiAlias;

            // Draw a shape depending on the page we are printing.
            switch (NextPageNum)
            {
                case 0: // Draw an ellipse.
                    using (Pen pen = new Pen(Color.Red, 10))
                    {
                        e.Graphics.DrawEllipse(pen, e.MarginBounds);
                    }
                    break;
                case 1: // Draw a triangle.
                    using (Pen pen = new Pen(Color.Green, 10))
                    {
                        int xmid = (int)(e.MarginBounds.X + e.MarginBounds.Width / 2);
                        Point[] pts =
                        {
                            new Point(xmid, e.MarginBounds.Top),
                            new Point(e.MarginBounds.Right, e.MarginBounds.Bottom),
                            new Point(e.MarginBounds.Left, e.MarginBounds.Bottom),
                        };
                        e.Graphics.DrawPolygon(pen, pts);
                    }
                    break;
                case 2: // Draw a rectangle.
                    using (Pen pen = new Pen(Color.Blue, 10))
                    {
                        e.Graphics.DrawRectangle(pen, e.MarginBounds);
                    }
                    break;
                case 3: // Draw a diamond.
                    using (Pen pen = new Pen(Color.Orange, 10))
                    {
                        int xmid = (int)(e.MarginBounds.X + e.MarginBounds.Width / 2);
                        int ymid = (int)(e.MarginBounds.Y + e.MarginBounds.Height / 2);
                        Point[] pts =
                        {
                            new Point(xmid, e.MarginBounds.Top),
                            new Point(e.MarginBounds.Right, ymid),
                            new Point(xmid, e.MarginBounds.Bottom),
                            new Point(e.MarginBounds.Left, ymid),
                        };
                        e.Graphics.DrawPolygon(pen, pts);
                    }
                    break;
            }

            // Draw the page number centered.
            using (StringFormat sf = new StringFormat())
            {
                sf.Alignment = StringAlignment.Center;
                sf.LineAlignment = StringAlignment.Center;

                using (Font font = new Font("Times New Roman", 200, FontStyle.Bold))
                {
                    using (Brush brush = new SolidBrush(Color.Black))
                    {
                        e.Graphics.DrawString(String.Format("{0}", NextPageNum + 1),
                            font, brush, e.MarginBounds, sf);
                    }
                }
            }

            // Next time print the next page.
            NextPageNum += 1;

            // We have more pages if we have not yet printed page 3.
            e.HasMorePages = (NextPageNum <= 3);

            // If we have no more pages, reset for the next time we print.
            if (NextPageNum > 3) NextPageNum = 0;
        }


        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frm95PrintMultiplePages));
            this.printButton = new System.Windows.Forms.Button();
            this.previewButton = new System.Windows.Forms.Button();
            this.shapesPrintPreviewDialog = new System.Windows.Forms.PrintPreviewDialog();
            this.shapesPrintDocument = new System.Drawing.Printing.PrintDocument();
            this.SuspendLayout();
            // 
            // printButton
            // 
            this.printButton.Anchor = System.Windows.Forms.AnchorStyles.None;
            this.printButton.Location = new System.Drawing.Point(155, 44);
            this.printButton.Name = "printButton";
            this.printButton.Size = new System.Drawing.Size(75, 23);
            this.printButton.TabIndex = 3;
            this.printButton.Text = "Print";
            this.printButton.UseVisualStyleBackColor = true;
            this.printButton.Click += new System.EventHandler(this.printButton_Click);
            // 
            // previewButton
            // 
            this.previewButton.Anchor = System.Windows.Forms.AnchorStyles.None;
            this.previewButton.Location = new System.Drawing.Point(55, 44);
            this.previewButton.Name = "previewButton";
            this.previewButton.Size = new System.Drawing.Size(75, 23);
            this.previewButton.TabIndex = 2;
            this.previewButton.Text = "Preview";
            this.previewButton.UseVisualStyleBackColor = true;
            this.previewButton.Click += new System.EventHandler(this.previewButton_Click);
            // 
            // shapesPrintPreviewDialog
            // 
            this.shapesPrintPreviewDialog.AutoScrollMargin = new System.Drawing.Size(0, 0);
            this.shapesPrintPreviewDialog.AutoScrollMinSize = new System.Drawing.Size(0, 0);
            this.shapesPrintPreviewDialog.ClientSize = new System.Drawing.Size(400, 300);
            this.shapesPrintPreviewDialog.Document = this.shapesPrintDocument;
            this.shapesPrintPreviewDialog.Enabled = true;
            this.shapesPrintPreviewDialog.Icon = ((System.Drawing.Icon)(resources.GetObject("shapesPrintPreviewDialog.Icon")));
            this.shapesPrintPreviewDialog.Name = "ppdShapes";
            this.shapesPrintPreviewDialog.Visible = false;
            // 
            // shapesPrintDocument
            // 
            this.shapesPrintDocument.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.shapesPrintDocument_PrintPage);
            // 
            // frm95PrintMultiplePages
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 111);
            this.Controls.Add(this.printButton);
            this.Controls.Add(this.previewButton);
            this.Name = "frm95PrintMultiplePages";
            this.Text = "PrintMultiplePages";
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Button printButton;
        private System.Windows.Forms.Button previewButton;
        private System.Windows.Forms.PrintPreviewDialog shapesPrintPreviewDialog;
        private System.Drawing.Printing.PrintDocument shapesPrintDocument;
    }
}