Share via


複数ページのテキスト ファイルを印刷する (Windows フォーム .NET)

Windows ベースのアプリケーションでは、テキストを印刷することがよくあります。 Graphics クラスは、画面やプリンターなどのデバイスにオブジェクト (グラフィックスやテキスト) を描画するためのメソッドを提供します。 次のセクションでは、テキスト ファイルを印刷するプロセスについて詳しく説明します。 このメソッドは、Office Word 文書や PDF ファイルなど、プレーンテキスト以外のファイルの印刷はサポートしません。

注意

TextRendererDrawText メソッドでは、印刷はサポートされていません。 印刷用にテキストを描画するには、次のコード例で示すように、GraphicsDrawString メソッドを常に使用する必要があります。

テキストを印刷するには

  1. Visual Studio の [ソリューション エクスプローラー] ペインで、印刷元のフォームをダブルクリックします。 これでビジュアル デザイナーが開きます。

  2. ツールボックスで、PrintDocument コンポーネントをダブルクリックしてフォームに追加します。 これにより、printDocument1 という名前の PrintDocument コンポーネントが作成されます。

  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 プロパティとドキュメントの内容を使用して、行の長さと 1 ページあたりの行数を計算します。 各ページを描画した後で、最後のページかどうかを調べて、PrintPageEventArgsHasMorePages プロパティを適切に設定します。 PrintPageHasMorePages になるまで 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
    

関連項目