ios – Apple’s PDFKit returns incorrect offset of characters’ bounds for some PDF files


I have two PDF files in this dropbox https://www.dropbox.com/scl/fo/4huai28xj0w9pqoxqsvvd/h?rlkey=ukl51ht370w4xsmrgs2syq8so&dl=0

the SCM one page PDF is the exported page 3 of SCM PDF.

I use this code to get the bounds of first character

if let path = Bundle.main.path(forResource: "SCM one page", ofType: "pdf"),
    let pdfDocument = PDFDocument(url: URL(fileURLWithPath: path)) {
    let page = pdfDocument.page(at: 0)! // change to 2 when opening `SCM` PDF
    let bounds = page.characterBounds(at: 0)
    print(bounds)
}

For the two PDFs, even if I’m querying the same character on the same page, I get different origin offset of the bounds. Why is this?

I’m doing some custom rendering that relies on these offset, I can’t display properly when some PDFs has randomly incorrect offset.

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img