PrintDocument.PrintPage イベント

定義

現在のページに印刷する出力が必要なときに発生します。

public event System.Drawing.Printing.PrintPageEventHandler PrintPage;

イベントの種類

次のコード例は、 イベントの処理を PrintPage 示しています。

別の例については、「方法: Windows フォームでマルチページ テキスト ファイルを印刷する」を参照してください。

using System;
using System.IO;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;

public partial class Form1 : System.Windows.Forms.Form
{
    private System.ComponentModel.Container components;
    private System.Windows.Forms.Button printButton;
    private Font printFont;
    private StreamReader streamToPrint;

    public Form1()
    {
        // The Windows Forms Designer requires the following call.
        InitializeComponent();
    }

    // The Click event is raised when the user clicks the Print button.
    private void printButton_Click(object sender, EventArgs e)
    {
        try
        {
            streamToPrint = new StreamReader
               ("C:\\My Documents\\MyFile.txt");
            try
            {
                printFont = new Font("Arial", 10);
                PrintDocument pd = new PrintDocument();
                pd.PrintPage += new PrintPageEventHandler
                   (this.pd_PrintPage);
                pd.Print();
            }
            finally
            {
                streamToPrint.Close();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    // The PrintPage event is raised for each page to be printed.
    private void pd_PrintPage(object sender, PrintPageEventArgs ev)
    {
        float linesPerPage = 0;
        float yPos = 0;
        int count = 0;
        float leftMargin = ev.MarginBounds.Left;
        float topMargin = ev.MarginBounds.Top;
        string line = null;

        // Calculate the number of lines per page.
        linesPerPage = ev.MarginBounds.Height /
           printFont.GetHeight(ev.Graphics);

        // Print each line of the file.
        while (count < linesPerPage &&
           ((line = streamToPrint.ReadLine()) != null))
        {
            yPos = topMargin + (count *
               printFont.GetHeight(ev.Graphics));
            ev.Graphics.DrawString(line, printFont, Brushes.Black,
               leftMargin, yPos, new StringFormat());
            count++;
        }

        // If more lines exist, print another page.
        if (line != null)
            ev.HasMorePages = true;
        else
            ev.HasMorePages = false;
    }

    // The Windows Forms Designer requires the following procedure.
    private void InitializeComponent()
    {
        this.components = new System.ComponentModel.Container();
        this.printButton = new System.Windows.Forms.Button();

        this.ClientSize = new System.Drawing.Size(504, 381);
        this.Text = "Print Example";

        printButton.ImageAlign =
           System.Drawing.ContentAlignment.MiddleLeft;
        printButton.Location = new System.Drawing.Point(32, 110);
        printButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
        printButton.TabIndex = 0;
        printButton.Text = "Print the file.";
        printButton.Size = new System.Drawing.Size(136, 40);
        printButton.Click += new System.EventHandler(printButton_Click);

        this.Controls.Add(printButton);
    }
}

注釈

印刷する出力を指定するには、 の プロパティを Graphics 使用します PrintPageEventArgs。 たとえば、印刷するテキスト行を指定するには、 メソッドを使用してテキストを Graphics.DrawString 描画します。

出力を指定するだけでなく、 プロパティtrueを に設定PrintPageEventArgs.HasMorePagesすることで、印刷するページが追加されているかどうかを示すことができます。 既定値は です false。これは、これ以上印刷するページがないことを示します。 個々のページ設定は を使用してPageSettings変更することもできます。また、 プロパティを にtrue設定することで印刷ジョブをPrintPageEventArgs.Cancel取り消すこともできます。 異なるページ設定を使用してドキュメントの各ページを印刷するには、 イベントを処理します QueryPageSettings

イベントをイベント ハンドラーに関連付けるには、デリゲートのインスタンスを PrintPageEventHandler イベントに追加します。 イベント ハンドラーは、イベントが発生するたびに呼び出されます。 デリゲートを使用したイベントの処理の詳細については、「イベントの 処理と発生」を参照してください。

適用対象

製品 バージョン
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

こちらもご覧ください