424,303 Members | 1,366 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,303 IT Pros & Developers. It's quick & easy.

Print Preview before printing a graphic file

P: n/a
I am trying to print a graphic file (tif) and also use the PrintPreview
control, the PageSetup control, and the Print dialog control. The code
attached is a concatination of two examples taken out of a Microsoft book,
"Visual Basic,Net Step by Step" in Chapter 18.

All but the bottom two subroutines will open a text file, and then allow me
to use the above controls, example 1. The bottom two subroutines will print
a graphic file, example 2. I have tried many things but have not been able
to modify either the first example to accept a graphic file, or add the
various controls to the bottom example.

I am wondering if the PrintPreview, PageSetup, and PrintDialog controls have
to be used differently if the files are graphic files. Perhaps there is a
trick passing object references.

Thanks for any suggestions here.

Hamil.

'******** start of code **********
Private PrintPageSettings As New PageSettings()
Private StringToPrint As String
Private PrintFont As New Font("Arial", 10)

Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnOpen.Click
Dim FilePath As String
'Display Open dialog box and select text file
OpenFileDialog1.Filter = "Text files (*.txt)|*.txt"
OpenFileDialog1.ShowDialog()
'If Cancel button not selected, load FilePath variable
If OpenFileDialog1.FileName <> "" Then
FilePath = OpenFileDialog1.FileName
Try
'Read text file and load into RichTextBox1
Dim MyFileStream As New FileStream(FilePath, FileMode.Open)
RichTextBox1.LoadFile(MyFileStream, _
RichTextBoxStreamType.PlainText)
MyFileStream.Close()
'Initialize string to print
StringToPrint = RichTextBox1.Text
'Enable Print button
btnPrint.Enabled = True
btnSetup.Enabled = True
btnPreview.Enabled = True
Catch ex As Exception
'display error messages if they appear
MessageBox.Show(ex.Message)
End Try
End If
End Sub

Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnPrint.Click
Try
'Specify current page settings
PrintDocument1.DefaultPageSettings = PrintPageSettings
'Specify document for print dialog box and show
StringToPrint = RichTextBox1.Text
PrintDialog1.Document = PrintDocument1
Dim result As DialogResult = PrintDialog1.ShowDialog()
'If click OK, print document to printer
If result = DialogResult.OK Then
PrintDocument1.Print()
End If

Catch ex As Exception
'Display error message
MessageBox.Show(ex.Message)
End Try
End Sub

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object,
ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles
PrintDocument1.PrintPage
Dim numChars As Integer
Dim numLines As Integer
Dim stringForPage As String
Dim strFormat As New StringFormat()
'Based on page setup, define drawable rectangle on page
Dim rectDraw As New RectangleF( _
e.MarginBounds.Left, e.MarginBounds.Top, _
e.MarginBounds.Width, e.MarginBounds.Height)
'Define area to determine how much text can fit on a page
'Make height one line shorter to ensure text doesn't clip
Dim sizeMeasure As New SizeF(e.MarginBounds.Width, _
e.MarginBounds.Height - PrintFont.GetHeight(e.Graphics))

'When drawing long strings, break between words
strFormat.Trimming = StringTrimming.Word
'Compute how many chars and lines can fit based on sizeMeasure
e.Graphics.MeasureString(StringToPrint, PrintFont, _
sizeMeasure, strFormat, numChars, numLines)
'Compute string that will fit on a page
stringForPage = StringToPrint.Substring(0, numChars)
'Print string on current page
e.Graphics.DrawString(stringForPage, PrintFont, _
Brushes.Black, rectDraw, strFormat)
'If there is more text, indicate there are more pages
If numChars < StringToPrint.Length Then
'Substract text from string that has been printed
StringToPrint = StringToPrint.Substring(numChars)
e.HasMorePages = True
Else
e.HasMorePages = False
'All text has been printed, so restore string
StringToPrint = RichTextBox1.Text
End If
End Sub

Private Sub btnSetup_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSetup.Click
Try
'Load page settings and display page setup dialog box
PageSetupDialog1.PageSettings = PrintPageSettings
PageSetupDialog1.ShowDialog()
Catch ex As Exception
'Display error message
MessageBox.Show(ex.Message)
End Try
End Sub

Private Sub btnPreview_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnPreview.Click
Try
'Specify current page settings
PrintDocument1.DefaultPageSettings = PrintPageSettings
'Specify document for print preview dialog box and show
StringToPrint = RichTextBox1.Text
PrintPreviewDialog1.Document = PrintDocument1
PrintPreviewDialog1.ShowDialog()
Catch ex As Exception
'Display error message
MessageBox.Show(ex.Message)
End Try
End Sub

'************************************************* *******************

'Sub for printing graphic
Private Sub PrintGraphic(ByVal sender As Object, _
ByVal ev As PrintPageEventArgs)
' Create the graphic using DrawImage
ev.Graphics.DrawImage(Image.FromFile(TextBox1.Text ), _
ev.Graphics.VisibleClipBounds)
' Specify that this is the last page to print
ev.HasMorePages = False
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

btnPrint.Enabled = True
btnSetup.Enabled = True
btnPreview.Enabled = True

Try
AddHandler PrintDocument2.PrintPage, AddressOf Me.PrintGraphic
PrintDocument2.Print() 'print graphic
Catch ex As Exception 'catch printing exception
MessageBox.Show("Sorry--there is a problem printing", _
ex.ToString())
End Try
End Sub
End Class
Private PrintPageSettings As New PageSettings()
Private StringToPrint As String
Private PrintFont As New Font("Arial", 10)

Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnOpen.Click
Dim FilePath As String
'Display Open dialog box and select text file
OpenFileDialog1.Filter = "Text files (*.txt)|*.txt"
OpenFileDialog1.ShowDialog()
'If Cancel button not selected, load FilePath variable
If OpenFileDialog1.FileName <> "" Then
FilePath = OpenFileDialog1.FileName
Try
'Read text file and load into RichTextBox1
Dim MyFileStream As New FileStream(FilePath, FileMode.Open)
RichTextBox1.LoadFile(MyFileStream, _
RichTextBoxStreamType.PlainText)
MyFileStream.Close()
'Initialize string to print
StringToPrint = RichTextBox1.Text
'Enable Print button
btnPrint.Enabled = True
btnSetup.Enabled = True
btnPreview.Enabled = True
Catch ex As Exception
'display error messages if they appear
MessageBox.Show(ex.Message)
End Try
End If
End Sub

Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnPrint.Click
Try
'Specify current page settings
PrintDocument1.DefaultPageSettings = PrintPageSettings
'Specify document for print dialog box and show
StringToPrint = RichTextBox1.Text
PrintDialog1.Document = PrintDocument1
Dim result As DialogResult = PrintDialog1.ShowDialog()
'If click OK, print document to printer
If result = DialogResult.OK Then
PrintDocument1.Print()
End If

Catch ex As Exception
'Display error message
MessageBox.Show(ex.Message)
End Try
End Sub

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object,
ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles
PrintDocument1.PrintPage
Dim numChars As Integer
Dim numLines As Integer
Dim stringForPage As String
Dim strFormat As New StringFormat()
'Based on page setup, define drawable rectangle on page
Dim rectDraw As New RectangleF( _
e.MarginBounds.Left, e.MarginBounds.Top, _
e.MarginBounds.Width, e.MarginBounds.Height)
'Define area to determine how much text can fit on a page
'Make height one line shorter to ensure text doesn't clip
Dim sizeMeasure As New SizeF(e.MarginBounds.Width, _
e.MarginBounds.Height - PrintFont.GetHeight(e.Graphics))

'When drawing long strings, break between words
strFormat.Trimming = StringTrimming.Word
'Compute how many chars and lines can fit based on sizeMeasure
e.Graphics.MeasureString(StringToPrint, PrintFont, _
sizeMeasure, strFormat, numChars, numLines)
'Compute string that will fit on a page
stringForPage = StringToPrint.Substring(0, numChars)
'Print string on current page
e.Graphics.DrawString(stringForPage, PrintFont, _
Brushes.Black, rectDraw, strFormat)
'If there is more text, indicate there are more pages
If numChars < StringToPrint.Length Then
'Substract text from string that has been printed
StringToPrint = StringToPrint.Substring(numChars)
e.HasMorePages = True
Else
e.HasMorePages = False
'All text has been printed, so restore string
StringToPrint = RichTextBox1.Text
End If
End Sub

Private Sub btnSetup_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSetup.Click
Try
'Load page settings and display page setup dialog box
PageSetupDialog1.PageSettings = PrintPageSettings
PageSetupDialog1.ShowDialog()
Catch ex As Exception
'Display error message
MessageBox.Show(ex.Message)
End Try
End Sub

Private Sub btnPreview_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnPreview.Click
Try
'Specify current page settings
PrintDocument1.DefaultPageSettings = PrintPageSettings
'Specify document for print preview dialog box and show
StringToPrint = RichTextBox1.Text
PrintPreviewDialog1.Document = PrintDocument1
PrintPreviewDialog1.ShowDialog()
Catch ex As Exception
'Display error message
MessageBox.Show(ex.Message)
End Try
End Sub

'************************************************* *******************

'Sub for printing graphic
Private Sub PrintGraphic(ByVal sender As Object, _
ByVal ev As PrintPageEventArgs)
' Create the graphic using DrawImage
ev.Graphics.DrawImage(Image.FromFile(TextBox1.Text ), _
ev.Graphics.VisibleClipBounds)
' Specify that this is the last page to print
ev.HasMorePages = False
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

btnPrint.Enabled = True
btnSetup.Enabled = True
btnPreview.Enabled = True

Try
AddHandler PrintDocument2.PrintPage, AddressOf Me.PrintGraphic
PrintDocument2.Print() 'print graphic
Catch ex As Exception 'catch printing exception
MessageBox.Show("Sorry--there is a problem printing", _
ex.ToString())
End Try
End Sub
End Class


Nov 23 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Sorry guys, I figured out the problem.

hamil.

"hamil" wrote:
I am trying to print a graphic file (tif) and also use the PrintPreview
control, the PageSetup control, and the Print dialog control. The code
attached is a concatination of two examples taken out of a Microsoft book,
"Visual Basic,Net Step by Step" in Chapter 18.

All but the bottom two subroutines will open a text file, and then allow me
to use the above controls, example 1. The bottom two subroutines will print
a graphic file, example 2. I have tried many things but have not been able
to modify either the first example to accept a graphic file, or add the
various controls to the bottom example.

I am wondering if the PrintPreview, PageSetup, and PrintDialog controls have
to be used differently if the files are graphic files. Perhaps there is a
trick passing object references.

Thanks for any suggestions here.

Hamil.

'******** start of code **********
Private PrintPageSettings As New PageSettings()
Private StringToPrint As String
Private PrintFont As New Font("Arial", 10)

Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnOpen.Click
Dim FilePath As String
'Display Open dialog box and select text file
OpenFileDialog1.Filter = "Text files (*.txt)|*.txt"
OpenFileDialog1.ShowDialog()
'If Cancel button not selected, load FilePath variable
If OpenFileDialog1.FileName <> "" Then
FilePath = OpenFileDialog1.FileName
Try
'Read text file and load into RichTextBox1
Dim MyFileStream As New FileStream(FilePath, FileMode.Open)
RichTextBox1.LoadFile(MyFileStream, _
RichTextBoxStreamType.PlainText)
MyFileStream.Close()
'Initialize string to print
StringToPrint = RichTextBox1.Text
'Enable Print button
btnPrint.Enabled = True
btnSetup.Enabled = True
btnPreview.Enabled = True
Catch ex As Exception
'display error messages if they appear
MessageBox.Show(ex.Message)
End Try
End If
End Sub

Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnPrint.Click
Try
'Specify current page settings
PrintDocument1.DefaultPageSettings = PrintPageSettings
'Specify document for print dialog box and show
StringToPrint = RichTextBox1.Text
PrintDialog1.Document = PrintDocument1
Dim result As DialogResult = PrintDialog1.ShowDialog()
'If click OK, print document to printer
If result = DialogResult.OK Then
PrintDocument1.Print()
End If

Catch ex As Exception
'Display error message
MessageBox.Show(ex.Message)
End Try
End Sub

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object,
ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles
PrintDocument1.PrintPage
Dim numChars As Integer
Dim numLines As Integer
Dim stringForPage As String
Dim strFormat As New StringFormat()
'Based on page setup, define drawable rectangle on page
Dim rectDraw As New RectangleF( _
e.MarginBounds.Left, e.MarginBounds.Top, _
e.MarginBounds.Width, e.MarginBounds.Height)
'Define area to determine how much text can fit on a page
'Make height one line shorter to ensure text doesn't clip
Dim sizeMeasure As New SizeF(e.MarginBounds.Width, _
e.MarginBounds.Height - PrintFont.GetHeight(e.Graphics))

'When drawing long strings, break between words
strFormat.Trimming = StringTrimming.Word
'Compute how many chars and lines can fit based on sizeMeasure
e.Graphics.MeasureString(StringToPrint, PrintFont, _
sizeMeasure, strFormat, numChars, numLines)
'Compute string that will fit on a page
stringForPage = StringToPrint.Substring(0, numChars)
'Print string on current page
e.Graphics.DrawString(stringForPage, PrintFont, _
Brushes.Black, rectDraw, strFormat)
'If there is more text, indicate there are more pages
If numChars < StringToPrint.Length Then
'Substract text from string that has been printed
StringToPrint = StringToPrint.Substring(numChars)
e.HasMorePages = True
Else
e.HasMorePages = False
'All text has been printed, so restore string
StringToPrint = RichTextBox1.Text
End If
End Sub

Private Sub btnSetup_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSetup.Click
Try
'Load page settings and display page setup dialog box
PageSetupDialog1.PageSettings = PrintPageSettings
PageSetupDialog1.ShowDialog()
Catch ex As Exception
'Display error message
MessageBox.Show(ex.Message)
End Try
End Sub

Private Sub btnPreview_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnPreview.Click
Try
'Specify current page settings
PrintDocument1.DefaultPageSettings = PrintPageSettings
'Specify document for print preview dialog box and show
StringToPrint = RichTextBox1.Text
PrintPreviewDialog1.Document = PrintDocument1
PrintPreviewDialog1.ShowDialog()
Catch ex As Exception
'Display error message
MessageBox.Show(ex.Message)
End Try
End Sub

'************************************************* *******************

'Sub for printing graphic
Private Sub PrintGraphic(ByVal sender As Object, _
ByVal ev As PrintPageEventArgs)
' Create the graphic using DrawImage
ev.Graphics.DrawImage(Image.FromFile(TextBox1.Text ), _
ev.Graphics.VisibleClipBounds)
' Specify that this is the last page to print
ev.HasMorePages = False
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

btnPrint.Enabled = True
btnSetup.Enabled = True
btnPreview.Enabled = True

Try
AddHandler PrintDocument2.PrintPage, AddressOf Me.PrintGraphic
PrintDocument2.Print() 'print graphic
Catch ex As Exception 'catch printing exception
MessageBox.Show("Sorry--there is a problem printing", _
ex.ToString())
End Try
End Sub
End Class
Private PrintPageSettings As New PageSettings()
Private StringToPrint As String
Private PrintFont As New Font("Arial", 10)

Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnOpen.Click
Dim FilePath As String
'Display Open dialog box and select text file
OpenFileDialog1.Filter = "Text files (*.txt)|*.txt"
OpenFileDialog1.ShowDialog()
'If Cancel button not selected, load FilePath variable
If OpenFileDialog1.FileName <> "" Then
FilePath = OpenFileDialog1.FileName
Try
'Read text file and load into RichTextBox1
Dim MyFileStream As New FileStream(FilePath, FileMode.Open)
RichTextBox1.LoadFile(MyFileStream, _
RichTextBoxStreamType.PlainText)
MyFileStream.Close()
'Initialize string to print
StringToPrint = RichTextBox1.Text
'Enable Print button
btnPrint.Enabled = True
btnSetup.Enabled = True
btnPreview.Enabled = True
Catch ex As Exception
'display error messages if they appear
MessageBox.Show(ex.Message)
End Try
End If
End Sub

Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnPrint.Click
Try
'Specify current page settings
PrintDocument1.DefaultPageSettings = PrintPageSettings
'Specify document for print dialog box and show
StringToPrint = RichTextBox1.Text
PrintDialog1.Document = PrintDocument1
Dim result As DialogResult = PrintDialog1.ShowDialog()
'If click OK, print document to printer
If result = DialogResult.OK Then
PrintDocument1.Print()
End If

Catch ex As Exception
'Display error message
MessageBox.Show(ex.Message)
End Try
End Sub

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object,
ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles
PrintDocument1.PrintPage
Dim numChars As Integer
Dim numLines As Integer
Dim stringForPage As String
Dim strFormat As New StringFormat()
'Based on page setup, define drawable rectangle on page
Dim rectDraw As New RectangleF( _
e.MarginBounds.Left, e.MarginBounds.Top, _
e.MarginBounds.Width, e.MarginBounds.Height)
'Define area to determine how much text can fit on a page
'Make height one line shorter to ensure text doesn't clip
Dim sizeMeasure As New SizeF(e.MarginBounds.Width, _
e.MarginBounds.Height - PrintFont.GetHeight(e.Graphics))

'When drawing long strings, break between words
strFormat.Trimming = StringTrimming.Word
'Compute how many chars and lines can fit based on sizeMeasure
e.Graphics.MeasureString(StringToPrint, PrintFont, _
sizeMeasure, strFormat, numChars, numLines)
'Compute string that will fit on a page
stringForPage = StringToPrint.Substring(0, numChars)
'Print string on current page
e.Graphics.DrawString(stringForPage, PrintFont, _
Brushes.Black, rectDraw, strFormat)
'If there is more text, indicate there are more pages
If numChars < StringToPrint.Length Then
'Substract text from string that has been printed
StringToPrint = StringToPrint.Substring(numChars)
e.HasMorePages = True
Else
e.HasMorePages = False
'All text has been printed, so restore string
StringToPrint = RichTextBox1.Text
End If
End Sub

Private Sub btnSetup_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSetup.Click
Try
'Load page settings and display page setup dialog box
PageSetupDialog1.PageSettings = PrintPageSettings
PageSetupDialog1.ShowDialog()
Catch ex As Exception
'Display error message
MessageBox.Show(ex.Message)
End Try
End Sub

Private Sub btnPreview_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnPreview.Click
Try
'Specify current page settings
PrintDocument1.DefaultPageSettings = PrintPageSettings
'Specify document for print preview dialog box and show
StringToPrint = RichTextBox1.Text
PrintPreviewDialog1.Document = PrintDocument1
PrintPreviewDialog1.ShowDialog()
Catch ex As Exception
'Display error message
MessageBox.Show(ex.Message)
End Try
End Sub

'************************************************* *******************

'Sub for printing graphic
Private Sub PrintGraphic(ByVal sender As Object, _
ByVal ev As PrintPageEventArgs)
' Create the graphic using DrawImage
ev.Graphics.DrawImage(Image.FromFile(TextBox1.Text ), _
ev.Graphics.VisibleClipBounds)
' Specify that this is the last page to print
ev.HasMorePages = False
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

btnPrint.Enabled = True
btnSetup.Enabled = True
btnPreview.Enabled = True

Nov 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.