jerjer
jerjer

Reputation: 8766

Detect orientation of every page in a PDF using ABCPDF

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.

Upvotes: 2

Views: 4236

Answers (3)

OnceUponATimeInTheWest
OnceUponATimeInTheWest

Reputation: 1222

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.

Upvotes: 2

Ross Presser
Ross Presser

Reputation: 6265

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

Upvotes: 2

Mihai Iancu
Mihai Iancu

Reputation: 1828

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.

Upvotes: 1

Related Questions