Printing PDF - How to offset the page?

Q: When I am printing a PDF, its cutting the right side and bottom of
the PDF off slightly , I looked at the sample code and ran it using
same pdf and it does the same thing printing the bitmap or using the
PDFDraw class. Any Assistance would greatly be appreciated.

Here is my code:

Private Sub Print_Document()

        Try
            'create PDF Rasterizer object
            PDFDraw = New PDFDraw
            itr = PDFDocument.PageBegin()
            Printer = New PrintDocument
            Printer.Print()
            PDFDraw = Nothing
            Printer = Nothing
        Catch ex As Exception
            LogError(ex, True)
        End Try

    End Sub

    Private Sub PrintPage(ByVal sender As System.Object, ByVal ev As
PrintPageEventArgs) Handles Printer.PrintPage
        Dim rectPage As Rectangle = ev.PageBounds
        Dim gr As System.Drawing.Graphics = ev.Graphics
        Dim dpi As Single = gr.DpiX

        If dpi > 200 Then
            dpi = 200
        End If

        PDFDraw.SetDPI(dpi)

        Dim bmp As Bitmap = PDFDraw.GetBitmap(itr.Current())
        gr.DrawImage(bmp, rectPage, 0, 0, bmp.Width, bmp.Height,
GraphicsUnit.Pixel)

        itr.Next()
        If itr.Equals(PDFDocument.PageEnd()) Then
            ev.HasMorePages = False
        Else
            ev.HasMorePages = True
        End If
    End Sub
-----

A:

You may want to adjust page boundary (i.e. rectPage ) to account for
the HardMarginX.

Dim rectPage As Rectangle = ev.PageBounds
Dim left As Double = (rectPage.Left - ev.PageSettings.HardMarginX)
Dim right As Double = (rectPage.Right - ev.PageSettings.HardMarginX)
Dim top As Double = (rectPage.Top - ev.PageSettings.HardMarginY)
Dim bottom As Double = (rectPage.Bottom - ev.PageSettings.HardMarginY)
rectPage.Left = left
rectPage.Right = right
rectPage.Top = top
rectPage.Bottom = bottom
...

Another printing example is as follows:

Dim rectPage As Rectangle = ev.PageBounds 'print without margins
Dim gr As System.Drawing.Graphics = ev.Graphics
gr.PageUnit = GraphicsUnit.Inch
Dim dpi As Double = gr.DpiX
If dpi > 200 Then dpi = 200

Dim left As Double = (rectPage.Left - ev.PageSettings.HardMarginX) /
100.0
Dim right As Double = (rectPage.Right - ev.PageSettings.HardMarginX) /
100.0
Dim top As Double = (rectPage.Top - ev.PageSettings.HardMarginY) /
100.0
Dim bottom As Double = (rectPage.Bottom - ev.PageSettings.HardMarginY)
/ 100.0
Dim rect As PDFTRON.PDF.Rect = New Rect(left * 72, bottom * 72, right *
72, top * 72)

Try
  pdfDraw.SetImageSmoothing(True)
  pdfDraw.SetAntiAliasing(True)
  ' pdfDraw.SetRasterizerType(PDFTRON.PDF.PDFRasterizer.Type.e_GDIPlus)
  pdfDraw.SetDPI(dpi)
  pdfDraw.DrawInRect(printItr(PnlActive).Current, gr, rect)
Catch ex As Exception
  MessageBox.Show("Printing Error: " + ex.ToString)
End Try

Q:

I am getting the following messages in Visual Studio 2003.

HardMarginX or HardMarginY are not members of
System.Drawing.Printing.PageSettings
rectPage.Left property is Readonly
----
A:

Looking at System.Drawing.Printing.PageSettings in MSDN documentation
I can see that HardMarginX and HardMarginY are public Properties.

Perhaps you need to create a new rectangle? Something along the
following lines:

Dim rectPage2 As Rectangle = new Rectangle
rectPage2.Left = left
rectPage2.Right = right
rectPage2.Top = top
rectPage2.Bottom = bottom

Q: I checked and that is a new property of .Net framework 2.0. I am
still developing in vs 2003 and 1.1.
----

A: As a workaround you may want to use GetDeviceCaps Win32 method to
access the 'hard margin' printer property. Because this is more of an
issue related to .NET Framework than to PDFNet API this may not be the
optimal solution.

// Static members
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
private static extern int GetDeviceCaps(IntPtr hdc, int nIndex);
private const int PHYSICALOFFSETX = 112;
private const int PHYSICALOFFSETY = 113;

...
  // Get handle to device context.
  IntPtr hdc = new IntPtr();
  hdc = ev.Graphics.GetHdc();

   // Code to obtain the hard margin.
  double pox = GetDeviceCaps(hdc, PHYSICALOFFSETX);
  double poy = GetDeviceCaps(hdc, PHYSICALOFFSETY);
  double HardMarginLeft = pox * 100.0;
  double HardMarginTop = poy * 100.0;

  // Release handle to device context.
  ev.Graphics.ReleaseHdc(hdc);

      ... Use HardMarginLeft/HardMarginTop as before.