Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the preferred method for printing to a printer in VB.NET?

A little background: Creating a program that generates reports and forms from database inputs. I have everything up and running just fine, but I cannot seem to figure out my printing issue. I have tried several methods for printing to a printer and every document that I print from my custom program is slightly off-center to the right on the page.

I understand the reason for this is the printer itself, but how can I accomodate for this? In print preview the report looks perfect. I need some guidance on what methods will work across multiple, varied, printers in VB.NET. Any help is greatly appreciated.

like image 810
FailHorse Avatar asked Dec 18 '12 16:12

FailHorse


1 Answers

Instead of using e.pagebounds for the bounding box, try using e.graphics.VisibleClipBounds. Some printer drivers are a little unreliable with the e.pagebounds, and the visibleclipbounds seems more accurate.

For example:

Sub printPage(ByVal sender As Object, ByVal e As PrintPageEventArgs)
Dim g As Graphics
g = e.Graphics
g.ResetTransform()

g.VisibleClipBounds is now a rectangle that should be the maximum page size, and can be used for centering text or graphics. Just use g as the graphics object for outputting to the printer. The horizontal center of the page is

g.VisibleClipBounds.Left + g.VisibleClipBounds.Width / 2
like image 132
xpda Avatar answered Sep 20 '22 13:09

xpda