次の方法で共有


印刷プレビューを使用して印刷する

Windows フォーム プログラミングでは、印刷サービスに加えて印刷プレビューを提供するのが一般的です。 印刷プレビューをアプリケーションに追加する簡単な方法は、 PrintPreviewDialog コントロールと PrintPage イベント処理ロジックを組み合わせて使用してファイルを印刷することです。

PrintPreviewDialog コントロールを使用してテキスト ドキュメントをプレビューするには

  1. Visual Studio で、ソリューション エクスプローラーの ウィンドウを使用し、印刷するフォームをダブルクリックします。 ビジュアル デザイナーが開きます。

  2. ツールボックス ウィンドウで、PrintDocument コンポーネントと PrintPreviewDialog コンポーネントの両方をダブルクリックして、フォームに追加します。

  3. フォームに Button を追加するか、フォームに既に存在するボタンを使用します。

  4. フォームのビジュアル デザイナーで、ボタンを選択します。 プロパティ ペインで、[イベント フィルター] ボタンを選択し、Click イベントをダブルクリックしてイベント ハンドラーを生成します。

  5. Click イベント コードを表示する必要があります。 イベント ハンドラーのスコープ外で、documentContentsstringToPrintという名前のクラスに 2 つのプライベート文字列変数を追加します。

    // 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 プロパティとファイルの内容を使用して、ページあたりの行数を計算し、ドキュメントの内容をレンダリングします。 各ページが描画されたら、それが最後のページであるかどうかを確認し、それに応じて HasMorePagesPrintPageEventArgs プロパティを設定します。 PrintPage イベントは、HasMorePagesfalseされるまで発生します。 ドキュメントのレンダリングが完了したら、レンダリングする文字列をリセットします。 また、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
    

こちらも参照ください