Created
September 8, 2019 02:09
-
-
Save motooka/b826fe7ef534ab42f0b3b83567af8d97 to your computer and use it in GitHub Desktop.
this code reads texts of PDF file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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