I have a requirement of changing the orientation of an existing pdf file. What is the best approach to this? Create a new pdf and read the contents of the first into the newly created pdf? I have tried setting is landscape to true which has no effect, changing the page dimensions which doesn’t move the content. Is there a predefined function for accomplishing this?
Hi Danny,
Thanks for contacting support.
In order to accomplish this requirement, you may consider using either of the following approaches. One solution can be usage of Rotation property, which changes orientation of entire page including its contents. In order to change page size you can set MediaBox of the page in the following way.
Document doc = new Document("PdfWithText.pdf");
foreach (Page page in doc.Pages)
{
Aspose.Pdf.Rectangle r = page.MediaBox;
double newHeight = r.Width;
double newWidth = r.Height;
double newLLX = r.LLX;
// We must move the page upper to compensate for changing page size.
// The lower edge of the page is at 0,0, and information is usually placed from the top of the page.
// That's why we move the lower edge up by the difference between the old and new height.
double newLLY = r.LLY + (r.Height - newHeight);
page.MediaBox = new Aspose.Pdf.Rectangle(newLLX, newLLY, newLLX + newWidth, newLLY + newHeight);
// Sometimes we also need to set CropBox if it was set in the original file.
page.CropBox = new Aspose.Pdf.Rectangle(newLLX, newLLY, newLLX + newWidth, newLLY + newHeight);
}
doc.Save("36115.pdf");
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:
Aspose.Pdf.Rectangle r = page.MediaBox;
// New height remains the same
double newHeight = r.Height;
// New width is expanded proportionally to make orientation landscape (assuming previous orientation is portrait)
double newWidth = r.Height * r.Height / r.Width;
Other option is to use PdfPageEditor facade (it can apply zoom to page contents)
Document doc = new Document("PdfWithText.pdf");
Aspose.Pdf.Rectangle r = doc.Pages[1].Rect;
PdfPageEditor ppe = new PdfPageEditor();
ppe.BindPdf("PdfWithText.pdf");
ppe.Zoom = (float)(r.Width / r.Height);
ppe.PageSize = new Aspose.Pdf.PageSize((float)r.Height, (float)r.Width);
ppe.Save("36115-1.pdf");
Thank you for the help. I used the code above and was able to get the page into landscape. I have two followup questions.
Hi Danny,
I have included 2 files that were saved from word as PDF. This is one of the many sources that we have for PDF’s. Thank you.
Hi Danny,
Thanks for sharing the sample documents.
When using following code snippet, if the input file is Portrait, its converted to Landscape orientation and if the input file is landscape, its converted to Portrait orientation. For your reference, I have also attached the output generated over my end.
Now concerning to moving the contents to center of page, you may consider adding some margin on left and trim the page margin from right side.
[C#]
Document doc = new Document("c:/pdftest/Source+Portrait+orientation.pdf");
Aspose.Pdf.Rectangle r = doc.Pages[1].Rect;
PdfPageEditor ppe = new PdfPageEditor();
ppe.BindPdf("c:/pdftest/Source+Portrait+orientation.pdf");
ppe.Zoom = (float)(r.Width / r.Height);
ppe.PageSize = new Aspose.Pdf.PageSize((float)r.Height, (float)r.Width);
ppe.Save("c:/pdftest/Source+Portrait+orientation_to_Landscape.pdf");