Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@motooka
Created September 8, 2019 02:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save motooka/b826fe7ef534ab42f0b3b83567af8d97 to your computer and use it in GitHub Desktop.
Save motooka/b826fe7ef534ab42f0b3b83567af8d97 to your computer and use it in GitHub Desktop.
this code reads texts of PDF file
import Cocoa
import Quartz
var str = "Hello, playground"
let urlStr = "/absolute/path/to/your/pdf-file.pdf"
let url = URL(fileURLWithPath: urlStr)
// see https://developer.apple.com/documentation/pdfkit/pdfdocument
let pdf = PDFDocument(url: url)!
pdf.pageCount
pdf.string
// see https://developer.apple.com/documentation/pdfkit/pdfpage
let page = pdf.page(at: 0)!
let pageStringArray = Array(page.string!)
// get string locations
pageStringArray[0]
page.characterBounds(at: 0)
page.characterIndex(at: NSPoint(x: 85.441, y: 712.818))
// pageStringArray[110] this causes an error : index out of range (depending on your PDF test length)
page.characterBounds(at: 110) // Even if the 110th char does not exist, this returns NSRect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment