Is there a way to detect the orientation of every page in a pdf file?
I am creating an application that adds watermarks(text) to pdf files.
These files can have pages portrait, landscape or a combination of both.
Using the doc.MediaBox property, I use the following logic below:
portrait = box.Height > box.Width
My problem is that, it is always returning a true value even on a landscape documents.
A Doc can have a different MediaBox on every single page. To inspect the Mediabox for page N:
doc.PageNumber = n
portrait = doc.Mediabox.Height > doc.Mediabox.Width
Landscape pages can be created in 2 ways: set a width larger than the height or set the page rotation to 90 or 270 degrees for a portrait page. Pseudo-code for determining if a page is portrait or landscape would look like this:
bool isPortrait = width < height;
if ((rotation == 90) || (rotation == -90) || (rotation == 270))
{
isPortrait = !isPortrait;
}
I'm not familiar with ABCPDF but I assume you have access to page rotation.
There are two ways that orientation can be implemented within a PDF.
The correct way is to specify a rotation angle for the page. You can get the rotation of the current page using code of the following form.
string GetRotate(Doc doc) {
return GetInheritedValue(doc, doc.Page, "/Rotate*:Num");
}
#
string GetInheritedValue(Doc doc, int id, string name) {
string val = "";
for (int i = 1; i < doc.PageCount * 2; i++) { // avoid overflow if doc corrupt
val = doc.GetInfo(id, name);
if (val.Length > 0)
break;
id = doc.GetInfoInt(id, "/Parent:Ref");
if (id == 0)
break;
}
return val;
}
However sometimes page orientation is implemented by setting the MediaBox to a wide rather than high page size. You can check the current MediaBox using the Doc.MediaBox property.