How can I scale the content of existing PDF page without changing the size of the Page itself?

Q: How can I scale the content of existing PDF page without changing
the size of the Page itself. I mean to set tthe size of the content
smaller so i can add some stuff in the border/margin of each page. I
found a sample on the google groups that explains how to change the
size of the content via setcropbox, setmediabox and page.scale but the
result is that the entire page (and not only the content) gets scaled
or smaller.

I also need to CENTER the scalled content in the page, so it it is
nicelly displayd in the center... logically.
-----
A: You can scale the content of existing PDF page without changing the
size of the Page itself as follows:

// 1) Get the current page boxes
Rect old_media_box = page.GetMediaBox(); Rect old_crop_box =
page.GetCropBox();

// 2) Scale the content (and page dimensions)...
page.Scale(0.5); // Scale page 50%...

// 3) Reset page boxes taking into accout old values:
Rect box = page.GetMediaBox();
box.x2 = box.x1 + old_media_box.Width();
box.y2 = box.y1 + old_media_box.Height(); page.SetMediaBox(box);

box = page.GetCropBox();
box.x2 = box.x1 + old_crop_box.Width();
box.y2 = box.y1 + old_crop_box.Height(); page.SetCropBox(box);
-----------

To center the content you could offset box.x1/box.y1. For example:

...
box.x1 = box.x1 + (old_media_box.Width()-box.Width())/2;
box.y1 = box.y1 + (old_media_box.Height()-box.Height())/2;
box.x2 = box.x1 + old_media_box.Width();
box.y2 = box.y1 + old_media_box.Height(); ...

Q: The scale solution works fine, but I still don’t get the content
centered in the page. Could you help me out?
This is my shrinkthepage code:

static void ShrinkPageContent(Page page)
{
Rect crop_box = Page.GetCropBox();
Rect media_box = Page.GetMediaBox();
double dScale = .95;
page.Scale(dScale);

page.SetCropBox(crop_box);
page.SetMediaBox(media_box);

Rect box = page.GetMediaBox();
box.x1 = box.x1 + (media_box.Width() - box.Width()) / 2;
box.y1 = box.y1 + (media_box.Height() - box.Height()) / 2;
box.x2 = box.x1 + media_box.Width();
box.y2 = box.y1 + media_box.Height();

}

what am i doing wrong?


A: Your code used to reset the media & crop box is not completely
correct. You may want to try the following:

static void ShrinkPageContent(Page page)
{
Rect crop_box = Page.GetCropBox();
Rect media_box = Page.GetMediaBox();
double dScale = .95;
page.Scale(dScale);

Rect box = page.GetMediaBox();
box.x1 = box.x1 + (media_box.Width() - box.Width()) / 2;
box.y1 = box.y1 + (media_box.Height() - box.Height()) / 2;
box.x2 = box.x1 + media_box.Width();
box.y2 = box.y1 + media_box.Height();
page.SetMediaBox(box);

box = page.GetCropBox();
box.x1 = box.x1 + (crop_box.Width() - box.Width()) / 2;
box.y1 = box.y1 + (crop _box.Height() - box.Height()) / 2;
box.x2 = box.x1 + crop_box.Width();
box.y2 = box.y1 + crop_box.Height();
pa

Q: It seems that the your snippet shifts PDF page content to the left
and down. I would like to shift the page to center.
------
A: Sorry I didn't test the code. To correct this simply switch the
signs:

static void ShrinkPageContent(Page page) {
  Rect crop_box = Page.GetCropBox();
  Rect media_box = Page.GetMediaBox();
  double dScale = .95;
  page.Scale(dScale);

  Rect box = page.GetMediaBox();
  box.x1 = box.x1 - (media_box.Width() - box.Width()) / 2;
  box.y1 = box.y1 - (media_box.Height() - box.Height()) / 2;
  box.x2 = box.x1 + media_box.Width();
  box.y2 = box.y1 + media_box.Height();
  page.SetMediaBox(box);

  box = page.GetCropBox();
  box.x1 = box.x1 - (crop_box.Width() - box.Width()) / 2;
  box.y1 = box.y1 - (crop _box.Height() - box.Height()) / 2;
  box.x2 = box.x1 + crop_box.Width();
  box.y2 = box.y1 + crop_box.Height();
  page.SetCropBox(box);
}