|
Imports System.Drawing.Printing
Imports System.IO
Imports System.Text
Public Class Form1
' 印刷処理の実体の定義
Private pd As PrintDocument = Nothing
' 印刷用オブジェクトと変数
Private pf As Font = New Font("MS 明朝", 12)
Private prContext As System.Drawing.Graphics
Private topMargin As Integer
Private leftMargin As Integer
' ///////////////////////////////////////////
' 実際の印刷をコントロールする一般的な変数
' ///////////////////////////////////////////
' ■ 最大行数( 1ページに印刷可能な行数 )
Private pMax As Integer
' ■ ページカウンタ
Private pCnt As Integer = 0
' ■ 行カウンタ( 次に印字する行 )
Private pRow As Integer = 1
' 外部データ
Private ReadFile As StreamReader
Private LineText As String
' ******************************************************
' 呼び出し
' ******************************************************
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
' 開始
PrintPreviewDialog1.ShowDialog()
End Sub
' ******************************************************
' プレビューダイアログの初期処理
' ( デザイナのイベントから作成します )
' ******************************************************
Private Sub PrintPreviewDialog1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles PrintPreviewDialog1.Load
' デスクトップ左上
PrintPreviewDialog1.DesktopLocation = New System.Drawing.Point(0, 0)
' ダイアログの大きさ
PrintPreviewDialog1.Width = 800
PrintPreviewDialog1.Height = 600
' 倍率
PrintPreviewDialog1.PrintPreviewControl.Zoom = 1
End Sub
' ******************************************************
' 初期処理
' ******************************************************
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
' 印刷処理の実体の作成
pd = New PrintDocument()
' プレビューのプロパティにセット
PrintPreviewDialog1.Document = pd
' 印刷イベントをプレビューから呼び出せるように登録する
AddHandler pd.PrintPage, AddressOf pd_PrintPage
End Sub
' ******************************************************
' 実際の印刷処理
' ******************************************************
Private Sub pd_PrintPage(ByVal sender As System.Object, _
ByVal e As System.Drawing.Printing.PrintPageEventArgs)
' 左側余白
leftMargin = e.MarginBounds.Left
' 上側余白
topMargin = e.MarginBounds.Top
' 現在のコンテキストを取得
prContext = e.Graphics
' 余白を除いた高さをフォントの高さで割る
pMax = e.MarginBounds.Height / pf.GetHeight(e.Graphics)
' 初回処理( ページ = 0 )
If pCnt = 0 Then
Try
' SHIFT_JIS
ReadFile = _
New StreamReader("C:\TMP\社員マスタ.csv", _
Encoding.GetEncoding(932))
Catch ' e As Exception
' ※ エラーの場合、空ページを出力してしまうので、
' ※ 呼び出す以前にチェックする必要がある
MessageBox.Show("入力エラーです")
' データ終了
e.HasMorePages = False
Return
End Try
End If
pCnt += 1
' 印字処理は、条件が多様になる為、単純ループで優先順位の高い
' 順序で処理を記述して行く( この場合、データが無い場合が最も優先順位が高い )
Do
' データがなくなった場合
' ※ データが無い場合は、空ページを印字してしまうので
' ※ 呼び出す以前にチェックする必要がある
If ReadFile.EndOfStream Then
ReadFile.Close()
ReadFile.Dispose()
' /////////////////////////////
' ■重要 : 次の呼び出しに備える
' /////////////////////////////
pCnt = 0
pRow = 1
' データ終了なので完全脱出
e.HasMorePages = False
Return
End If
' 読んだデータがに印字できないと考えられる場合は
' データを読み込まないようにする
If pRow > pMax Then
' /////////////////////////////
' ■重要 : 次のページに備える
' /////////////////////////////
pRow = 1
' ページ内にデータを印字できないので脱出
Exit Do
End If
' 次行読み込み
LineText = ReadFile.ReadLine()
' 印字
PrString(pRow, LineText)
' /////////////////////////////
' ■重要 : 1行印字したので、行ポインタを次に印字する位置に進める
' /////////////////////////////
pRow += 1
Loop
' 次のページがある
e.HasMorePages = True
End Sub
' ******************************************************
' 文字列を指定行の先頭から印字
' ******************************************************
Private Sub PrString(ByVal row As Integer, ByVal str As String)
Dim yPos As Integer = 0
' フォントの高さで一行の高さを決定
yPos = topMargin + (row - 1) * pf.GetHeight(prContext)
' 印字可能な左端から文字列をセットする
prContext.DrawString(str, pf, Brushes.Black, leftMargin, yPos)
End Sub
End Class
| |