PDF document orientation

Hi,


I have been using the PDF page height and width to determine the page orientation and auto-rotate the document to portrait. Recently I faced this issue where the page height and the rect height are not correspond to each other. PDF reader display the document in landscape.

For example on the same PDF doc:
PdfConverter.Document.Pages[1].PageInfo.Height = 842
PdfConverter.Document.Pages[1].PageInfo.Width = 595

PdfConverter.Document.Pages[1].Rect.Height = 627
PdfConverter.Document.Pages[1].Rect.Width = 789

If I were to use the page info properties, the document stay in landscape.

What is the proper way to determine the orientation in order for the me rotate the document?

Hi Praetorion,

Thanks for sharing sourced document, May be your document has some rotation angle so you are getting the wrong dimensions. Please try following code snippet to get page orientation, it also consider page rotation. Hopefully it will help you to accomplish the task. However it the issue persist then please share your sample PDF document here, so we will look into it and will guide you accordingly.

Aspose.Pdf.Document doc = new Aspose.Pdf.Document("Input.pdf");

foreach (Aspose.Pdf.Page page in doc.Pages)
{
    Aspose.Pdf.Rectangle rect = page.GetPageRect(true);

    Console.WriteLine("Page {0} width is {1} and height is {2}, rotation: {3}, size considering rotation: width {4} : height {5}", 
        page.Number, page.Rect.Width, page.Rect.Height, page.Rotate.ToString(), rect.Width, rect.Height);

    if (rect.Width > rect.Height)
        Console.WriteLine("Page no {0}: Landscape", page.Number);
    else
        Console.WriteLine("Page no {0}: Portrait", page.Number);
}

Please feel free to contact us for any further assistance.
Best Regards,

Here is the PDF.


I dont use this line Aspose.Pdf.Rectangle rect = page.GetPageRect(true);
What’s the impact if that is omitted?

BTW this is my code:

PdfConverter pdfDoc = new PdfConverter();
Aspose.Pdf.Devices.Resolution res = new Aspose.Pdf.Devices.Resolution(204, 196); //fine by default
if (this._OutputResolution == OutputResolutions.Normal)
{
res = new Resolution(204, 98);
}
pdfDoc.Resolution = res;
pdfDoc.BindPdf(input);

//check for landspace and auto rotate
for (int i = 0; i < pdfDoc.Document.Pages.Count; i++)
{
//if (pdfDoc.Document.Pages[i + 1].PageInfo.Height < pdfDoc.Document.Pages[i + 1].PageInfo.Width)
if (pdfDoc.Document.Pages[i + 1].Rect.Height < pdfDoc.Document.Pages[i + 1].Rect.Width)
{
pdfDoc.Document.Pages[i + 1].Rotate = Aspose.Pdf.Rotation.on90;
}
}
Hi Praetorion,

Please note if the rotation property of the PDF page is set then Page.Rect does not provide correct value, in that case we have to consider the rotation as well. So If you want to consider page rotation property in getting page dimension then you have to pass true argument to GetPageRect() as suggested above.

Furthermore, please note doc.PageInfo property is used for generator only. In order to get size of the page in existing PDF document, we should use Page.Rect property.

Please feel free to contact us for any further assistance.

Best Regards,