How to print PDF pages on different paper sizes (e.g A4, legal style paper, etc)?


I have the need to print a batch of PDF forms on both letter and legal
style paper as part of the same process.

Can you please inform me how to use the Page.SetMediaBox and
PdfDraw.DrawInRect methods to distinguish between, and correctly
print, files in each page size based on the original document's aspect
ratio? I don't quite understand how these methods relate the actual
print surface to the document's measurements.


Page.SetMediaBox() can be used to crop the page or to adjust PDF page
size. However, this method (and other methods in Page class) do not
influence the scaling, positioning and even dimensions of the printed

The scaling and positioning of the printed page is specified using the
PDF.Rect parameter in pdfdraw.DrawInRect() method. PDFNet library
doesn't know anything about the currently selected printer or the
dimensions of printer paper, but you can use standard .NET Framework
APIs (or Win32 GDI functions if you are developing with C++) to find
all of the required information. For example:

If you are developing under .NET you can use
'ev.PageSettings.HardMarginX' and 'ev.PageSettings.HardMarginY' to
adjust the page rectangle in which you want to print. To take into
account PageSettings 'HardMargin' property you could modify 'PDFPrint'
sample ( as
follows :

' Using VB.NET
Dim rectPage As Rectangle = ev.PageBounds
Dim gr As System.Drawing.Graphics = ev.Graphics
gr.PageUnit = GraphicsUnit.Inch

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) / 100.0
Dim rect As PDFTRON.PDF.Rect = New Rect(left * 72, bottom * 72, right
* 72, top * 72)

  page = printItr(PnlActive).Current
  pdfDraw.DrawInRect(page, gr, rect)
Catch ex As Exception
  MessageBox.Show("Printing Error: " + ex.ToString)
End Try

If you are developing with C/C++ you could use GetDeviceCaps() Win32
function to obtain paper size, resolution, hard margin and other
printer properties.