问题描述:

I wanted to open a PDF file and check the page orientation. Nothing else. Is there any quick way to do it using c++ or python?

I didn't want to use any external library for this.

网友答案:

Without using a preëxisting PDF handling library? Nothing "quick" nor "easy" about that.

At the very least, you must be able to read and parse the PDF Page tree, which in turn requires you to read and parse the PDF Object tree (which may be compressed and updated several times).

Scanning the Page tree, you may find pages are rotated and/or have dimensions indicating they are wider than they are high, or the other way around (a common definition of "portrait" and "landscape"). Of course, a page may have its size defined in landscape orientation but then rotated by 90 or 270 degrees.

But it's more complicated than that! Page rotation or size does not define the orientation; ultimately, it's the text on that page that defines it. Suppose a page has a portrait size and is not rotated; yet, it is perfectly possible to have all of its contents (text and graphics) rotated -- sideways to the left or right, upside down, or at any other angle.

Furthermore, for a PDF designed for a book or journal, it's not uncommon to see an upright page with its header and/or footer in the "regular" position, and have content, such as a wide table, rotated.

.. Of course, it's tremendous fun to write all of this by yourself. The official PDF Specification contains enough information to get you started; see PDF specifications for coders: Adobe or ISO?. Make sure to reserve plenty of time to read all of it.

相关阅读:
Top