Windows ベースのアプリケーションではテキストを印刷するのが一般的です。 Graphics クラスは、画面やプリンターなどのデバイスにオブジェクト (グラフィックスまたはテキスト) を描画するためのメソッドを提供します。 次のセクションでは、テキスト ファイルを印刷するプロセスについて詳しく説明します。 このメソッドは、Office Word 文書や PDF ファイルなど、プレーンテキスト以外のファイルの印刷をサポートしていません。
注
DrawTextのTextRendererメソッドは印刷ではサポートされていません。 次のコード例に示すように、DrawStringの Graphics メソッドを使用して、印刷目的でテキストを描画する必要があります。
テキストを印刷するには
Visual Studio で、ソリューション エクスプローラーの [] ウィンドウで、印刷するフォームをダブルクリックします。 このアクションにより、ビジュアル デザイナーが開きます。
ツールボックスで、PrintDocument コンポーネントをダブルクリックしてフォームに追加します。 このアクションにより、
PrintDocument
という名前のprintDocument1
コンポーネントが作成されます。フォームに
Button
を追加するか、フォームに既に存在するボタンを使用します。フォームのビジュアル デザイナーで、ボタンを選択します。 プロパティ ペインで、[イベント フィルター] ボタンを選択し、
Click
イベントをダブルクリックしてイベント ハンドラーを生成します。Click
イベント コードを表示する必要があります。 イベント ハンドラーのスコープ外で、stringToPrint
という名前のクラスにプライベート文字列変数を追加します。private string stringToPrint="";
'Private PrintDocument1 As New PrintDocument() Private stringToPrint As String
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
フォームのビジュアル デザイナーに戻り、
PrintDocument
コンポーネントを選択します。 [のプロパティ] ウィンドウで、イベント フィルターを選択し、PrintPage
イベントをダブルクリックしてイベント ハンドラーを生成します。PrintPage イベント ハンドラーで、Graphics クラスの PrintPageEventArgs プロパティとドキュメントコンテンツを使用して、行の長さと行数をページごとに計算します。 各ページが描画されたら、それが最後のページであるかどうかを確認し、それに応じて HasMorePages の
PrintPageEventArgs
プロパティを設定します。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
こちらも参照ください
.NET Desktop feedback