Question

I'm doing some drawing on pdfs, and I've found what may be a problem on Xcode handling of pdf dimensions, which then become clear when CGPDFDocumentCreateWithURL is triggered.

I have two PDFs. One is A4 portrait size (612w x 792h) and it displays fine. The other one is closer to A4 landscape, 842w x 595h. While it displays in the UIScrollview I'm using, the vertical offset of page height (pageRect.size.height) in CGContextTranslateCTM caused it to move too far down the view. I NSlogged pageRect dimensions and found that the height and width had been flipped. I then checked back to the pdf's listing in supporting documents, and while clearly displaying the pdf as landscape, the dimensions were shown as 595x842, whereas the A4 portrait was correctly dimensioned as 612x792.

Has anyone else hit this problem, and is there a fix.

The link to the original landscape file is here. flipped image dimensions image dimensions OK

Was it helpful?

Solution

You can create landscape pages in 2 ways:

  1. set the width to 842 and height to 595 or
  2. set the width to 595 and height to 842 and rotate the page with 90/270 degrees.

In your situation I assume you have a rotated portrait page (2).
After you get the page width and height you also have to get the page rotation and if the rotation is 90/270 you have to swap the width/height.
You also have to consider the origin of the coordinate system. When the page is not rotated the origin is in the visual bottom left corner of the page, visually X grows from left to right, Y grows from bottom to top. When the page is rotated with 90 degrees, the origin is in the visual top left corner, visually X grows from top to bottom, Y grows from left to right.
The actual PDF coordinate system does not change when the page is rotated, what changes is how to PDF coodinate system is mapped to what you actually see on the screen, this is what 'visually' refers to.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top