PrintDocument and PrintPreviewDialog

D

Duke

I have a PrintDocument which is passed to a PrintPreviewDialog. The
format looks exactly the way I want it when the PrintPreviewDialog
comes up. When I click on the printer icon and actually print it, the
paper comes out with everything moved down approximately 1 inch. I've
tried printing on several different printers and types of printers
(laser, ink jet, etc.)

Is there something I have to do besides pass the PrintDocument to the
PrintPreviewDialog to get this to work right?

Dim pp As New PrintPreviewDialog
pp.Document = pd
pp.WindowState = FormWindowState.Maximized
pp.ShowDialog()

(pd is my PrintDocument)

Thank you,
Duke


duke061720040933
 
H

Herfried K. Wagner [MVP]

* (e-mail address removed) (Duke) scripsit:
I have a PrintDocument which is passed to a PrintPreviewDialog. The
format looks exactly the way I want it when the PrintPreviewDialog
comes up. When I click on the printer icon and actually print it, the
paper comes out with everything moved down approximately 1 inch. I've
tried printing on several different printers and types of printers
(laser, ink jet, etc.)

PrintPreviewDialog doesn't care about physical printer offsets.

My FAQ:

Sample is based on a C# sample written by Ron Allen.

The method 'GetHardMargins' expects a hDC to the printer's 'Graphics'
object during the print call. The printer may have a margin that is
different from the PrintPreViewDialog's margin. Values are returned in
the procedure's parameters:

\\\
Public Declare Function GetDeviceCaps Lib "gdi32.dll" ( _
ByVal hdc As IntPtr, _
ByVal nIndex As Int32 _
) As Int32

Private Const PHYSICALOFFSETX As Int32 = 112
Private Const PHYSICALOFFSETY As Int32 = 113
Private Const HORZRES As Int32 = 8
Private Const VERTRES As Int32 = 10
Private Const HORZSIZE As Int32 = 4
Private Const VERTSIZE As Int32 = 6

Public Sub GetHardMargins( _
ByVal hDC As IntPtr, _
ByRef Left As Single, _
ByRef Top As Single, _
ByRef Right As Single, _
ByRef Bottom As Single _
)
Dim offx As Single = _
Convert.ToSingle(GetDeviceCaps(hDC, PHYSICALOFFSETX))
Dim offy As Single = _
Convert.ToSingle(GetDeviceCaps(hDC, PHYSICALOFFSETY))
Dim resx As Single = _
Convert.ToSingle(GetDeviceCaps(hDC, HORZRES))
Dim resy As Single = _
Convert.ToSingle(GetDeviceCaps(hDC, VERTRES))
Dim hsz As Single = _
Convert.ToSingle(GetDeviceCaps(hDC, HORZSIZE)) / _
25.4F ' Screen width in inches.
Dim vsz As Single = _
Convert.ToSingle(GetDeviceCaps(hDC, VERTSIZE)) / _
25.4F ' Screen height in inches.
Dim ppix As Single = resx / hsz
Dim ppiy As Single = resy / vsz
Left = (offx / ppix) * 100.0F
Top = (offy / ppix) * 100.0F
Bottom = Top + (vsz * 100.0F)
Right = Left + (hsz * 100.0F)
End Sub
///
 
D

Duke

Thanks for the response. That's good code to know.

I was making a dumb mistake though. I set the Y value I was at in the
public Print subroutine instead of the private pd_PrintPage routine.
When I printed, it was starting from the last Y value I had used
instead of resetting Y to be at the top margin.

Duke
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top