Share via


列印多頁文字檔 (Windows Forms .NET)

Windows 應用程式通常會列印文字。 Graphics 類別提供將物件 (圖形或文字) 繪製到螢幕或印表機等裝置的方法。 下一節將詳細說明列印文字檔的程式。 此方法不支援列印非純文字檔,例如 Office Word 檔或 PDF 檔案。

注意

TextRendererDrawText 方法不支援列印。 您應該一律使用 GraphicsDrawString 方法,來繪製要用於列印的文字,如下列程式碼範例所示:

列印文字

  1. 在 Visual Studio 中,在 [方案總管 ] 窗格中按兩下您要列印 的表單。 這會開啟視覺化設計工具。

  2. 在 [ 工具箱 ] 中,按兩下 PrintDocument 元件以將它新增至表單。 這應該會建立 PrintDocument 名稱為 的 printDocument1 元件。

  3. 將 加入 Button 至表單,或使用已經在表單上的按鈕。

  4. 在表單的 [視覺化設計工具] 中,選取按鈕。 在 [屬性] 窗格中,選取 [ 事件 篩選] 按鈕,然後按兩下 Click 事件來產生事件處理常式。

  5. 事件 Click 程式碼應該會顯示。 在事件處理常式範圍之外,將私用字串變數新增至名為 stringToPrint 的類別。

    private string stringToPrint="";
    
    'Private PrintDocument1 As New PrintDocument()
    Private stringToPrint As String
    
  6. Click回到事件處理常式程式碼,將 DocumentName 屬性設定為檔的名稱。 此資訊會傳送至印表機。 接下來,讀取檔文字內容,並將其儲存在字串中 stringToPrint 。 最後,呼叫 Print 方法來引發 PrintPage 事件。 方法 Print 會反白顯示于下方。

    private void button1_Click(object sender, EventArgs e)
    {
        string docName = "testPage.txt";
        string docPath = @"C:\";
        string fullPath = System.IO.Path.Combine(docPath, docName);
    
        printDocument1.DocumentName = docName;
    
        stringToPrint = System.IO.File.ReadAllText(fullPath);
        
        printDocument1.Print();
    }
    
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
        Dim docName As String = "testPage.txt"
        Dim docPath As String = "C:\"
        Dim fullPath As String = System.IO.Path.Combine(docPath, docName)
    
        PrintDocument1.DocumentName = docName
    
        stringToPrint = System.IO.File.ReadAllText(fullPath)
        
        PrintDocument1.Print()
    
    End Sub
    
  7. 返回表單的 [視覺化設計工具],然後選取 PrintDocument 元件。 在 [ 屬性] 窗格中,選取 [事件 篩選],然後按兩下 PrintPage 事件以產生事件處理常式。

  8. PrintPage 事件處理常式中,使用 PrintPageEventArgs 類別的 Graphics 屬性和文件內容來計算行的長度和每頁的行數。 繪製每個頁面之後,請檢查其是否為最後一頁,並據此設定 HasMorePagesPrintPageEventArgs 屬性。 在 PrintPage 成為 HasMorePages 之前,會持續引發 false事件。

    在下列程式碼範例中,事件處理常式是用來以與表單上所使用的相同字型列印 「testPage.txt」 檔案的內容。

    private void PrintDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        int charactersOnPage = 0;
        int linesPerPage = 0;
    
        // Sets the value of charactersOnPage to the number of characters
        // of stringToPrint that will fit within the bounds of the page.
        e.Graphics.MeasureString(stringToPrint, this.Font,
            e.MarginBounds.Size, StringFormat.GenericTypographic,
            out charactersOnPage, out linesPerPage);
    
        // Draws the string within the bounds of the page
        e.Graphics.DrawString(stringToPrint, this.Font, Brushes.Black,
            e.MarginBounds, StringFormat.GenericTypographic);
    
        // Remove the portion of the string that has been printed.
        stringToPrint = stringToPrint.Substring(charactersOnPage);
    
        // Check to see if more pages are to be printed.
        e.HasMorePages = (stringToPrint.Length > 0);
    }
    
    Private Sub PrintDocument1_PrintPage(ByVal sender As Object,
    ByVal e As PrintPageEventArgs) Handles PrintDocument1.PrintPage
    
        Dim charactersOnPage As Integer = 0
        Dim linesPerPage As Integer = 0
    
        ' Sets the value of charactersOnPage to the number of characters 
        ' of stringToPrint that will fit within the bounds of the page.
        e.Graphics.MeasureString(stringToPrint, Me.Font, e.MarginBounds.Size,
        StringFormat.GenericTypographic, charactersOnPage, linesPerPage)
    
        ' Draws the string within the bounds of the page
        e.Graphics.DrawString(stringToPrint, Me.Font, Brushes.Black,
        e.MarginBounds, StringFormat.GenericTypographic)
    
        ' Remove the portion of the string that has been printed.
        stringToPrint = stringToPrint.Substring(charactersOnPage)
    
        ' Check to see if more pages are to be printed.
        e.HasMorePages = stringToPrint.Length > 0
    
    End Sub
    

另請參閱