VB.NET 2008/VB - 인쇄

인쇄 - PrintDocument 를 사용한 인쇄 지원

본클라쓰 2012. 1. 3. 10:43

 

 

 

다중 페이지 텍스트 파일 인쇄

 

Windows 기반 응용 프로그램에서 텍스트를 인쇄하는 것은 배우 일반적인 것이다. Graphics 클래스에서는 개체를 화면이나 프린터 같은 장치에 그릴 수 있는 방법을 제공한다.

 

Private printDocument1 As New PrintDocument()

Private stringToPrint As String

 

Dim docName As String = "testPage.txt"
Dim docPath As String = "c:\"
printDocument1.DocumentName = docName


Dim stream As New FileStream(docPath + docName, FileMode.Open)


Try
    Dim reader As New StreamReader(stream)
    Try
        stringToPrint = reader.ReadToEnd()
    Finally
        reader.Dispose()
    End Try
Finally
    stream.Dispose()
End Try

 

PrintPage 이벤트 처리기에서 PrintPageEventArgs 클래스의 Gaphics 속성과 문서 내용을 사용하여 줄 길이와 페이지 당 줄 수를 계산한다. 각 페이지가 그려진 후 마지막 페이지인지 그에 따라 PrintPageEventArgs의 HasMorePages 속성을 설정한다. HasMorepages가 false가 될 때까지 PrintPage 이벤트가 발생한다. 또한 PrintPage 이벤트가 이벤트 처리 메서드에 연결되어 있는지 확인한다.

 

Private Sub printDocument1_PrintPage(ByVal sender As ObjectByVal e As PrintPageEventArgs)

    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

 

이제 Print 메서드를 호출하여 Printpage 이벤트를 발생시킨다.

 

printDocument1.Print()

 

 

 

 

 

Windows Forms 인쇄

 

개발 과정의 일부로 Windows Form의 복사본을 인쇄해야 하는 경우가 많다. 다음 코드 예제는 CopyFromScreen 메서드를 사용하여 현재 폼의 복사본을 인쇄하는 방법을 보여준다.

 

Imports System Imports System.Windows.Forms
Imports System.Drawing
Imports System.Drawing.Printing

Public Class Form1
    Inherits Form
    Private WithEvents printButton As New Button
    Private WithEvents printDocument1 As New PrintDocument

    Public Sub New()
        printButton.Text = "Print Form"
        Me.Controls.Add(printButton)

    End Sub

    Dim memoryImage As Bitmap

    Private Sub CaptureScreen()
        Dim myGraphics As Graphics = Me.CreateGraphics()
        Dim s As Size = Me.Size
        memoryImage = New Bitmap(s.Width, s.Height, myGraphics)
        Dim memoryGraphics As Graphics = Graphics.FromImage(memoryImage)
        memoryGraphics.CopyFromScreen(Me.Location.X, Me.Location.Y, 0, 0, s)
    End Sub

    Private Sub printDocument1_PrintPage(ByVal sender As System.Object, _
       ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles _
       printDocument1.PrintPage
        e.Graphics.DrawImage(memoryImage, 0, 0)
    End Sub

    Private Sub printButton_Click(ByVal sender As System.Object, ByVal e As _
       System.EventArgs) Handles printButton.Click
        CaptureScreen()
        printDocument1.Print()
    End Sub

    Public Shared Sub Main()
        Application.Run(New Form1())
    End Sub
End Class