Search code examples
iosswiftxcodepdfkitcgrect

How to get dimensions and load PDF Page?


I have a single PDF page in Swift Xcode iOS,

let page = self.pdfDocument!.page(at: page_index!)!

and I wish to get the dimensions of the page. When I create a breakpoint on the code above, I can see just the PDF. How can I get the width and height of my PDF page and its relative location on my screen (x, y) from top left?

In addition, I wish to load this single page (not the entire pdfDocument) into QuickLookPreviewContainer. How can I get a NSURL of this page with the code above?

Thanks in advance.


Solution

  • You can get a page dimension for PDFPage doing the following:

    let page = self.pdfDocument!.page(at: page_index!)!
    let bounds = page?.bounds(for: PDFDisplayBox.mediaBox)
    let size = bounds?.size