11 Replies - 21360 Views - Last Post: 16 December 2006 - 06:02 PM Rate Topic: -----

#1 bingy  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 91
  • View blog
  • Posts: 690
  • Joined: 15-December 06

Print Preview and Printing Problem *fixed*

Posted 15 December 2006 - 02:36 PM

Every time I try a print preview, it only shows 1 page and only 1 line. Then when i print it will not take the settings set forth in the page settings for the margins. Any help with this would be appreciated. This isn't for a grade in my VB class just going above and beyond what is neccessary for the project. I like to make fully functional programs and this problem is really killing me.


Imports System.IO
Imports System.Drawing.Printing

Main body of code

Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
		'Set the document to print
		Dim strTextToPrint As String
		strTextToPrint = rtxtFile.Text
		e.Graphics.DrawString(strTextToPrint, New Font("Arial Black", 12, FontStyle.Regular), Brushes.Blue, 50, 50)
	End Sub

	Private Sub mnupreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnupreview.Click
		PrintPreviewDialog1.ShowDialog()
	End Sub

	Private Sub mnuPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuPrint.Click
		If PrintDialog1.ShowDialog() = DialogResult.OK Then
			PrintDocument1.Print()
		End If
	End Sub

	Private Sub mnusetup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnusetup.Click
		PageSetupDialog1.ShowDialog()
	End Sub

This post has been edited by bingy: 18 December 2006 - 12:35 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Print Preview and Printing Problem *fixed*

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Print Preview and Printing Problem *fixed*

Posted 15 December 2006 - 02:56 PM

It is only printing one line because that is all that you have told it to print.

Where are you getting the information from that you need to print, ie. text file, listbox, etc?

Basically you will need to loop through the information that you want to print and output that information using the e.Graphics.DrawString method that you are currently using.

As you are looping through and adding each line to the DrawString don't forget to increment the vertical coordinates so that each line moves down. Otherwise you will print everything on one line on the page.

I would suggest creating a integer variable to increment the vertical spacing for inside the loop.

Here is an example that I did in my first VB.NET class, that should give you a pretty good idea of what I'm talking about.
		For row = 0 To itemCount - 1
			'retrieve index number of item selected
			'retrieve the name of the item
			listItemName = Convert.ToString(Me.uiItemsOrderedListBox.Items(row))
			'determine the index number of the item on the menu
			indexNumber = Me.uiMenuListBox.Items.IndexOf(listItemName)
			'retrieve the price
			itemPrice = Double.Parse(menuItemAndPrices(indexNumber, 1))

			e.Graphics.DrawString(listItemName.PadRight(25) & itemPrice.ToString("C2").PadLeft(8), _
				reportFont, Brushes.Black, horizontal, vertical)
			'increase vertical coordinates
			vertical = vertical + 15
		Next row



In the previous example I was taking the data from a listbox and printing it one line at a time. Notice the vertical variable that I'm incrementing to move the print postition down for each line.

Hope that helps.
Was This Post Helpful? 0
  • +
  • -

#3 bingy  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 91
  • View blog
  • Posts: 690
  • Joined: 15-December 06

Re: Print Preview and Printing Problem *fixed*

Posted 15 December 2006 - 02:59 PM

I am taking all the text from a rich text box and printing it to a printer. Basically making a form of Wordpad. I also can't seem to get the document to wordwrap.

This post has been edited by bingy: 15 December 2006 - 03:02 PM

Was This Post Helpful? 0
  • +
  • -

#4 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Print Preview and Printing Problem *fixed*

Posted 15 December 2006 - 03:15 PM

Ok, well then my previous post wont help you since you don't need to loop through the information.

What are the page settings for the margins that you are trying to use?

When you say it only print previews one line and one page, do you mean that information is missing from the preview that is in the richtextbox?
Was This Post Helpful? 0
  • +
  • -

#5 bingy  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 91
  • View blog
  • Posts: 690
  • Joined: 15-December 06

Re: Print Preview and Printing Problem *fixed*

Posted 15 December 2006 - 07:16 PM

View Postjayman9, on 15 Dec, 2006 - 03:15 PM, said:

Ok, well then my previous post wont help you since you don't need to loop through the information.

What are the page settings for the margins that you are trying to use?

When you say it only print previews one line and one page, do you mean that information is missing from the preview that is in the richtextbox?

the page settings are all 1" margins and page orientation is set to portrait. Yes information is missing from the preview that is in the richtextbox.
Was This Post Helpful? 0
  • +
  • -

#6 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Print Preview and Printing Problem *fixed*

Posted 15 December 2006 - 10:58 PM

I think this little tutorial will shed some light on the problem. You need to manually handle line printing when printing from a richtextbox. You have to determine the printable area and then use stringformat and measurestring to make the text fit into the printable area.

Keep in mind a richtextbox is just one long continuous line of text until you hit the enter key on your keyboard. So its possible you can enter more text than can fit on one print line.

http://www.startvbdo...intdialog1.aspx
Was This Post Helpful? 0
  • +
  • -

#7 bingy  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 91
  • View blog
  • Posts: 690
  • Joined: 15-December 06

Re: Print Preview and Printing Problem *fixed*

Posted 16 December 2006 - 09:57 AM

ok after following the tutorial, I have gotten it to read that there is multiple pages but whenever you click on print whether it be from the print preview dialog or from the menu it just spits out blank pages. I have submitted the code for my entire printing routine.

Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
		'PrintPage is the foundational printing event. This event gets fired for every 
		' page that will be printed
		Static intCurrentChar As Int32
		' declaring a static variable to hold the position of the last printed char
		Dim font As New Font("Verdana", 12)
		' initializing the font to be used for printing
		Dim PrintAreaHeight, PrintAreaWidth, marginLeft, marginTop As Int32
		With PrintDocument1.DefaultPageSettings
			' initializing local variables that contain the bounds of the printing area rectangle
			PrintAreaHeight = .PaperSize.Height - .Margins.Top - .Margins.Bottom
			PrintAreaWidth = .PaperSize.Width - .Margins.Left - .Margins.Right
			' initializing local variables to hold margin values that will serve
			' as the X and Y coordinates for the upper left corner of the printing 
			' area rectangle.
			marginLeft = .Margins.Left
			marginTop = .Margins.Top
			' X and Y coordinate
		End With

		If PrintDocument1.DefaultPageSettings.Landscape Then
			Dim intTemp As Int32
			intTemp = PrintAreaHeight
			PrintAreaHeight = PrintAreaWidth
			PrintAreaWidth = intTemp
			' if the user selects landscape mode, swap the printing area height and width
		End If

		Dim intLineCount As Int32 = CInt(PrintAreaHeight / font.Height)
		' calculating the total number of lines in the document based on the height of
		' the printing area and the height of the font
		Dim rectPrintingArea As New RectangleF(marginLeft, marginTop, PrintAreaWidth, PrintAreaHeight)
		' initializing the rectangle structure that defines the printing area
		Dim fmt As New StringFormat(StringFormatFlags.LineLimit)
		'instantiating the StringFormat class, which encapsulates text layout information
		Dim intLinesFilled, intCharsFitted As Int32
		e.Graphics.MeasureString(Mid(rtxtFile.Text, intCurrentChar + 1), font, New SizeF(PrintAreaWidth, PrintAreaHeight), fmt, intCharsFitted, intLinesFilled)
		' calling MeasureString to determine the number of characters that will fit in
		' the printing area rectangle
		e.Graphics.DrawString(Mid(rtxtFile.Text, intCurrentChar + 1), font, Brushes.Black, rectPrintingArea, fmt)
		' print the text to the page
		intCurrentChar += intCharsFitted
		'advancing the current char to the last char printed on this page
		Select Case rtxtFile.Text.Length
			Case Is > intCurrentChar
				'HasMorePages tells the printing module whether another PrintPage event should be fired
				e.HasMorePages = True
			Case Is < intCurrentChar
				e.HasMorePages = False
				intCurrentChar = 0
		End Select
	End Sub

	Private Sub mnupreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnupreview.Click
		Try
			PrintPreviewDialog1.ShowDialog()
		Catch es As Exception
			MessageBox.Show(es.Message)
		End Try
	End Sub

	Private Sub mnuPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuPrint.Click
		If PrintDialog1.ShowDialog = DialogResult.OK Then
			'showDialog method makes the dialog box visible at run time
			PrintDocument1.Print()
		End If
	End Sub

	Private Sub mnusetup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnusetup.Click
		With PageSetupDialog1
			.PageSettings = PrintDocument1.DefaultPageSettings
		End With
		Try
			If PageSetupDialog1.ShowDialog = DialogResult.OK Then
				PrintDocument1.DefaultPageSettings = PageSetupDialog1.PageSettings
			End If
		Catch es As Exception
			MessageBox.Show(es.Message)
		End Try
	End Sub

This post has been edited by bingy: 16 December 2006 - 02:05 PM

Was This Post Helpful? 0
  • +
  • -

#8 bingy  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 91
  • View blog
  • Posts: 690
  • Joined: 15-December 06

Re: Print Preview and Printing Problem *fixed*

Posted 16 December 2006 - 03:02 PM

[After some further review of the program and stesting i have figured out that it is not taking the page settings set forth as default and also as soon as you use printdocument1 once does not use it again. when you click on the menu option for print preview the second time the preview comes up blank. If i click on the print button before looking at the print preview i can get it to print but with 2" top and left margins where default page settings should be at 1".
Was This Post Helpful? 0
  • +
  • -

#9 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Print Preview and Printing Problem *fixed*

Posted 16 December 2006 - 03:55 PM

The problem is that you have left out one number in the Select statement.

You've covered if the value is less than and you've covered if its greater than, but what if it is equal to the length of the richtextbox. You need to modify one of the cases as noted below.
		Select Case Me.RichTextBox1.Text.Length
			Case Is > intCurrentChar
				'HasMorePages tells the printing module whether another PrintPage event should be fired
				e.HasMorePages = True
			Case Is <= intCurrentChar	'added equals sign here
				e.HasMorePages = False
				intCurrentChar = 0
		End Select


Was This Post Helpful? 0
  • +
  • -

#10 bingy  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 91
  • View blog
  • Posts: 690
  • Joined: 15-December 06

Re: Print Preview and Printing Problem *fixed*

Posted 16 December 2006 - 05:22 PM

View Postjayman9, on 16 Dec, 2006 - 03:55 PM, said:

The problem is that you have left out one number in the Select statement.

You've covered if the value is less than and you've covered if its greater than, but what if it is equal to the length of the richtextbox. You need to modify one of the cases as noted below.
		Select Case Me.RichTextBox1.Text.Length
			Case Is > intCurrentChar
				'HasMorePages tells the printing module whether another PrintPage event should be fired
				e.HasMorePages = True
			Case Is <= intCurrentChar	'added equals sign here
				e.HasMorePages = False
				intCurrentChar = 0
		End Select


ok that's fixed and working but what about it not using the default page settings. For some reason if you don't go in and set the margins it will use a 2" margin on the top and on the left. When you go in and set them to 0.5", it then uses a 1" margin for the top and left and 0.5" for the bottom and the right.
Was This Post Helpful? 0
  • +
  • -

#11 bingy  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 91
  • View blog
  • Posts: 690
  • Joined: 15-December 06

Re: Print Preview and Printing Problem *fixed*

Posted 16 December 2006 - 05:35 PM

here is the code i have written for this.

Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
		'PrintPage is the foundational printing event. This event gets fired for every 
		' page that will be printed
		Static intCurrentChar As Int32
		' declaring a static variable to hold the position of the last printed char
		Dim font As New Font("Verdana", 12)
		' initializing the font to be used for printing
		Dim PrintAreaHeight, PrintAreaWidth, marginLeft, marginTop As Int32
		With PrintDocument1.DefaultPageSettings
			' initializing local variables that contain the bounds of the printing area rectangle
			PrintAreaHeight = .PaperSize.Height - .Margins.Top - .Margins.Bottom
			PrintAreaWidth = .PaperSize.Width - .Margins.Left - .Margins.Right
			' initializing local variables to hold margin values that will serve
			' as the X and Y coordinates for the upper left corner of the printing 
			' area rectangle.
			marginLeft = .Margins.Left
			marginTop = .Margins.Top
			' X and Y coordinate
		End With

		If PrintDocument1.DefaultPageSettings.Landscape Then
			Dim intTemp As Int32
			intTemp = PrintAreaHeight
			PrintAreaHeight = PrintAreaWidth
			PrintAreaWidth = intTemp
			' if the user selects landscape mode, swap the printing area height and width
		End If

		Dim intLineCount As Int32 = CInt(PrintAreaHeight / font.Height)
		' calculating the total number of lines in the document based on the height of
		' the printing area and the height of the font
		Dim rectPrintingArea As New RectangleF(marginLeft, marginTop, PrintAreaWidth, PrintAreaHeight)
		' initializing the rectangle structure that defines the printing area
		Dim fmt As New StringFormat(StringFormatFlags.LineLimit)
		'instantiating the StringFormat class, which encapsulates text layout information
		Dim intLinesFilled, intCharsFitted As Int32
		e.Graphics.MeasureString(Mid(rtxtFile.Text, intCurrentChar + 1), font, New SizeF(PrintAreaWidth, PrintAreaHeight), fmt, intCharsFitted, intLinesFilled)
		' calling MeasureString to determine the number of characters that will fit in
		' the printing area rectangle
		e.Graphics.DrawString(Mid(rtxtFile.Text, intCurrentChar + 1), font, Brushes.Black, rectPrintingArea, fmt)
		' print the text to the page
		intCurrentChar += intCharsFitted
		'advancing the current char to the last char printed on this page
		Select Case rtxtFile.Text.Length
			Case Is > intCurrentChar
				'HasMorePages tells the printing module whether another PrintPage event should be fired
				e.HasMorePages = True
			Case Is <= intCurrentChar
				e.HasMorePages = False
				intCurrentChar = 0
		End Select
	End Sub



Private Sub mnusetup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnusetup.Click
		With PageSetupDialog1
			.PageSettings = PrintDocument1.DefaultPageSettings
		End With
		Try
			If PageSetupDialog1.ShowDialog = DialogResult.OK Then
				PrintDocument1.DefaultPageSettings = PageSetupDialog1.PageSettings
			End If
		Catch es As Exception
			MessageBox.Show(es.Message)
		End Try
	End Sub


if you go into the setup it takes some of the settings as per right and bottom margins and uses 1" margins for the top and left and if you don't go in it prints to the end of the paper and also puts in the 2" margins on the left and top margins.
Was This Post Helpful? 0
  • +
  • -

#12 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Print Preview and Printing Problem *fixed*

Posted 16 December 2006 - 06:02 PM

Not sure why you are experiencing that problem. It works just fine when I do it. Default prints at 1" all margins. And if I set all margins to .5" it works fine too.

May have something to do with the model of printer your using and its default print margins. Don't really know for sure though.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1