閱讀英文

共用方式為


PrintDocument 類別

定義

定義從 Windows Forms 應用程式中列印時會將輸出傳送至印表機的可重複使用物件。

C#
public class PrintDocument : System.ComponentModel.Component
繼承

範例

下列程式代碼範例會在預設印表機上列印名為 C:\My Documents\MyFile.txt 的檔案。 若要執行此範例,請建立新的 Windows Forms 專案,並將範例程式代碼貼到窗體中,並取代檔案內容。 針對 C#,您必須刪除 Form1。Designer.cs 檔案。 此外,將路徑變更為您要列印的檔案。

注意

此範例會要求每一行都符合頁面寬度。

C#
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);
    }
}

備註

一般而言,您會建立 類別的PrintDocument實例、設定 和 等DocumentNamePrinterSettings屬性,並呼叫 Print 方法來啟動列印程式。 PrintPage使用 的 屬性,GraphicsGraphics處理指定要列印之輸出的事件PrintPageEventArgs

如需從 Windows Form 應用程式列印的詳細資訊,請參閱 Windows Forms 列印支援。 如果您想要從 Windows Presentation Foundation 應用程式列印,請參閱 System.Printing 命名空間。

注意

在 .NET 6 和更新版本中,只有 Windows 作業系統才支援包含此類型的 System.Drawing.Common 套件。 在跨平臺應用程式中使用此類型會導致編譯時間警告和運行時間例外狀況。 如需詳細資訊,請參閱 僅限 Windows 上支援的 System.Drawing.Common

建構函式

PrintDocument()

初始化 PrintDocument 類別的新執行個體。

屬性

CanRaiseEvents

取得值,指出元件是否能引發事件。

(繼承來源 Component)
Container

取得包含 IContainerComponent

(繼承來源 Component)
DefaultPageSettings

取得或設定頁面設定,其會使用為所有要列印頁面的預設設定。

DesignMode

取得值,指出 Component 目前是否處於設計模式。

(繼承來源 Component)
DocumentName

取得或設定要在列印文件時顯示的文件名稱 (例如,在列印狀態對話方塊中或在印表機佇列中)。

Events

取得附加在這個 Component 上的事件處理常式清單。

(繼承來源 Component)
OriginAtMargins

取得或設定值,指出與頁面相關的圖形物件位置是否正好在使用者指定的邊界內部,或在頁面可列印區域的左上角。

PrintController

取得或設定指引列印程序的列印控制程式。

PrinterSettings

取得或設定列印文件的印表機。

Site

取得或設定 ComponentISite

(繼承來源 Component)

方法

CreateObjRef(Type)

建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。

(繼承來源 MarshalByRefObject)
Dispose()

釋放 Component 所使用的所有資源。

(繼承來源 Component)
Dispose(Boolean)

釋放 Component 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。

(繼承來源 Component)
Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetLifetimeService()
已淘汰.

擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。

(繼承來源 MarshalByRefObject)
GetService(Type)

傳回表示 Component 或其 Container 所提供之服務的物件。

(繼承來源 Component)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
InitializeLifetimeService()
已淘汰.

取得存留期服務物件,以控制這個執行個體的存留期原則。

(繼承來源 MarshalByRefObject)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
MemberwiseClone(Boolean)

建立目前 MarshalByRefObject 物件的淺層複本。

(繼承來源 MarshalByRefObject)
OnBeginPrint(PrintEventArgs)

引發 BeginPrint 事件。 在呼叫 Print() 方法之後和文件的第一頁列印之前呼叫它。

OnEndPrint(PrintEventArgs)

引發 EndPrint 事件。 在文件最後一頁已列印時呼叫它。

OnPrintPage(PrintPageEventArgs)

引發 PrintPage 事件。 在頁面列印之前呼叫它。

OnQueryPageSettings(QueryPageSettingsEventArgs)

引發 QueryPageSettings 事件。 在每個 PrintPage 事件之前立即呼叫它。

Print()

啟動文件的列印程序。

ToString()

提供列印文件的相關資訊 (以字串形式)。

事件

BeginPrint

發生於呼叫 Print() 方法時 (在文件的第一頁列印之前)。

Disposed

Dispose() 方法的呼叫處置元件時,就會發生。

(繼承來源 Component)
EndPrint

發生於文件最後一頁列印時。

PrintPage

發生於需要目前頁面的列印輸出時。

QueryPageSettings

PrintPage 事件之前立即發生。

適用於

產品 版本
.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

另請參閱