HardMarginX & HardMarginY Problems

Hi,

I'm trying to print to a HP DeskJet printer. Using PrintPreview the output is properly centered on the page, but when I print the page, the output is shifted down and to the right. After a lot of head-scratching, I determined that the printed image is shifted right by an amount equal to HardMarginX and shifted down by an amount equal to HardMarginY. I then subtracted HardMarginX from Margin.Left and HardMarginY from Margin.Top. With this adjustment the printed page is perfectly centered, but the PrintPreview is now shifted up and to the left. The values for the margins are:

Margins=[Margins Left=50 Right=50 Top=100 Bottom=100]
[HardMarginX]: 25, [HardMarginY]: 50

How can I fix this problem? Thanks for your assistance.

[761 byte] By [GrandpaB] at [2007-12-24]
# 1

Hi back again,

I had a post on the VB Express forum about programmatically changing the paper size and orientation. In that post I whined about this offset problem that I was having. Today, I received a reply from Tall Dude that was a eureka moment. He educated me about the PrintDocument.PrintController.IsPreview option. The problem was solved with the following code snippet.

Dim lMargin As Int32 = e.MarginBounds.X
Dim tMargin As Int32 = e.MarginBounds.Y
If Not PrtDoc.PrintController.IsPreview
Then
lMargin -= PrtDoc.DefaultPageSettings.HardMarginX
tMargin -= PrtDoc.DefaultPageSettings.HardMarginY
End
If

The code was placed in the PrintDocument.PrintPage event handler. The margins are set to the value in e (the event arguments), but if the outpt is not going to a PrintPreview the HardMargins X and Y are subtracted from these two margins.

GrandpaB at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 2
Man you've just saved my neck (I hope). I have had the same problem with the hardmargins. I am going home to use your code snippet provided by Tall Dude. Thanks for posting this code for all of us desperate programmers!
RicherTextBox at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 3
Bummer! I tried to use your code snippet but I cannot find any reference to HardMarginX or even PrintController. ==> ISPREVIEW <== ! I have the VB.Net 2003 Standard Edition. The only reference to the printer's margins were found by the following syntax: PrinterSettings.DefaultPageSettings.Margins. When I queried this at run time the margins showed: Bottom: 100 Left: 100 Right: 100 Top: 100. The print preview is fine but it is offset to the right at print time! I've never been more stumped in my life. If anybody has further help I would be eternally grateful!
RicherTextBox at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 4

The values GrandPaB talked about are available

in VB Express Edition.

There is a 'PrintDocument.OriginAtMargins Property'

that I have not studied closely that may be available

in your version. (I only have VB EE)

It may be useful.

TallDude at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...