VB.Net/GUI/Print Document

Материал из VB Эксперт
Перейти к: навигация, поиск

Assign Bmp file to a Print Document

 
Imports System
Imports System.Collections
Imports System.Data
Imports System.IO
Imports System.Xml.Serialization
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Text
Imports System.Drawing.Printing

Public Class MainClass
    Shared Sub Main()
       Dim form1 As Form = New Form1()
       Application.Run(form1) 
     End Sub
End Class


Public Class Form1
    Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
    Public Sub New()
        MyBase.New()
        "This call is required by the Windows Form Designer.
        InitializeComponent()
        "Add any initialization after the InitializeComponent() call
    End Sub
    "Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub
    Friend WithEvents PrintDocument1 As System.Drawing.Printing.PrintDocument
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
    "Required by the Windows Form Designer
    Private components As System.ruponentModel.Container
    "NOTE: The following procedure is required by the Windows Form Designer
    "It can be modified using the Windows Form Designer.  
    "Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
        Me.PrintDocument1 = New System.Drawing.Printing.PrintDocument()
        Me.Button1 = New System.Windows.Forms.Button()
        Me.PictureBox1 = New System.Windows.Forms.PictureBox()
        Me.SuspendLayout()
        "
        "PrintDocument1
        "
        "
        "Button1
        "
        Me.Button1.Location = New System.Drawing.Point(16, 216)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(264, 32)
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Print"
        "
        "PictureBox1
        "
        Me.PictureBox1.Image = New System.Drawing.Bitmap("figure2.bmp")
        Me.PictureBox1.Location = New System.Drawing.Point(16, 8)
        Me.PictureBox1.Name = "PictureBox1"
        Me.PictureBox1.Size = New System.Drawing.Size(264, 200)
        Me.PictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage
        Me.PictureBox1.TabIndex = 1
        Me.PictureBox1.TabStop = False
        "
        "Form1
        "
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 15)
        Me.ClientSize = New System.Drawing.Size(292, 268)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.PictureBox1, Me.Button1})
        Me.Name = "Form1"
        Me.Text = "Printing demo"
        Me.ResumeLayout(False)
    End Sub
#End Region
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    End Sub
    Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, _
         ByVal e As System.Drawing.Printing.PrintPageEventArgs) _
         Handles PrintDocument1.PrintPage
        Dim g As Graphics
        g = e.Graphics
        g.DrawImage(PictureBox1.Image, 0, 0)
        g.Dispose()
        e.HasMorePages = False
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        PrintDocument1.Print()
    End Sub
End Class


Print Test

  
Imports System.Drawing.Printing
Imports System.Windows.Forms
Imports System.Drawing
<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.cmdPreviewInForm = New System.Windows.Forms.Button
        Me.Preview = New System.Windows.Forms.PrintPreviewControl
        Me.cmdPreview = New System.Windows.Forms.Button
        Me.lblStatus = New System.Windows.Forms.Label
        Me.cmdPrint = New System.Windows.Forms.Button
        Me.GroupBox1 = New System.Windows.Forms.GroupBox
        Me.RadioButton1 = New System.Windows.Forms.RadioButton
        Me.RadioButton2 = New System.Windows.Forms.RadioButton
        Me.RadioButton3 = New System.Windows.Forms.RadioButton
        Me.RadioButton4 = New System.Windows.Forms.RadioButton
        Me.RadioButton5 = New System.Windows.Forms.RadioButton
        Me.GroupBox1.SuspendLayout()
        Me.SuspendLayout()
        "
        "cmdPreviewInForm
        "
        Me.cmdPreviewInForm.Location = New System.Drawing.Point(12, 76)
        Me.cmdPreviewInForm.Name = "cmdPreviewInForm"
        Me.cmdPreviewInForm.Size = New System.Drawing.Size(148, 24)
        Me.cmdPreviewInForm.TabIndex = 9
        Me.cmdPreviewInForm.Text = "Preview (In Form)"
        "
        "Preview
        "
        Me.Preview.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.Preview.AutoZoom = False
        Me.Preview.Location = New System.Drawing.Point(180, 12)
        Me.Preview.Name = "Preview"
        Me.Preview.Size = New System.Drawing.Size(339, 350)
        Me.Preview.TabIndex = 8
        "
        "cmdPreview
        "
        Me.cmdPreview.Location = New System.Drawing.Point(12, 44)
        Me.cmdPreview.Name = "cmdPreview"
        Me.cmdPreview.Size = New System.Drawing.Size(148, 24)
        Me.cmdPreview.TabIndex = 7
        Me.cmdPreview.Text = "Preview (Stand-alone)"
        "
        "lblStatus
        "
        Me.lblStatus.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
        Me.lblStatus.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
        Me.lblStatus.Location = New System.Drawing.Point(12, 235)
        Me.lblStatus.Name = "lblStatus"
        Me.lblStatus.Size = New System.Drawing.Size(148, 127)
        Me.lblStatus.TabIndex = 6
        "
        "cmdPrint
        "
        Me.cmdPrint.Location = New System.Drawing.Point(12, 12)
        Me.cmdPrint.Name = "cmdPrint"
        Me.cmdPrint.Size = New System.Drawing.Size(148, 24)
        Me.cmdPrint.TabIndex = 5
        Me.cmdPrint.Text = "Print"
        "
        "GroupBox1
        "
        Me.GroupBox1.Controls.Add(Me.RadioButton5)
        Me.GroupBox1.Controls.Add(Me.RadioButton4)
        Me.GroupBox1.Controls.Add(Me.RadioButton3)
        Me.GroupBox1.Controls.Add(Me.RadioButton2)
        Me.GroupBox1.Controls.Add(Me.RadioButton1)
        Me.GroupBox1.Location = New System.Drawing.Point(12, 108)
        Me.GroupBox1.Name = "GroupBox1"
        Me.GroupBox1.Size = New System.Drawing.Size(148, 111)
        Me.GroupBox1.TabIndex = 10
        Me.GroupBox1.TabStop = False
        Me.GroupBox1.Text = "Zoom"
        "
        "RadioButton1
        "
        Me.RadioButton1.AutoSize = True
        Me.RadioButton1.Location = New System.Drawing.Point(16, 17)
        Me.RadioButton1.Name = "RadioButton1"
        Me.RadioButton1.Size = New System.Drawing.Size(48, 17)
        Me.RadioButton1.TabIndex = 11
        Me.RadioButton1.TabStop = True
        Me.RadioButton1.Text = "25%"
        Me.RadioButton1.UseVisualStyleBackColor = True
        "
        "RadioButton2
        "
        Me.RadioButton2.AutoSize = True
        Me.RadioButton2.Location = New System.Drawing.Point(16, 34)
        Me.RadioButton2.Name = "RadioButton2"
        Me.RadioButton2.Size = New System.Drawing.Size(48, 17)
        Me.RadioButton2.TabIndex = 12
        Me.RadioButton2.TabStop = True
        Me.RadioButton2.Text = "50%"
        Me.RadioButton2.UseVisualStyleBackColor = True
        "
        "RadioButton3
        "
        Me.RadioButton3.AutoSize = True
        Me.RadioButton3.Location = New System.Drawing.Point(16, 68)
        Me.RadioButton3.Name = "RadioButton3"
        Me.RadioButton3.Size = New System.Drawing.Size(54, 17)
        Me.RadioButton3.TabIndex = 13
        Me.RadioButton3.TabStop = True
        Me.RadioButton3.Text = "100%"
        Me.RadioButton3.UseVisualStyleBackColor = True
        "
        "RadioButton4
        "
        Me.RadioButton4.AutoSize = True
        Me.RadioButton4.Location = New System.Drawing.Point(16, 85)
        Me.RadioButton4.Name = "RadioButton4"
        Me.RadioButton4.Size = New System.Drawing.Size(54, 17)
        Me.RadioButton4.TabIndex = 14
        Me.RadioButton4.TabStop = True
        Me.RadioButton4.Text = "200%"
        Me.RadioButton4.UseVisualStyleBackColor = True
        "
        "RadioButton5
        "
        Me.RadioButton5.AutoSize = True
        Me.RadioButton5.Location = New System.Drawing.Point(16, 51)
        Me.RadioButton5.Name = "RadioButton5"
        Me.RadioButton5.Size = New System.Drawing.Size(48, 17)
        Me.RadioButton5.TabIndex = 15
        Me.RadioButton5.TabStop = True
        Me.RadioButton5.Text = "75%"
        Me.RadioButton5.UseVisualStyleBackColor = True
        "
        "Form1
        "
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(531, 374)
        Me.Controls.Add(Me.GroupBox1)
        Me.Controls.Add(Me.cmdPreviewInForm)
        Me.Controls.Add(Me.Preview)
        Me.Controls.Add(Me.cmdPreview)
        Me.Controls.Add(Me.lblStatus)
        Me.Controls.Add(Me.cmdPrint)
        Me.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Text = "Print Test"
        Me.GroupBox1.ResumeLayout(False)
        Me.GroupBox1.PerformLayout()
        Me.ResumeLayout(False)
    End Sub
    Friend WithEvents cmdPreviewInForm As System.Windows.Forms.Button
    Friend WithEvents Preview As System.Windows.Forms.PrintPreviewControl
    Friend WithEvents cmdPreview As System.Windows.Forms.Button
    Friend WithEvents lblStatus As System.Windows.Forms.Label
    Friend WithEvents cmdPrint As System.Windows.Forms.Button
    Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
    Friend WithEvents RadioButton4 As System.Windows.Forms.RadioButton
    Friend WithEvents RadioButton3 As System.Windows.Forms.RadioButton
    Friend WithEvents RadioButton2 As System.Windows.Forms.RadioButton
    Friend WithEvents RadioButton1 As System.Windows.Forms.RadioButton
    Friend WithEvents RadioButton5 As System.Windows.Forms.RadioButton
    Private WithEvents MyDoc As New PrintDocument()
    Private PageNumber As Integer
    Private Offset As Integer
    Private PrintData(100) As String
    Private Sub PrintStatus_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim i As Integer
        For i = 0 To 100
            PrintData(i) = "This is line number " & i + 1 & ". "
        Next
    End Sub
    Private Sub cmdPrint_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles cmdPrint.Click
        lblStatus.Text = ""
        PageNumber = 0
        Offset = 0
        Dim dlgSettings As New PrintDialog()
        dlgSettings.Document = MyDoc
        Dim Result As DialogResult
        Result = dlgSettings.ShowDialog()
        If Result = DialogResult.OK Then MyDoc.Print()
    End Sub
    Private Sub MyDoc_PrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs) Handles MyDoc.PrintPage
        Dim MyFont As New Font("Arial", 10)
        Dim LineHeight As Single = MyFont.GetHeight(e.Graphics)
        Dim x As Single = e.MarginBounds.Left
        Dim y As Single = e.MarginBounds.Top
        PageNumber += 1
        lblStatus.Text &= vbNewLine & "Sending Page " & PageNumber & " ..."
        Do
            e.Graphics.DrawString(PrintData(Offset), MyFont, Brushes.Black, x, y)
            Offset += 1
            y += LineHeight
        Loop Until (y + LineHeight) > e.MarginBounds.Bottom Or Offset > PrintData.GetUpperBound(0)
        If Offset < PrintData.GetUpperBound(0) Then
            e.HasMorePages = True
        Else
            Offset = 0
            lblStatus.Text &= vbNewLine & "Print or Preview Completed"
        End If
    End Sub
    Private Sub cmdPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPreview.Click
        lblStatus.Text = ""
        Dim dlgPreview As New PrintPreviewDialog()
        dlgPreview.Document = MyDoc
        dlgPreview.Show()
    End Sub
    Private Sub cmdPreviewInForm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPreviewInForm.Click
        lblStatus.Text = ""
        Preview.Zoom = 0.2
        Preview.Columns = 2
        Preview.Rows = 3
        Preview.Document = MyDoc
        Preview.InvalidatePreview()
    End Sub
    Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged, RadioButton4.Click, RadioButton3.Click, RadioButton2.Click, RadioButton1.Click, RadioButton5.CheckedChanged
        Dim ctrl As Control = CType(sender, Control)
        Preview.Zoom = Val(ctrl.Text) / 100
    End Sub
End Class


Wrapped Printing

  
Imports System.Drawing.Printing
Imports System.Windows.Forms
Imports System.Drawing
<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Dim resources As System.ruponentModel.ruponentResourceManager = New System.ruponentModel.ruponentResourceManager(GetType(Form1))
        Me.cmdPrintWrapped = New System.Windows.Forms.Button
        Me.Label1 = New System.Windows.Forms.Label
        Me.cmdPrint = New System.Windows.Forms.Button
        Me.txtData = New System.Windows.Forms.TextBox
        Me.SuspendLayout()
        "
        Me.cmdPrintWrapped.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.cmdPrintWrapped.Location = New System.Drawing.Point(210, 174)
        Me.cmdPrintWrapped.Name = "cmdPrintWrapped"
        Me.cmdPrintWrapped.Size = New System.Drawing.Size(104, 24)
        Me.cmdPrintWrapped.TabIndex = 7
        Me.cmdPrintWrapped.Text = "Print Wrapped"
        "
        Me.Label1.Location = New System.Drawing.Point(14, 10)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(164, 16)
        Me.Label1.TabIndex = 6
        Me.Label1.Text = "Text To Print:"
        "
        "cmdPrint
        "
        Me.cmdPrint.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
        Me.cmdPrint.Location = New System.Drawing.Point(14, 174)
        Me.cmdPrint.Name = "cmdPrint"
        Me.cmdPrint.Size = New System.Drawing.Size(104, 24)
        Me.cmdPrint.TabIndex = 5
        Me.cmdPrint.Text = "Print Unwrapped"
        "
        "txtData
        "
        Me.txtData.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.txtData.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.txtData.Location = New System.Drawing.Point(14, 30)
        Me.txtData.Multiline = True
        Me.txtData.Name = "txtData"
        Me.txtData.ReadOnly = True
        Me.txtData.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
        Me.txtData.Size = New System.Drawing.Size(300, 132)
        Me.txtData.TabIndex = 4
        Me.txtData.Text = resources.GetString("txtData.Text")
        "
        "Form1
        "
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(329, 212)
        Me.Controls.Add(Me.cmdPrintWrapped)
        Me.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.cmdPrint)
        Me.Controls.Add(Me.txtData)
        Me.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Name = "Form1"
        Me.Text = "Wrapped Printing"
        Me.ResumeLayout(False)
        Me.PerformLayout()
    End Sub
    Friend WithEvents cmdPrintWrapped As System.Windows.Forms.Button
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents cmdPrint As System.Windows.Forms.Button
    Friend WithEvents txtData As System.Windows.Forms.TextBox
    Private Sub cmdPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPrint.Click
        Dim MyDoc As New PrintDocument()
        AddHandler MyDoc.PrintPage, AddressOf UnWrappedPrint
        Dim dlgSettings As New PrintDialog()
        dlgSettings.Document = MyDoc
        Dim Result As DialogResult
        Result = dlgSettings.ShowDialog()
        If Result = DialogResult.OK Then MyDoc.Print()
    End Sub

    Private Sub cmdPrintWrapped_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPrintWrapped.Click
        Dim MyDoc As New PrintDocument()
        AddHandler MyDoc.PrintPage, AddressOf WrappedPrint
        Dim dlgSettings As New PrintDialog()
        dlgSettings.Document = MyDoc
        Dim Result As DialogResult
        Result = dlgSettings.ShowDialog()
        If Result = DialogResult.OK Then MyDoc.Print()
    End Sub
    Private Sub UnWrappedPrint(ByVal sender As Object, ByVal e As PrintPageEventArgs)
        Dim MyFont As New Font("Verdana", 16)
        Dim x As Single = e.MarginBounds.Left
        Dim y As Single = e.MarginBounds.Top
        e.Graphics.DrawString(txtData.Text, MyFont, Brushes.Black, x, y)
    End Sub
    Private Sub WrappedPrint(ByVal sender As Object, ByVal e As PrintPageEventArgs)
        Dim MyFont As New Font("Verdana", 16)
        Dim x As Single = e.MarginBounds.Left
        Dim y As Single = e.MarginBounds.Top
        e.Graphics.DrawString(txtData.Text, MyFont, Brushes.Black, e.MarginBounds, StringFormat.GenericDefault)
    End Sub
    Private Sub WrappedPrint2(ByVal sender As Object, ByVal e As PrintPageEventArgs)
        Dim MyFont As New Font("Verdana", 16)
        Dim LineHeight As Single = MyFont.GetHeight(e.Graphics)
        Dim x As Single = e.MarginBounds.Left
        Dim y As Single = e.MarginBounds.Top
        Dim Line As String
        Dim ParsedLines As New ArrayList()
        Dim TextToPrint As String = txtData.Text
        Dim i As Integer
        Do
            Line &= TextToPrint.Chars(0)
            TextToPrint = TextToPrint.Substring(1)
            If Line.EndsWith(" ") Then
                If e.Graphics.MeasureString(Line, MyFont).Width > (e.PageBounds.Width - 300) Then
                    ParsedLines.Add(Line)
                    Line = ""
                End If
            End If
        Loop While TextToPrint <> ""
        ParsedLines.Add(Line)
        For Each Line In ParsedLines
            e.Graphics.DrawString(Line, MyFont, Brushes.Black, x, y)
            y += LineHeight
        Next
    End Sub
End Class