Imports System.IO
Imports System.Text.Encoding
Imports System.Drawing
Imports System.Drawing.Printing
Imports System.Windows.Forms
public class PrintTextFile
public Shared Sub Main
Application.Run(New Form1)
End Sub
End class
Public Class Form1
Inherits System.Windows.Forms.Form
Public Sub New()
MyBase.New()
InitializeComponent()
End Sub
Private components As System.ruponentModel.IContainer
Friend WithEvents PrintDialog1 As System.Windows.Forms.PrintDialog
Friend WithEvents PrintPreviewDialog1 As System.Windows.Forms.PrintPreviewDialog
Friend WithEvents PageSetupDialog1 As System.Windows.Forms.PageSetupDialog
Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem
Friend WithEvents MenuItem5 As System.Windows.Forms.MenuItem
Friend WithEvents mnuPreview As System.Windows.Forms.MenuItem
Friend WithEvents mnuPrint As System.Windows.Forms.MenuItem
Friend WithEvents mnuExit As System.Windows.Forms.MenuItem
Friend WithEvents MenuItem3 As System.Windows.Forms.MenuItem
Friend WithEvents Panel1 As System.Windows.Forms.Panel
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
Friend WithEvents mnuOpen As System.Windows.Forms.MenuItem
Friend WithEvents mnuPageSetup As System.Windows.Forms.MenuItem
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.PrintDialog1 = New System.Windows.Forms.PrintDialog()
Me.PrintPreviewDialog1 = New System.Windows.Forms.PrintPreviewDialog()
Me.PageSetupDialog1 = New System.Windows.Forms.PageSetupDialog()
Me.MainMenu1 = New System.Windows.Forms.MainMenu()
Me.MenuItem1 = New System.Windows.Forms.MenuItem()
Me.mnuOpen = New System.Windows.Forms.MenuItem()
Me.MenuItem3 = New System.Windows.Forms.MenuItem()
Me.mnuPageSetup = New System.Windows.Forms.MenuItem()
Me.mnuPreview = New System.Windows.Forms.MenuItem()
Me.mnuPrint = New System.Windows.Forms.MenuItem()
Me.MenuItem5 = New System.Windows.Forms.MenuItem()
Me.mnuExit = New System.Windows.Forms.MenuItem()
Me.Panel1 = New System.Windows.Forms.Panel()
Me.TextBox1 = New System.Windows.Forms.TextBox()
Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog()
Me.Panel1.SuspendLayout()
Me.SuspendLayout()
"
"PrintPreviewDialog1
"
Me.PrintPreviewDialog1.AutoScrollMargin = New System.Drawing.Size(0, 0)
Me.PrintPreviewDialog1.AutoScrollMinSize = New System.Drawing.Size(0, 0)
Me.PrintPreviewDialog1.ClientSize = New System.Drawing.Size(400, 300)
Me.PrintPreviewDialog1.Enabled = True
Me.PrintPreviewDialog1.Location = New System.Drawing.Point(375, 17)
Me.PrintPreviewDialog1.MaximumSize = New System.Drawing.Size(0, 0)
Me.PrintPreviewDialog1.Name = "PrintPreviewDialog1"
Me.PrintPreviewDialog1.Opacity = 1
Me.PrintPreviewDialog1.TransparencyKey = System.Drawing.Color.Empty
Me.PrintPreviewDialog1.Visible = False
"
"MainMenu1
"
Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1})
"
"MenuItem1
"
Me.MenuItem1.Index = 0
Me.MenuItem1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuOpen, Me.MenuItem3, Me.mnuPageSetup, Me.mnuPreview, Me.mnuPrint, Me.MenuItem5, Me.mnuExit})
Me.MenuItem1.Text = "&File"
"
"mnuOpen
"
Me.mnuOpen.Index = 0
Me.mnuOpen.Text = "Open"
"
"MenuItem3
"
Me.MenuItem3.Index = 1
Me.MenuItem3.Text = "-"
"
"mnuPageSetup
"
Me.mnuPageSetup.Index = 2
Me.mnuPageSetup.Text = "Page Setup"
"
"mnuPreview
"
Me.mnuPreview.Index = 3
Me.mnuPreview.Text = "Print Preview"
"
"mnuPrint
"
Me.mnuPrint.Index = 4
Me.mnuPrint.Text = "Print"
"
"MenuItem5
"
Me.MenuItem5.Index = 5
Me.MenuItem5.Text = "-"
"
"mnuExit
"
Me.mnuExit.Index = 6
Me.mnuExit.Text = "Exit"
"
"Panel1
"
Me.Panel1.Controls.AddRange(New System.Windows.Forms.Control() {Me.TextBox1})
Me.Panel1.Dock = System.Windows.Forms.DockStyle.Fill
Me.Panel1.DockPadding.All = 5
Me.Panel1.Name = "Panel1"
Me.Panel1.Size = New System.Drawing.Size(563, 325)
Me.Panel1.TabIndex = 0
"
"TextBox1
"
Me.TextBox1.Dock = System.Windows.Forms.DockStyle.Fill
Me.TextBox1.Location = New System.Drawing.Point(6, 5)
Me.TextBox1.Multiline = True
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(551, 315)
Me.TextBox1.TabIndex = 0
Me.TextBox1.Text = ""
"
"Form1
"
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(563, 325)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Panel1})
Me.Menu = Me.MainMenu1
Me.Name = "Form1"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "Dialog Application"
Me.Panel1.ResumeLayout(False)
Me.ResumeLayout(False)
End Sub
Private printerSettings As System.Drawing.Printing.PageSettings
Private printingFont As Font
Private streamReader As StreamReader
Private filename As String
Private Sub mnuPageSetup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuPageSetup.Click
With PageSetupDialog1
Try
If (printerSettings Is Nothing) Then
printerSettings = New System.Drawing.Printing.PageSettings()
End If
.AllowMargins = True
.AllowOrientation = True
.AllowPaper = True
.AllowPrinter = True
.PageSettings = printerSettings
.ShowDialog()
Catch ex As Exception
MessageBox.Show(ex.StackTrace.ToString(), "Print", _
MessageBoxButtons.OK, MessageBoxIcon.Error, _
MessageBoxDefaultButton.Button1)
End Try
End With
End Sub
Private Sub mnuPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuPreview.Click
If Not filename Is Nothing Then
Try
streamReader = New StreamReader(filename)
Try
printingFont = New Font("Arial", 10)
Dim pd As New PrintDocument()
AddHandler pd.PrintPage, AddressOf Me.pd_PrintPage
If Not (printerSettings Is Nothing) Then
pd.DefaultPageSettings = printerSettings
End If
With PrintPreviewDialog1
.Document = pd
.ShowDialog()
End With
Finally
streamReader.Close()
End Try
Catch ex As Exception
MessageBox.Show(ex.Message, "Print", _
MessageBoxButtons.OK, MessageBoxIcon.Error, _
MessageBoxDefaultButton.Button1)
End Try
Else
MessageBox.Show("Please select file first.", "Print", _
MessageBoxButtons.OK, MessageBoxIcon.Error, _
MessageBoxDefaultButton.Button1)
End If
End Sub
Private Sub mnuPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuPrint.Click
If Not filename Is Nothing Then
Try
streamReader = New StreamReader(filename)
Try
printingFont = New Font("Arial", 10)
Dim pd As New PrintDocument()
AddHandler pd.PrintPage, AddressOf Me.pd_PrintPage
If Not (printerSettings Is Nothing) Then
pd.DefaultPageSettings = printerSettings
End If
With PrintDialog1
.Document = pd
.AllowPrintToFile = True
.AllowSelection = True
.AllowSomePages = True
If .ShowDialog() = DialogResult.OK Then
pd.Print()
End If
End With
Finally
streamReader.Close()
End Try
Catch ex As Exception
MessageBox.Show(ex.StackTrace.ToString, "Print", _
MessageBoxButtons.OK, MessageBoxIcon.Error, _
MessageBoxDefaultButton.Button1)
End Try
Else
MessageBox.Show("Please select file first.", "Print", _
MessageBoxButtons.OK, MessageBoxIcon.Error, _
MessageBoxDefaultButton.Button1)
End If
End Sub
Private Sub pd_PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
Dim linesPerPage As Single = 0
Dim yPos As Single = 0
Dim count As Integer = 0
Dim leftMargin As Single = ev.MarginBounds.Left
Dim topMargin As Single = ev.MarginBounds.Top
Dim line As String = Nothing
linesPerPage = ev.MarginBounds.Height / printingFont.GetHeight(ev.Graphics)
While count < linesPerPage
line = streamReader.ReadLine()
If line Is Nothing Then
Exit While
End If
yPos = topMargin + count * printingFont.GetHeight(ev.Graphics)
ev.Graphics.DrawString(line, printingFont, Brushes.Black, leftMargin, yPos, New StringFormat())
count += 1
End While
If Not (line Is Nothing) Then
ev.HasMorePages = True
Else
ev.HasMorePages = False
End If
End Sub
Private Sub mnuOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuOpen.Click
Dim sr As StreamReader = Nothing
Dim fs As FileStream = Nothing
With OpenFileDialog1
.InitialDirectory = "C:\"
.Filter = "Text files (*.txt)|*.txt"
.FilterIndex = 2
.RestoreDirectory = True
.ShowHelp = True
.Title = "Open File Dialog"
If .ShowDialog() = DialogResult.OK Then
Me.Text = .FileName
filename = .FileName
TextBox1.Text = ""
Try
fs = New FileStream(.FileName, FileMode.Open, FileAccess.Read)
sr = New StreamReader(fs, System.Text.Encoding.Default)
TextBox1.Text = sr.ReadToEnd()
Catch ex As Exception
MessageBox.Show(ex.StackTrace.ToString(), "Print", _
MessageBoxButtons.OK, MessageBoxIcon.Error, _
MessageBoxDefaultButton.Button1)
Finally
If Not (sr Is Nothing) Then
sr.Close()
End If
If Not (fs Is Nothing) Then
fs.Close()
End If
End Try
End If
End With
End Sub
End Class