Narendra
Narendra

Reputation: 31

How to highlight hyperlinks in PDFPage using PDFKit framework?

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.

Upvotes: 1

Views: 1966

Answers (2)

Charitha Basnayake
Charitha Basnayake

Reputation: 303

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.

Upvotes: 1

Ankit Jayaswal
Ankit Jayaswal

Reputation: 5679

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) {
    
    }

Upvotes: 3

Related Questions