Google
 
Web itpro-blogger.blogspot.com
木曜日, 11月 22, 2007

[VB2005]印刷関連

▼質問
Excelを使った印刷はしたことがあるんだけど、テキスト文字列を直接印刷したことはないんだ。
どうしたらいいんだい!?

▼回答
PrintDocumentコンポーネント(System.Drawing.Printing名前空間)を使えばできるよ。

サンプルを教えてやるから参考にしてくれ!

ボタンのクリックイベントに下記を記述する。
'印刷処理実行
Me.PrintDocument1.Print()

PrintDocument1コンポーネントのイベントを下記のように記述すればOK!

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage

Dim yPos As Single = 0
Dim count As Integer = 0
Dim line As String = Nothing

'フォント設定
Dim printFont As Font = New Font("MS 明朝", 10)

'ページ余白(内側部分)を取得
Dim topMargin As Single = e.MarginBounds.Top
Dim leftMargin As Single = e.MarginBounds.Left

'ページ行数取得.
Dim linesPerPage As Single = e.MarginBounds.Height / printFont.GetHeight(e.Graphics)

Dim stream As New IO.StringReader(テキスト文字列)

'文字列ストリーム印刷.
While (count < linesPerPage)
line = stream.ReadLine()
If (line Is Nothing) Then
Exit While
End If
yPos = topMargin + count * printFont.GetHeight(e.Graphics)
e.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, New StringFormat())
count += 1
End While

'追加ページの印刷があるかチェックする.
If Not (line Is Nothing) Then
e.HasMorePages = True
Else
e.HasMorePages = False
End If

stream.Close()

End Sub

■参考サイト
Windowsアプリケーションで印刷を行うには?
http://www.atmarkit.co.jp/fdotnet/dotnettips/393printdoc/printdoc.html

印刷のテストは、PDFに出力するようにすれば印刷のデバックがやり易くなるので、下記の
ソフトはお奨めだ。しかも、フリーソフトだからお得!?

XLsoft エクセルソフト : activePDF 無料 PDF 作成/変換ソフトウェア PrimoPDF 日本語版 - ホーム
http://www.xlsoft.com/jp/products/primopdf/index.html






Comments: コメントを投稿

<< Home

This page is powered by Blogger. Isn't yours?