General discussion

Locked

Printing in VB.NET

By doan_n ·
In VB6 we have PRINTER object to handle our printing chores. In VB.NET we have PrintDocument object. Personally, I don't have much info about this new .NET object. Can anyone help? Thank you.

This conversation is currently closed to new comments.

2 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

u din't read the win based application

by abdkel In reply to Printing in VB.NET

u have to instantait an object of the print document class
the u have to handle its print event
in the print event u should use the
e event arges to add the graphics u want
that's all

Collapse -

RE: u din't read the win based application

by ge.goodman In reply to u din't read the win base ...

Yeah Abdkel, "use the e event arges to add the graphics u want that's all" - that's great help. Not. You may or may not know what you're talking about, but from your post I would say you either know it all and don't want to help, or you know very little - either help the person who asked the question, or don't bother posting.


Try this

First of all you will need to Import the System.Drawing.Printing and possibly System.Drawing and System.Drawing.Text namespaces.

1 - Add a form-level PrintDocument instance

Private m_objPrint As PrintDocument = New System.Drawing.Printing.PrintDocument()


2 - In your constructor, form load (or anywhere else before you want to print), setup handlers for the PrintPage and EndPrint events that the PrintDocument object raises during printing: -

AddHandler m_objPrint.PrintPage, New PrintPageEventHandler(AddressOf OnPrintPage)

AddHandler m_objPrint.EndPrint, New PrintEventHandler(AddressOf OnEndPrint)

(NOTE - I've called my event handlers OnPrintPage and OnEndPrint, as per .NET convention)

3 - Write the handler functions. OnPrintPage (The PrintPage handler) will be called repeatedly by the framework (more in a sec) to print pages. It is in here you must decide the content of your page(s), this is the tricky bit.

4) Kick the process off by doing

m_objPrint.Print()

This will start the Print process - your PrintPage handler will be called. If you allow execution to finish in your PrintPage handler, printing will end and your EndPrint handler will be called. If you want to print another page, setting

args.HasMorePages = true

will make the print spooler complete the current page, request another, and your PrintPage handler will fire again. Repeat until printing is done.

Sounds easy doesn't it ? The devil is in the detail, as always - the complexity here is in what you want to print.

I've worked with text and graphics, the hard part is to determine

a) what each page should contain
b) when you want to start a new page
c) how to keep track of this between calls to your PrintPage handler
d) when printing should finish

Take the example of a simple text document. One way of printing this would be as follows :-

1) Build an in-memory collection of the document's paragraphs

2) Use a form-level integer variable to keep track of which paragraph is being printed

3) Inside your PrintPage handler, keep track of where you are on the page. When you run off the bottom of the page, you need to start a new one. The PrintPageEventArgs instance that gets passed into PrintPage contains some useful objects that allow you to determine the size of the page, so keeping track of where you are vertically on a page should be easy.

Drawing to the printer is simple enough. The PrintPageEventArgs does indeed have a "Graphics" child object. Use this in combination with the MarginBounds child object to draw images, shapes or text to the printer. The Graphics object is fairly complex (look it up under System.Drawing) but a quick example here will print a line of text to the printer -

(inside OnPrintPage)

Dim objGDI As Graphics = args.Graphics
Dim intLeft As Integer = args.MarginBounds.Left
Dim intWidth As Integer = args.MarginBounds.Width
Dim intHeight As Integer = args.MarginBounds.Height
Dim intYPosition As Integer = 0

' Use this for left/right/centre justification, trimming, etc
Dim objStringFormat as StringFormat = new StringFormat()
objStringFormat.Alignment = StringAlignment.Near

' ensure words aren't printed over the edge of the margin (start a new line)
objStringFormat.Trimming = StringTrimming.Word

' font to draw in
Dim fntFont as Font = new Font("Arial", 12, FontStyle.Regular)


' typically from here onwards will be in a loop, printing the document
' piece by piece but measuring each line as required

' text to draw
Dim strText as String = "Hello World"

' measure the size - you'll need to do this if you're printing several
' lines of text - measure the item, print it, move onto next one and
' increase intYPosition by the measured amount
Dim sizeText as SizeF = objGDI.MeasureString(strText, fntFont, intWidth)

' if it overruns the page height, start a new page
if intYPosition + sizeCell.Height > intHeight Then
args.HasMorePages = True
return

End If

' construct bounding rectangle - NOTE this will be the width of the page,
' regardless of alignment
Dim As RectangleF = new RectangleF(new PointF(intLeft, intYPosition), new SizeF(intWidth, sizeText.Height))

' draw the content, including formatting information
objGDI.DrawString(strCellText, fntFont, brushes.Black, rectLayout, objStringFormat)


4) Increment your form-level variable each time a paragraph is successfully printed, and when it exceeds the size of the "paragraphs" collection, allow processing in your PrintPage handler to finish. At this point your EndPrint handler will fire, where you can do any cleaning up (disposing of your collection, etc).


I can send you a better example if you want.

Regards

Gareth

Back to Windows Forum
2 total posts (Page 1 of 1)  

Related Discussions

Related Forums