0

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

Neil MCCABE
  • 67
  • 1
  • 10

1 Answers1

1

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.

iPDFdev
  • 5,229
  • 2
  • 17
  • 18
  • I think the problem occurs before PDF display. I've added screenshots to show the error occuring in Xcode. When I display the PDFs in Preview the dimensions of the Media and CropBoxes are shown correctly as 842x595, but are flipped to 595x842 when transferred into Xcode – Neil MCCABE Jul 22 '13 at 10:42
  • Post a link to the landscape file and I'll take a look at it. After that I can give you more information. – iPDFdev Jul 22 '13 at 14:18
  • Done - I've included the link in the question. – Neil MCCABE Jul 24 '13 at 15:05
  • Preview displays incorrectly the media box and crop box. In your file both /MediaBox and /CropBox are [0 0 595.22 842] and these are the values XCode displays. The page also includes the /Rotate entry with 90 degrees (2nd situation in my answer). XCode does not display this value and this caused your confusion, while Preview seems to rotate the media box and crop box and display the adjusted boxes. – iPDFdev Jul 25 '13 at 07:18
  • Thanks-I can now see that get info in Finder and the preview inspector show different orientations. I assume then that I use CGPDFPageGetRotationAngle to interrogate whether a pdf has been flipped? – Neil MCCABE Jul 31 '13 at 12:22
  • Yes, the CGPDFPageGetRotationAngle will help you determine if the page has been flipped. – iPDFdev Jul 31 '13 at 12:50