We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Changing the Media Box Size

I have a requirement where the pdf i have has the media box of a smaller size , I need to resize the media box of it but looks like the below code isn’t doing so.

string clientDataDir = @“C:\messed up media and rotation\Original files\J100 Small~5.pdf”;
string correctDataDir = @“C:\tmp\J100.pdf”;

        Document correctDoc = new Document(correctDataDir);
        Page correctPdfPage = correctDoc.Pages[1];

        Document clientDoc = new Document(clientDataDir);
        PageCollection clientPageCollection = clientDoc.Pages;
        Page clientPdfPage = clientPageCollection[1];

        clientPdfPage.SetPageSize(correctPdfPage.MediaBox.Width, correctPdfPage.MediaBox.Height);
        Rectangle mediaBox = correctPdfPage.MediaBox;
        clientPdfPage.MediaBox = mediaBox;
Please provide the solution.


Thanks for contacting support.

Would you please share sample PDF document with us, so that we can test the scenario in our environment and address it accordingly.

J100 Small~5.pdf (312.9 KB)

Please pull up the pdf in the Illustrator as the adobe reader shows only the content within the media box it has but the page has got more content on it that we need to display, we need to increase the mediabox size and want to place the content at the center (equally placed from left/right and top/bottom).

Let me know if you need any further info.


Thanks for writing back.

You may please try setting all boxes of page equal to your target page rectangle as follows:

clientPdfPage.MediaBox = correctPdfPage.Rect;
clientPdfPage.ArtBox = correctPdfPage.Rect;
clientPdfPage.BleedBox = correctPdfPage.Rect;
clientPdfPage.CropBox = correctPdfPage.Rect;
clientPdfPage.TrimBox = correctPdfPage.Rect; 

In case you still face any issue, please share target PDF document i.e. correctDoc. We will again test the scenario in our environment and address it accordingly.

I tried the same code but it doesn’t position the content in the center.

Attached is the correct doc.
J100.pdf (318.1 KB)


We have worked with the file shared by you and have been able to notice the problem. A ticket with ID PDFNET-45471 has been logged in our issue management system for further investigations. The ticket ID has been linked with this thread so that you will receive notification as soon as the ticket is resolved.

We are sorry for the inconvenience.

Hopefully you already found a solution, but for future readers:

We were able to solve a similar problem (we were expanding the media box instead of shrinking it) like this:

// Get both rectangles
Rectangle clientRectangle = clientPdfPage.MediaBox;
Rectangle corRectangle = correctPdfPage.MediaBox; // Forgive the pun

// This is the un-intuitive part. For the various boxes to be the correct size AND 
// center themselves on the client pdf, we'll need to adjust their coordinates.
// Otherwise, they'll be off-center due to a mismatch between the sizes of each document.
var widthShift = (corRectangle.Width - clientRectangle.Width)/2;
var heightShift = (corRectangle.Height - clientRectangle.Height)/2;
Rectangle fixedRectangle =
    new Rectangle(
        clientRectangle.LLX - widthShift, clientRectangle.LLY - heightShift,
        clientRectangle.URX + widthShift, clientRectangle.URY + heightShift);

// Set all boxes
clientPdfPage.MediaBox = fixedRectangle;
clientPdfPage.ArtBox = fixedRectangle;
clientPdfPage.BleedBox = fixedRectangle;
clientPdfPage.CropBox = fixedRectangle;
clientPdfPage.TrimBox = fixedRectangle;

Please note that re-sizing the client PDF via clientPdfPage.SetPageSize() may break this. We didn’t need to change that setting to accomplish our goal. It may conflict with this solution.


These findings will certainly be helpful for other people with similar requirements. We appreciate your inputs.