Free Support Forum - aspose.com

Detecting & changing dimensions for intermediate landscape pages

Hi,


I’ve a few pages in document that are landscape.
But the page dimensions are portrait and content dimensions are Landscape.So, adobe reader is shrinking the oversized pages and displaying them as portrait.

How can I change the page dimensions to landscape ?

Attached the sample pdf doc.

Thanks,
Lalitya




Hi Lalitya,


Thanks for contacting support.

In order to change page size you can set MediaBox of the page in the following way.

Document doc = new Document(“PdfWithText.pdf”);<o:p></o:p>

foreach (Page page in doc.Pages)<o:p></o:p>

{<o:p></o:p>

Aspose.Pdf.Rectangle r = page.MediaBox;<o:p></o:p>

double newHeight = r.Width;<o:p></o:p>

double newWidth = r.Height;<o:p></o:p>

double newLLX = r.LLX;<o:p></o:p>

//we must to move page upper in order to compensate changing page size (lower edge of the page is 0,0 and information is usually placed from the top of the page. That’s why we move lover edge upper on difference between old and new height.<o:p></o:p>

double newLLY = r.LLY + (r.Height - newHeight);<o:p></o:p>

page.MediaBox = new Aspose.Pdf.Rectangle(newLLX, newLLY, newLLX + newWidth, newLLY + newHeight);<o:p></o:p>

//sometimes we also need to set CropBox (if it was set in original file)<o:p></o:p>

page.CropBox = new Aspose.Pdf.Rectangle(newLLX, newLLY, newLLX + newWidth, newLLY + newHeight);<o:p></o:p>

}<o:p></o:p>

doc.Save(“36115.pdf”);<o:p></o:p>


Please note that in that case you can cut some contents of the document (because we decrease height)
in order to avoid this you can increase width proportionally and leave height intact as following:

[C#]

Aspose.Pdf.Rectangle r = page.MediaBox;<o:p></o:p>

//new height the same<o:p></o:p>

double newHeight = r.Height;<o:p></o:p>

//new width is expanded proportionally to make orientation landscape (we assume that previous orientation is portrait)<o:p></o:p>

double newWidth = r.Height * r.Height / r.Width;<o:p></o:p>



Other option is to use PdfPageEditor facade (it can apply zoom to page contents)

[C#]


Document doc = new Document(“PdfWithText.pdf”);<o:p></o:p>

Aspose.Pdf.Rectangle r = doc.Pages[1].Rect;<o:p></o:p>

PdfPageEditor ppe = new PdfPageEditor();<o:p></o:p>

ppe.BindPdf(“PdfWithText.pdf”);<o:p></o:p>

ppe.Zoom = (float)(r.Width / r.Height);<o:p></o:p>

ppe.PageSize = new Aspose.Pdf.PageSize((float)r.Height, (float)r.Width);<o:p></o:p>

ppe.Save(“36115-1.pdf”);

Hi Nayyer,


Thanks for the quick reply.

how can i detect whether a page is landscape or portrait among mixed pages.

as i’m getting same height and width to all pages.

if i can detect all landscape pages then i can change dimensions as you have shown.

Thanks,
lalitya

Hi Lalitya,


Thanks for your patience and sorry for the delayed response.

Please try using following code snippet to determine page height and width information.

[C#]

// load source PDF file<o:p></o:p>

Document pdfDocument = new Document("c:/pdftest/4831+EBDRTransactionAmountDiffersForm.pdf");

//adds a blank page to pdf document

Page page = pdfDocument.Pages.Count > 0 ? pdfDocument.Pages[1] : pdfDocument.Pages.Add();

// get page height and width information

Console.WriteLine(page.GetPageRect(true).Width.ToString() + ":" + page.GetPageRect(true).Height);

// rotate page at 90 degree angle

page.Rotate = Rotation.on90;

// get page height and width information

Console.WriteLine(page.GetPageRect(true).Width.ToString() + “:”

  • page.GetPageRect(true).Height);