views:

229

answers:

2

My application draws charts in a Windows Metafile. Users need to be able to print the charts nicely centered on the page. Quick rundown of printing code:

Private Sub PrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs)
     Dim header As Imaging.MetafileHeader = metafile.GetMetafileHeader()
     Dim sz As New SizeF(100 * header.Bounds.Width / header.DpiX, 100 * header.Bounds.Height / header.DpiY)
     Dim p As New PointF((e.PageBounds.Width - sz.Width) / 2, (e.PageBounds.Height - sz.Height) / 2)

     e.Graphics.DrawImage(metafile, p)
End Sub

If I print to a PDF, this looks perfect. But if I print to an actual printer it is off center, about 1/8 of an inch down and to the right. I did a quick experiment to see where the PageBounds were by doing

e.Graphics.DrawRectangle(Pens.Red, e.PageBounds)

and the results were the same, slightly off center. On the PDF this draws a rectangle on the very edge of the page. For what its worth, I have tested on a Toshiba e-Studio 3510c and a HP LaserJet 4000 with the same results. Any help is appreciated, this has been plaguing me for weeks.

Update:

I ended up using e.PageSettings.PrintableArea, but it looks like e.Graphics.VisibleClipBounds gets you the same values as per xpda's answer.

A: 

I think it is a problem of the printer having different margins on all sides of the page. Instead of positioning your chart relative to the Bounds, try to position it relative to the the real border of the page by using these properties:

e.PageSettings.PaperSize.Width
e.PageSettings.PaperSize.Height

The problem probably doesn't arise with the pdf because you can print on the entire area of the page (i.e. Bounds.Width and Bounds.Height = 0, which is the same as using the PaperSize property).

Julien Poulin
+1  A: 

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.

xpda