How to make page landscape in aspose.pdf for .net

Hii

I need to make my document page landscape and I tried to achieve that by using below code. But it is not working. I need to know why it’s not working and how can I achieve that.

 foreach (Page page in pdfDocument.Pages)
    {
      page.PageInfo.IsLandscape = true;                            
    }

@nethmi

Please use the following code example to achieve your requirement. Hope this helps you.

Aspose.Pdf.Document doc = new Aspose.Pdf.Document(MyDir + "input.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 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.
    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 original file)
    page.CropBox = new Aspose.Pdf.Rectangle(newLLX, newLLY, newLLX + newWidth, newLLY + newHeight);

    // Setting Rotation angle of page
    page.Rotate = Rotation.None;
}
            
dataDir = dataDir + "ChangeOrientation_out.pdf";
doc.Save(dataDir);

Moreover, we suggest you please read the following article.
Change Page Orientation

1 Like

@nethmi

Further to my previous post, you can change the PDF page size to achieve your requirement as well. Please read the following article.
Change PDF Page Size with C#

1 Like

Thank you very much. got it

I got an output by trying your suggestion. please check below pdf
output.pdf (383.8 KB)
output2.pdf (383.9 KB)

why this content is in left size?? I need to fit content to whole page. How can I do it??

@nethmi

Please read the following article to resize the page content. Hope this helps you.
Resize Page Contents of Specific Pages in a PDF file

hi,
This is not working with all page size(A0,A1, etc).we need a soluton for all paper size.

Further I used pageEditor to make landscape .

                    var editor = new PdfPageEditor();
                    editor.BindPdf(pdfDocument);
                    PageSize ps = new PageSize(421, 595);

                    ps.IsLandscape = true;
                    editor.PageSize = ps;
                    editor.ApplyChanges();

But It has the same issue. Content is not fit to the page. Please find my output below. Is there any solution to avoid that problem??

output.pdf (407.6 KB)

@nethmi

You are using old version of Aspose.PDF for .NET. Please use the latest version of Aspose.PDF for .NET 22.10.