Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Printing and Margin Bounds 3

Status
Not open for further replies.

jonbatts

Programmer
Apr 12, 2005
114
US
Alright, I'm printing on a printdocument [tt]e.Graphics.DrawRectangle(Pens.Red, e.MarginBounds)[/tt] with my margins all set to one inch. Looks fine on the print preview, but when I print, my right margin is 1 1/4 inches and my left is 3/4 of an inch. When I print from other applications (Microsoft Paint, and Word) I can get 1 inch margins perfect. I haven't found a reason anywhere why my app can't do it. This question seems to have been asked different ways many times with no certain answers. Anyone have any ideas? Thanks.
 
Dig deeper into the link from Dimandja..look at the links at the bottom, and there is a function to return a Margins object which looks much better.


Sweep
...if it works dont f*** with it
...if its f****ed blame someone else
...if its your fault that its f***ed, say and admit nothing.
 
Jon

Ive had a quick look at the links and tried to implement it, but to no avail. The printdocument margins problem is really annoying, and I can see the logic behind what the link is trying to fix, its just that it doesnt seem quite right, and I havent got the time to look deep into it right now. If you manage to make any progress, please post back, and share your findings. I for one would be most appreciative.

What workarounds does anyone else use with Printdocument, and erroneous margins?



Sweep
...if it works dont f*** with it
...if its f****ed blame someone else
...if its your fault that its f***ed, say and admit nothing.
 
HOORAH! Finally got it to work. Here we go. Place
Code:
Declare Function GetDeviceCaps% Lib "GDI32" (ByVal hDC%, ByVal nIndex%)
in your code to use GetDeviceCaps which helps you get information about your printer (or other devices). Then in the PrintPage event of your PrintDocument put
Code:
Dim hDC As IntPtr
Dim hardMarginLeft As Integer
Dim hardMarginTop As Integer

hDC = e.Graphics.GetHdc()
hardMarginLeft = GetDeviceCaps(hDC.ToInt64, 112)
hardMarginTop = GetDeviceCaps(hDC.ToInt64, 113)
e.Graphics.ReleaseHdc(hDC)
hardMarginLeft = (hardMarginLeft * 100.0) / e.Graphics.DpiX
hardMarginTop = (hardMarginTop * 100.0) / e.Graphics.DpiY
112, and 113 are specific constants which represent the x and y offsets of the device. Other integers represent other aspects of the device. So now you've got the printers margins which aren't calculated in to [tt]e.MarginBounds[/tt]. So in my code, when determining how much space I have to print on and where I can print I use
Code:
Dim marginBoundsWidth, marginBoundsHeight, marginBoundsX, marginBoundsY as Integer
marginBoundsWidth = e.MarginBounds.Width
marginBoundsHeight = e.MarginBounds.Height
marginBoundsX = e.MarginBounds.X - hardMarginLeft
marginBoundsY = e.MarginBounds.Y - hardMarginTop
One thing. If you use this in your PrintPage event, you have to distinguish between a print preview, and an actual print. If you use this for both your print preview margins will appear slightly off because this code is only necessary for printing, not print previewing.
 
Nice one John....a purpley for you too!

Good old M$...they apparently know about this bug
(ahem...feature), but surprisingly havent fixed it in Ver2 of the framework either


Sweep
...if it works dont f*** with it
...if its f****ed blame someone else
...if its your fault that its f***ed, say and admit nothing.
 
but surprisingly havent fixed it in Ver2 of the framework either

that's not surprising, it's called jobprotection.

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top