Aspose.PDF : Not able to change the page to landscape mode

Approach 1 Change Page SIze.pdf (132.3 KB)

Original PDF.pdf (159.8 KB)

Approach 2 Change Dimension.pdf (142.7 KB)

Approach 3 Rotate PDF.pdf (142.6 KB)

Hi
We are trying to make the page landscape orientation using aspose.PDF .Net. But we are getting issues doing that. The content of the page is not scaling as per the page size. We tried following 3 approaches.

Please find attached original PDF and all the approach result PDF. We are trying to change page no 2 to landscape.

Approach 1: Change Page Size
foreach (Aspose.Pdf.Page page in pdfDoc.Pages)
{
if (page.Number == 2)
{
page.SetPageSize(842.4, 597.6);
page.PageInfo.Margin = new MarginInfo(10, 54, 10, 72);
}
else
{
page.SetPageSize(597.6, 842.4);
}

Approach 2 : Change Media Dimension
foreach (Aspose.Pdf.Page page in pdfDoc.Pages)
{
if (page.Number == 2)
{
// Get the current page dimensions
Aspose.Pdf.Rectangle r = page.MediaBox;
double newHeight = r.Width;
double newWidth = r.Height;
double newLLX = r.LLX;
double newLLY = r.LLY + (r.Height - newHeight);

    // Set the new dimensions    
    page.MediaBox = new Aspose.Pdf.Rectangle(newLLX, newLLY, newLLX + newWidth, newLLY + newHeight);
    page.CropBox = new Aspose.Pdf.Rectangle(newLLX, newLLY, newLLX + newWidth, newLLY + newHeight);

    // Adjust the content to fit the new dimensions
    PdfPageEditor editor = new PdfPageEditor();
    editor.BindPdf(pdfDoc);
    editor.PageSize = new PageSize((float)newWidth, (float)newHeight);
    editor.ApplyChanges();
}
else
{
    page.SetPageSize(597.6, 842.4);
}

}

Approach 3 : Rotate to landscape
foreach (Aspose.Pdf.Page page in pdfDoc.Pages)
{
if (page.Number == 2)
{
// Rotate the page to landscape
page.Rotate = Rotation.on90;
// Adjust the content to fit the new dimensions
Aspose.Pdf.Rectangle r = page.MediaBox;
double newHeight = r.Width;
double newWidth = r.Height;
PdfPageEditor editor = new PdfPageEditor();
editor.BindPdf(pdfDoc);
editor.PageSize = new PageSize((float)newHeight, (float)newWidth);
editor.Zoom = 1.0f; // Adjust zoom if necessary
editor.VerticalAlignmentType = VerticalAlignment.Center;
editor.HorizontalAlignment = HorizontalAlignment.Center;
editor.ApplyChanges();
}
else
{
page.SetPageSize(597.6, 842.4);
}
}

@NiksD2024

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFNET-57986

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.