How do I crop a PDF page?

Q: Can I crop a PDF with PDFNet sdk? Some of our PDF files that are
uploaded have crop marks, is there anyway of auto-cropping or if we
give the coords to pdfnet sdk would it crop the PDF... not crop an
image after the conversion, but crop the actual PDF. Just like if i
were using adobe acrobat.. it would crop it.

I thank you in advance for all of your help.
------
A: You can crop PDF pages by adjusting the crop box on a PDF page
(i.e. page.Get/SetCropBox(rect)). The relevant sample project is
'RectTest' (http://www.pdftron.com/net/samplecode.html#Rect) ... and
of course you would be adjusting the crop box instead of the media
box.

Q: Thanks for your response. I have seen this example, and several
others on your site. I am able to use this code to increase the size
of the page. My problem lies in the position of the existing data on
the page. I can’t seem to figure out how to get the extra space at
the bottom of the editable area. Every time I increase the page
vertically by a half inch, the existing elements on the page fill in
the bottom 11 inches of the page, instead of the top 11 inches. Can
you give me a code sample that will increase an 8 1/2 x 11 page to 8
1/2 x 11 1/2, leaving the bottom 1/2 inch as editable whitespace?


A: To add extra space at the bottom of the page you would subtract a
number from y1 coordinate. For example,

// Assuming C# (C/C++/JAVA/VB is similar)

Page pg = doc.GetPage(1);

Rect box = pg.GetMediaBox();
box.y1 -= 31; // 1 inch = 72 pts
box.Update();

box = pg.GetCropBox();
box.y1 -= 32;
box.Update();

Please keep in mind that this code does not take into account page
rotation (page.GetRotation()).

Q: That code definitely shows the page with a 1/2 inch of whitespace
at the bottom. But is it editable? I can't seem to write any text in
the new area. Here is the code I'm using to write a text element:

Page pg = doc.GetPage(1);
Rect box = pg.GetMediaBox();
box.y1 -= 32;
box.Update();
box = pg.GetCropBox();
box.y1 -= 32;
box.Update();

ElementBuilder eb = new ElementBuilder();
ElementWriter writer = new ElementWriter();
Element elmt; Font f = Font.CreateTrueTypeFont(id, "c:\\windows\\fonts\
\arial.TTF", true, false);
writer.Begin(pg);
elmt = eb1.CreateTextBegin(f, 12);
writer.WriteElement(elmt);
elmt = eb.CreateTextRun("Test Text");
elmt.SetTextMatrix(1, 0, 0, 1, 216, -15);
writer.WriteElement(elmt);
writer.WriteElement(eb.CreateTextEnd());
writer.End();

If I set the v coordinate of SetTextMatrix to 0, the text shows 1/2
inch above the bottom of the page. If I set the v coordinate to a
negative value, the text disappears, instead of writing in the new
whitespace area. I'm obviously missing something on how you can
insert data elements into the new area that was created.
-----------
A: The problem is that the page origin does not coincide anymore with
[0, 0]. To solve this issue when adding new content to you may want to
refer the following article:
  http://groups.google.com/group/pdfnet-sdk/browse_thread/thread/10190611076bd2cd/eb7382f49c0ee0fe

or search for "add new content to an existing (rotated/cropped) PDF
page" in
  http://groups.google.com/group/pdfnet-sdk/

For users of PDFNet v4.1 or above the simplest solution is to use
writer.BeginNormalized(page) instead of more generic
writer.Begin(page).