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 1 5672
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Rob McLennan - ZETLAND |
last post by:
Hi,
I have set up an external stylesheet, named "print.css", to format the
style of all pages printed from my company's website. I've been
previewing my changes to the stylesheet by doing...
|
by: Neo Geshel |
last post by:
I'm helping on a web site, and it's got our knickers in a knot. We're
using the same basic CSS files (with mods) and same headers as from our
other sites, but the "print preview" won't work...
|
by: Pkant |
last post by:
I want to Print a txt file with this code :
class Printing
{
PrintDocument prndoc = new PrintDocument();
PrintPreviewDialog ppd = new PrintPreviewDialog();
|
by: Chase |
last post by:
The print preview dialog will display the preview of the document but will
print a blank page when the print icon is clicked. Printing is fine
otherwise. Can i set the print icon in the...
|
by: CSharpguy |
last post by:
In my gridview I have 2 -3 template fields which are hyperlinks. I allow the
user to print this grid. When the grid prints it also prints the links, how
can I take the user to a print preview page...
|
by: Brad Pears |
last post by:
I have some sample code that uses the print dialog, print preview and a
print direct options.
If I select print preview and then click the printer icon from that, the
document prints. If I...
|
by: Brad Pears |
last post by:
I have a vb.net 2005 application and am using the print preview screen. This screen has a printer icon on it that the user can use to print the document currently being viewed. It uses the default...
|
by: mailjaneen |
last post by:
Hello, can someone help me.
I want to display some fields on a report in print preview (i.e.
instructions), that I don't want to print to the printer or export to
file. I can't seem to find any...
|
by: raylopez99 |
last post by:
I am running out of printing paper trying to debug this...it has to be
trivial, but I cannot figure it out--can you? Why am I not printing
text, but just the initial string "howdy"?
On the...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |