Free Support Forum - aspose.com

Detect horizontal page

Hi,

I want to detect if a PDF page is in horizontal or portrait format.
I tried:
if (page.Rect.Width > page.Rect.Height)
and:
if (page.TrimBox.Width > page.TrimBox.Height)

but for some PDF files, it doesn’t work… (I have a PDF file with only one horizontal page, and it gives me Height>Width… but for other files, it can work…)
Any help appreciated

Hi Raphael,


Thanks for your inquiry. You are following correct logic for detection of landscape mode. However, might be your problematic document has some rotation setting, so getting the wrong results. Please share your sample document here, we will look into it and will suggest you solution accordingly.

We are sorry for the inconvenience caused.

Best Regards,

Hi,

Here is the PDF horizontal doc:
https://www.sendspace.com/file/t4j1jg

and my code:

for (int pageCount = 1; pageCount
{
using (FileStream imageStream = new FileStream(path + “/” + pageCount + “.jpg”, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
{
// Create Resolution object
Resolution resolution = new Resolution(100);
var page = pdfDocument.Pages[pageCount];
// Create JPEG device with specified attributes (Width, Height, Resolution, Quality)
JpegDevice jpegDevice;
if (page.TrimBox.Width > page.TrimBox.Height)
{
jpegDevice = new JpegDevice(877, 620, resolution, 100);
}
else
{
jpegDevice = new JpegDevice(620, 877, resolution, 100);
}

// Convert a particular page and save the image to stream
jpegDevice.Process(page, imageStream);
// Close stream
imageStream.Close();
}
}

(it’s a web page we use to convert PDF into images)

Thanks in advance

Hi Raphael,


Thanks for sharing sourced document, Your document has rotation angle of 90 so you are getting the wrong dimensions. You need to consider rotation as well while checking the document dimensions. In order to take into consideration page rotation, we have Page.GetPageRect(bool considerRotation) method. If we pass consider Rotation parameter as true then it consider rotation angle and return actual rectangle dimensions. Please check following code snippet for the purpose.

Aspose.Pdf.Document
doc = new Aspose.Pdf.Document(myDir

  • “Invoices_FR_PG rental_app_M+1.pdf”);<o:p></o:p>

foreach (Aspose.Pdf.Page page in doc.Pages)

{

Aspose.Pdf.Rectangle rect = page.GetPageRect(true);

Console.WriteLine("Page {0} width is {1} and heigth is {2}, rotation: {3}, size considering rotation: widht {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,



Thanks, it works perfectly now :slight_smile:

Hi Raphael,


Thanks for your feedback. It is good to know that you have managed to accomplish your requirements.

Please keep using Aspose.Pdf and feel free to contact us for any further assistance, we will be more than happy to extend our support.

Best Regards,