Detect orientation of every page in a PDF using ABCPDF

自闭症网瘾萝莉.ら 提交于 2019-12-06 07:45:36

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!