Page size in mixed orientation PDF

Hi,


I’m using Apose.Pdf to open PDF documents, add annotations and then save the resultant document. This works well generally but if the pages in the PDF file are of mixed orientation (some landscape and some portrait), Aspose.Pdf reports all pages as having the same height and width. This is preventing me from calculating the correct location for my annotations.

The attached PDF and the following code demonstrate the problem:

using (FileStream fs = new FileStream(inputPDF, FileMode.Open))
using (Document document = new Document(fs))
{
PageInfo p1 = document.Pages[1].PageInfo;
Console.WriteLine("Height: " + p1.Height + ", Width: " + p1.Width + ", Landscape: " + p1.IsLandscape);
PageInfo p2 = document.Pages[2].PageInfo;
Console.WriteLine("Height: " + p2.Height + ", Width: " + p2.Width + ", Landscape: " + p2.IsLandscape);
}

Is there an alternative method of getting the page size and/or orientation?

Thanks

Hi Tom,


Thanks
for using our API’s.<o:p></o:p>

I have tested the scenario and I am able to reproduce the same problem. For the sake of correction, I have logged it in our issue tracking system as PDFNEWNET-38026. We will investigate this issue in details and will keep you updated on the status of a correction.

We apologize for your inconvenience.

tomwa:
Is there an alternative method of getting the page size and/or orientation?
Hi Tom,

In order to get page dimensions, you may also consider using Aspose.Pdf.Facades.PdfFileInfo class.Please try using following code snippet.

[C#]

Aspose.Pdf.Facades.PdfFileInfo info = new PdfFileInfo();<o:p></o:p>

info.BindPdf("c:/pdftest/Mixed+Orientation.pdf");

string Orientation = (info.GetPageWidth(2)>info.GetPageHeight(2))?"Landsacepe":"Portrait";

Console.WriteLine("Height: " + info.GetPageHeight(2) + ", Width: " + info.GetPageWidth(2)+ ", Page Orientation: " + Orientation);

Thank you, Facades.PdfFileInfo worked as expected.

Hi Tom,


Thanks for the acknowledgement.

We are glad to hear that your problem is resolved. Please continue using our API and in the event of any further query, please feel free to contact.

@tomwa

The PageInfo properties Width, Height, IsLandscape used in the attached code snippet is intended for using a PDF file generator and are not filled in when reading the file.

To reliably determine the orientation of the page, use the MediaBox property of the page.
If the width there is less (or equal) than the height, the page has a portrait orientation, otherwise landscape.

Page sizes can be determined using UserUnit (if specified) and MediaBox.Width, MediaBox.Height.

using (FileStream fs = new FileStream("c:/pdftest/Mixed+Orientation.pdf", FileMode.Open))
using (Document document = new Document(fs))
{
    foreach (Page page in document.Pages)
    {
        var pageMediaBox = page.MediaBox;
        var isLandscape = pageMediaBox.Width > pageMediaBox.Height; // reliably determined.

        double userUnit = 72.0; // pixels per inch
        if (page.EnginePage.PageInformation.UserUnit != null)
        {
            userUnit = 72.0 * page.EnginePage.PageInformation.UserUnit.ToDouble();
        }

        double k = 25.4 / userUnit;
        var pageWidth = k * pageMediaBox.Width;
        var pageHeight = k * pageMediaBox.Height;

        Console.WriteLine("Height: " + pageHeight + ", Width: " + pageWidth + ", Landscape: " + isLandscape);
    }
}