How can I implement 'Fit page content to Printer Margins'?

Q:

I have a question on how to achieve "Fit/Reduce to Printer Margins"
when printing/drawing PDF pages. The other way to phrase this - how do
I draw a PDF page to disregard its margins and maximize all visible
elements of the page to be drawn on a bitmap with different margins.

The scenario is: texts in PDF page are close to page borders and its
page bounding boxes start at (0,0). Using the SDK, how do I get the
actual offsets of texts (or other visible elements) that are nearest
to the PDF page edges? Knowing such information I could apply
additional offset and scaling in ctx in page drawing so that the
visible elements can be drawn into a bitmap presenting different
printer device output without cut-off.
-----
A:

Essentially you would like to compute a bounding box for all content
on the page. This can be done as follows:

- Use ElementReader to traverse all graphical elements on the page
(similar to the code in ElementReader sample -
www.pdftron.com/net/samplecode.html#ElementReader).
- Use element.GetBBox(rect) to get the bounding box for each element
(i.e. text, image, or path).
- Find the bounding box rectangle that includes all bounding boxes of
sub-elements.
- Since the page content may also exist outside of the page media box,
you may also want to intersect the content rectangle with the page
crop box (i.e. (result_rect.IntersectRect (content_bbox,
page.GetCropBox()))).