1

I'm using PDFKit framework, and want to highlight all the hyperlinks with blue color in all the pages in the PDF. How do I go ahead? I have searched but could not get enough relevant post.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Narendra
  • 31
  • 2
  • you can use `fillcolor()` function for links to color theme. follow this doc https://pdfkit.org/docs/guide.pdf – Chilll007 Feb 07 '19 at 10:57
  • how to get all the hyperlinks from PDFPage in full PDFDocument in order to color them? – Narendra Feb 07 '19 at 11:20
  • use a regex. This topic has been asked on here quite a few times. – Scriptable Feb 07 '19 at 11:21
  • @Scriptable: I want to highlight all the hyperlinks with some custom color. I already have a pdf with me. So using PDFkit how do I find all the hyperlinks from all the pages. Is there any inbuilt methods or delegates for it? If not how do i proceed step by step? – Narendra Feb 07 '19 at 11:38
  • I understood your question and suggested to look at using a regex? and other answers on SO: https://stackoverflow.com/questions/46487471/pdfkit-highlight-annotation – Scriptable Feb 07 '19 at 11:45

2 Answers2

3

If you want to extract all links from pdf, then apply a regular expression and extract all links in an array like:

    let text = pdfView.document?.string ?? ""
    let types: NSTextCheckingResult.CheckingType = .link
    do {
        let detector = try NSDataDetector(types: types.rawValue)
        let matchResult = detector.matches(in: text, options: .reportCompletion, range: NSRange(location: 0, length: text.count))

        let linksArray: [URL] = matchResult.compactMap({ $0.url })
        print("List of available links: \(linksArray)")
        
    } catch (let error) {
        print (error.localizedDescription)
    }

But, if you just want to highlight the links and click action in them then PDFKit does have a property enableDataDetectors to detect links in the PDFView. You have to just enable it.

As per apple documentation:

Turns on or off data detection. If enabled, page text will be scanned for URL's as the page becomes visible. Where URL's are found, Link annotations are created in place. These are temporary annotations and are not saved.

You can use it as:

    let pdfView = PDFView.init(frame: self.view.bounds)
    pdfView.enableDataDetectors = true

If you need to handle click of this link, then conform to PDFViewDelegate, and it will call delegate method:

    func pdfViewWillClick(onLink sender: PDFView, with url: URL) {
    
    }
Community
  • 1
  • 1
Ankit Jayaswal
  • 5,549
  • 3
  • 16
  • 36
1

I also had the same question and this is implemented functionality to accomplish the task.

When initializing the pdf document, we need to enable enableDataDetectors which will turns on the data detection, which adds annotations for detected URLs in a page.

PDFView *pdfView = [[PDFView alloc] initWithFrame: containerView.bounds];
pdfView.document = pdfDocument;
pdfView.enableDataDetectors = true;

Then using following function, we can extract the page hyperlinks. I converted those hyperlinks to PDFDestination for easy navigation. Hope this will helps to someone!

-(NSArray*)getHyperlinksDestinationsListFrom:(PDFDocument*)pdfDocument {
    NSMutableArray *list = [NSMutableArray new];
    if (pdfDocument) {
        for (int i = 0; i < pdfDocument.pageCount; i++) {
            PDFPage *page = [pdfDocument pageAtIndex:i];
            // After enabling 'enableDataDetectors', all the detectable hyperlinks will return as 'Link' annotations
            for (PDFAnnotation *anno in page.annotations) {
                if ([anno.type  isEqual: @"Link"]) {
                    PDFDestination *dest = [[PDFDestination alloc] initWithPage:page atPoint:anno.bounds.origin];
                    [list addObject:dest];
                }
            }
        }
    }
    return  list;
}

Highlight part is straight-forward once you detected the links.