1

I want to know the resolution of the page of PDF. I have tried one solution but getting the wrong resolution (612 x 792). But a correct resolution is 816x1056.

guard let provider = CGDataProvider(data: fileData as CFData) else { return }
guard let coreDocument = CGPDFDocument(provider) else { return }
guard let page = coreDocument.page(at: 0) else { return }

let size = page.getBoxRect(.mediaBox).size
Pramod Tapaniya
  • 1,228
  • 11
  • 30
  • try `let size = page.getBoxRect(.cropBox).size` – Leo Dabus Jun 02 '18 at 13:29
  • @LeoDabus getting the wrong size. – Pramod Tapaniya Jun 04 '18 at 04:58
  • A pdf does not have a resolution. Merely if there is a single page filling bitmap image (without rotation or skewing), you might derive some resolution value. A crop box of 612 x 792 by the way usually means a letter size page. – mkl Jun 04 '18 at 06:27
  • @mkl thanks, But when i view in adobe reader get resolution 816x1056. but by above code i get resolution 612 x 792. – Pramod Tapaniya Jun 04 '18 at 08:20
  • *"when i view in adobe reader get resolution 816x1056"* - where exactly in Adobe Reader do you get that resolution? That "resolution" most likely gives you the page dimensions in px while the code above gives you the page dimensions in pt, and the former numbers equal 4/3 times the latter ones. – mkl Jun 04 '18 at 09:38
  • @mkl ok thanks got a solution. Can you write in the answer? so i accept that as correct answer. – Pramod Tapaniya Jun 04 '18 at 10:21

1 Answers1

0

The "resolution" you are looking for is given in px (= 1/96 in) while the dimensions in PDFs are given in pt (= 1/72 in).

Thus, you can calculate your desired output by multiplying the dimensions from the PDF by 4/3.


Some asides...

Strictly speaking PDF pages don't have "resolutions" as such because PDF is a vector format. Of course, though, their pages have dimensions which is what you are determining.

Furthermore, you indeed should first look for the CropBox and only in its absence use the MediatBox. You can read about all the defined boxes here.

mkl
  • 90,588
  • 15
  • 125
  • 265