PDF search on the iPhone

后端 未结 3 2095
感动是毒
感动是毒 2020-11-29 19:35

After two days trying to read annotations from a PDF using Quartz, I\'ve managed to do it and posted my code.

Now I\'d like to do the same for another frequently as

相关标签:
3条回答
  • 2020-11-29 19:54

    This isn't a simple problem to implement, but it is straightforward.

    For any given page you need to scan the page using the CGPDF scanner API. You need to register callbacks for PDF operators that affect text in the page - not just TJ/Tj, but also those that set font, affect the text drawing matrix, etc. You need to build a state machine that updates with each encountered tag+parameters. You need to examine text accounting for the current font's encoding. When you find text that you want to highlight, you'll need to examine the current text drawing matrix you've been updating to determine the drawing coordinates. Read the PDF specification (version 1.7 is downloadable from Adobe) to understand which operators you need to pay attention to.

    Font encoding is perhaps the most difficult part since there are a handful of ways encoding can be specified, and some of them are proprietary to the font. Mostly you can cheat and fall back on a subset of ANSI encoding - but this WILL break on certain PDFs having strange fonts.

    Essentially you are processing the page as if you were to render it.

    0 讨论(0)
  • 2020-11-29 20:00

    So now in iOS 11 we have PDFKit with which searching text is a breeze

    if #available(iOS 11.0, *) {
         let pdfDocument = PDFDocument(url: fileUrl)!
         let allText = pdfDocument.string /// Gets all text in pdf separated by /n
    
         let s: PDFSelection = pdfDocument.findString("Hello", withOptions: [])
         let sWithFormatting = s!.first!.attributedString
    }
    
    0 讨论(0)
  • 2020-11-29 20:03

    I created utility class in objective-c using PDF.js

    Which will allow display as well as search PDF file.

    Utility class allow search using Highlight all search result and 'case sensitive' options.

    have look PDF search in action Link

    0 讨论(0)
提交回复
热议问题