共用方式為


使用列印預覽列印

這在 Windows Forms 程式設計中很常見,除了列印服務,還提供預覽列印。 將列印預覽新增至您的應用程式的一種簡單方法,是結合使用 PrintPreviewDialog 控件和 PrintPage 事件處理邏輯來列印檔案。

使用 PrintPreviewDialog 控制項來預覽文字文件

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

  2. 從 [工具組] 窗格中,按兩下 PrintDocument 元件和 PrintPreviewDialog 元件,以將其新增至表單。

  3. Button 新增至表單,或使用已經在表單上的按鈕。

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

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

    // Declare a string to hold the entire document contents.
    private string documentContents="";
    
    // Declare a variable to hold the portion of the document that
    // is not printed.
    private string stringToPrint="";
    
    ' Declare a string to hold the entire document contents.
    Private documentContents As String
    ' Declare a variable to hold the portion of the document that
    ' is not printed.
    Private stringToPrint As String
    
  6. 回到 Click 事件處理常式程式碼中,將 DocumentName 屬性設定為您想要列印的文件,開啟文件,並將文件內容讀取至您之前加入的字串。

    string docName = "testPage.txt";
    string docPath = @"C:\";
    string fullPath = System.IO.Path.Combine(docPath, docName);
    printDocument1.DocumentName = docName;
    stringToPrint = System.IO.File.ReadAllText(fullPath);
    
    Dim docName As String = "testPage.txt"
    Dim docPath As String = "C:\Users\v-rsatao\Desktop\"
    Dim fullPath As String = System.IO.Path.Combine(docPath, docName)
    
    PrintDocument1.DocumentName = docName
    stringToPrint = System.IO.File.ReadAllText(fullPath)
    
  7. 就像平常在列印文件一樣,在 PrintPage 事件處理常式中,使用 Graphics 類別的 PrintPageEventArgs 屬性以及檔案內容來計算每一頁的行數,並轉譯文件的內容。 在繪製每一頁之後,請檢查該頁面是否為最後一頁,並且據此設定 PrintPageEventArgsHasMorePages 屬性。 在 PrintPage 成為 HasMorePages 之前,會持續引發 false事件。 當檔完成轉譯時,請重設要轉譯的字串。 此外,也請確定 PrintPage 事件與其事件處理方法相關聯。

    備註

    如果您已在應用程式中實作列印,步驟 5 和 6 可能已經完成。

    在下列程式碼範例中,會使用事件處理常式,以表單上使用的相同字型來列印 "testPage.txt" 檔案。

    void PrintDocument1_PrintPage(object sender, 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);
    
        // If there are no more pages, reset the string to be printed.
        if (!e.HasMorePages)
            stringToPrint = documentContents;
    }
    
    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
    
        ' If there are no more pages, reset the string to be printed.
        If Not e.HasMorePages Then
            stringToPrint = documentContents
        End If
    
    End Sub
    
  8. Document 控制項的 PrintPreviewDialog 屬性設為表單上的 PrintDocument 元件。

    printPreviewDialog1.Document = printDocument1;
    
    PrintPreviewDialog1.Document = PrintDocument1
    
  9. ShowDialog 控制項上呼叫 PrintPreviewDialog 方法。 請注意下列醒目提示的程式代碼,您通常會從ShowDialog按鈕的事件處理方法呼叫 Click 。 呼叫 ShowDialog 會引發 PrintPage 事件,並將輸出轉譯至 PrintPreviewDialog 控制項。 當使用者選取對話方塊中的列印圖示,就會再引發一次 PrintPage 事件,將輸出傳送至印表機,而不是預覽對話方塊。 因此,便會在步驟 4 中轉譯程序的最後重設字串。

    下列程式碼範例顯示針對表單上的按鈕所使用的 Click 事件處理方法。 這個事件處理方法會呼叫方法來讀取文件,並顯示列印預覽對話方塊。

    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);
    
        printPreviewDialog1.Document = printDocument1;
    
        printPreviewDialog1.ShowDialog();
    }
    
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
        Dim docName As String = "testPage.txt"
        Dim docPath As String = "C:\Users\v-rsatao\Desktop\"
        Dim fullPath As String = System.IO.Path.Combine(docPath, docName)
    
        PrintDocument1.DocumentName = docName
        stringToPrint = System.IO.File.ReadAllText(fullPath)
    
        PrintPreviewDialog1.Document = PrintDocument1
    
        PrintPreviewDialog1.ShowDialog()
    
    End Sub
    

另請參閱