Printer Torture

bcorbett

Active member
Joined
Oct 16, 2006
Messages
27
Programming Experience
Beginner
Let me begin by saying UUUUUUUUURRRRRRRRRRGGGGGGGGGHHHHHHHH!!!!

ok, I feel better.

Heres my issue, I have an OLD Pitney Bowes Envelope Printer that the company’s owner has become fond of and doesn't want to part with. So I have been tasked with the job of
converting the FoxPro app to VB.net. I originally had this as a Web app
I couldn't figure out how to get the printer to recognize the "Form Feeds" when I printed from the browser.
so I set it up to be a download and the user would have to open it up in WordPad to print. Why WordPad? You might ask,
Well, that’s because Notepad did the same thing that the browser did. It just ignored the form feeds. Well the boss wasn't
happy with that, so now they want it to be a windows app and they want it be a one button click. No problem, I just used most of the code I used in the
Web app and used the Printdocument object to handle the printing, Sounds good doesn't it?

Well, I thought that too, til I printed the first run. Its doing the same thing now that Notepad and the browser did.
So my question is.................What am I doing wrong? I pretty much know its not seeing the "form Feeds" because it prints them out as periods. There are 8 lines per envelope, what it does is prints part of two labels on one page. With word pad it
prints one label kicks out the envelope and prints the next.

I'm stuck so any help will GREATLY be appreciated.
 
Each time PrintPage (the event handler method) is called it prints one sheet, if that helps to understand. If you in that method set e.HasMorePages=True it will be called again to print next sheet.

Glad you feel better, but you didn't vomit on the forum, did you? It almost sounded so. Anyway, moved you to printing forum for much good company.
 
No sir that was a stress Cry. I'm pulling my hair out...LOL

It prints that page to the print spooler right?

Are you saying I need to turn that off so It will print the entire file as one page? Thanks for moving me...lol!

Heres the code I use for printing:

Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As _
System.Drawing.Printing.PrintPageEventArgs)
Handles _
PrintDocument1.PrintPage
Dim linesPerPage As Single = 0
Dim yPos As Single = 0
Dim count As Integer = 0
Dim leftMargin As Single = 0
Dim topMargin As Single = 0
Dim line As String = Nothing
linesPerPage = e.MarginBounds.Height / printFont.GetHeight(e.Graphics)
While count < linesPerPage
line = fileToPrint.ReadLine()
If line Is Nothing Then
Exit While
End If
yPos = topMargin + count * printFont.GetHeight(e.Graphics)
e.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, _
yPos,
New StringFormat)
count += 1
End While
If Not (line Is Nothing) Then
e.HasMorePages = True
End If
End Sub



 
No, not sure what you suggest, but your task in PrintPage is to draw the graphics of that page (draw text, draw circles, draw rectangles, draw images etc), no control codes will be interpretes as anything else than a curly character if you try to draw them with the Graphics.DrawString method. The image of the printed page you make up there is sent behind the .Net curtains to the printer as a single image when that method is finished, and as I said, if you have more pages you tell so to HasMorePages property and that same method is called again. Now the 'second' time this method calls you better have an index ready to continue where you left off on first page, else you will just start over and over again and print the first page endlessly.
 
try a webbrowser with bitblt

Perhaps if you try letting the browser read an image rather than text it will print it correctly. if you can show your info in the Webbrowser you can make an image of that then return that to the web browser and the web browser knows nothing of text, print just the image. It might make things work.

VB.NET:
Expand Collapse Copy
Public Function Set_Content(byval strStreet as string and so on)
     dim content as string
     content = "<html><body><font color='red' size='5'><center>Property Sold "
             _& </center></font><br><br><br> Property Address : "
             _& strStreet & ", " & strSuburb & ", " & strCity & "<br><br> Lister : "
             _& strName & "<br><br>Seller : " & strSeller & "<br><br>Price : "
             _& Format(decPrice, "c") & "</body></html>"
     Return content
End Sub
 
Sub display
     WebBrowser1.DocumentText = Set_Content
End Sub
 
Sub Print_Document
     WebBrowser1.Print()
End Sub

If you can create an image of a selected part of this display you could then set the image to the web browser and use the webbrowser to print just the image or something like that. you can use bitblt.
Just copy this code and paste into a test form and add appropriate controls, it should work as is...
This example uses picturebox's. but it should be easy to take an image from a webbrowser and then clear the webbrowser and set the image back to the webbrowser. It might just work if you use image rather than text plus you can set the image to the exact size you want and even just scale it to a diferent size if you use multiple types of envelopes.
VB.NET:
Expand Collapse Copy
[SIZE=2][COLOR=#0000ff]Imports[/COLOR][/SIZE][SIZE=2] System.Drawing.Drawing2D[/SIZE]
 
[SIZE=2][COLOR=#0000ff]Declare[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Auto[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE][SIZE=2] BitBlt [/SIZE][SIZE=2][COLOR=#0000ff]Lib[/COLOR][/SIZE][SIZE=2][COLOR=#800000]"GDI32.DLL"[/COLOR][/SIZE][SIZE=2] ( _[/SIZE]
[SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] hdcDest [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] IntPtr, _[/SIZE]
[SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] nXDest [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2], _[/SIZE]
[SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] nYDest [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2], _[/SIZE]
[SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] nWidth [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2], _[/SIZE]
[SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] nHeight [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2], _[/SIZE]
[SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] hdcSrc [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] IntPtr, _[/SIZE]
[SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] nXSrc [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2], _[/SIZE]
[SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] nYSrc [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2], _[/SIZE]
[SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] dwRop [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Int32) [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Boolean[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE][SIZE=2] copyRect([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] src [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] PictureBox, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] rect [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] RectangleF) [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Bitmap[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] srcPic [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Graphics = src.CreateGraphics [/SIZE][SIZE=2][COLOR=#008000]'Get a Graphics Object from the form [/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] srcBmp [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Bitmap(src.Width, src.Height, srcPic) [/SIZE][SIZE=2][COLOR=#008000]'Create a EMPTY bitmap from that graphics [/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] srcMem [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Graphics = Graphics.FromImage(srcBmp) [/SIZE][SIZE=2][COLOR=#008000]'Create a Graphics object in memory from that bitmap [/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] HDC1 [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] IntPtr = srcPic.GetHdc [/SIZE][SIZE=2][COLOR=#008000]'get the IntPtr's of the graphics [/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] HDC2 [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] IntPtr = srcMem.GetHdc [/SIZE][SIZE=2][COLOR=#008000]'get the IntPtr's of the graphics [/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'get the picture [/COLOR][/SIZE]
[SIZE=2]BitBlt(HDC2, 0, 0, rect.Width, rect.Height, HDC1, rect.X, rect.Y, 13369376)[/SIZE]
[SIZE=2][COLOR=#008000]'Clone the bitmap so we can dispose this one [/COLOR][/SIZE]
[SIZE=2]copyRect = srcBmp.Clone()[/SIZE]
[SIZE=2][COLOR=#008000]'Clean Up [/COLOR][/SIZE]
[SIZE=2]srcPic.ReleaseHdc(HDC1)[/SIZE]
[SIZE=2]srcMem.ReleaseHdc(HDC2)[/SIZE]
[SIZE=2]srcPic.Dispose()[/SIZE]
[SIZE=2]srcMem.Dispose()[/SIZE]
[SIZE=2]srcMem.Dispose()[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] dstCopy_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] dstCopy.Click[/SIZE]
[SIZE=2]dest.Image = [/SIZE][SIZE=2][COLOR=#0000ff]CType[/COLOR][/SIZE][SIZE=2](copyRect(src, [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] RectangleF(0, 0, 50, src.Height)), Bitmap).Clone[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
Last edited:
Back
Top