PDF Web page exterior hyperlinks ( hyperlink) should not working after rendering PDF content material to UIView in Swift iOS


The pdf that has exterior weblink on the primary web page for accessing they youtube. When i open it pdf of reader or different apps it will likely be requested open the youtube.
I need to help that performance in UIView degree. I copied PDF content material to UIView efficiently and however can not see any motion, as soon as i click on the exterior net hyperlink.

Right here is the my code steps:

  1. Storing pdf file in undertaking degree (Doc.pdf)
  2. Take the primary web page and convert that web page to Knowledge. (Throughout the sensible state of affairs, i must retailer that worth as Knowledge first, it will likely be wanted reuse later, So can not use PDFPage object for processing straight.).
  3. Binding Knowledge object with CanvasView and override the CanvasView draw methodology for copying pdf knowledge to UIView. (Throughout sensible state of affairs, I can not use PDFView object, I’ve to make use of customized UIView and it will likely be wanted help different customise operation (ex: erasing, reduce, copy and so on)

Lastly, though all contents are copied efficiently, i can not see any motion, as soon as i clicked the exterior net hyperlink.

Right here is the code instance:

ViewController.Swift

import UIKit
import PDFKit

class ViewController: UIViewController {

    var pdfDocument: PDFDocument?

    override func loadView() {
        tremendous.loadView()
    }
    
    func getData(pdfPage: PDFPage) -> Knowledge {
        let cropBox = pdfPage.bounds(for: .cropBox)
        var adjustedCropBox = cropBox
        if ((pdfPage.rotation == 90) || (pdfPage.rotation == 270) || (pdfPage.rotation == -90)) {
            adjustedCropBox.dimension = CGSize(width: cropBox.peak, peak: cropBox.width)
        }
         
        let renderer = UIGraphicsPDFRenderer(bounds: adjustedCropBox)
        return renderer.pdfData { (ctx) in
            ctx.beginPage()
            ctx.cgContext.setFillColor(UIColor.white.cgColor)
            ctx.fill(adjustedCropBox)
            pdfPage.remodel(ctx.cgContext, for: .cropBox)
            
            change pdfPage.rotation {
            case 0:
                ctx.cgContext.translateBy(x: 0, y: adjustedCropBox.peak)
                ctx.cgContext.scaleBy(x: 1, y: -1)
            case 90:
                ctx.cgContext.scaleBy(x: 1, y: -1)
                ctx.cgContext.rotate(by: -.pi / 2)
            case 180, -180:
                ctx.cgContext.scaleBy(x: 1, y: -1)
                ctx.cgContext.translateBy(x: adjustedCropBox.width, y: 0)
                ctx.cgContext.rotate(by: .pi)
            case 270, -90:
                ctx.cgContext.translateBy(x: adjustedCropBox.peak, y: adjustedCropBox.width)
                ctx.cgContext.rotate(by: .pi / 2)
                ctx.cgContext.scaleBy(x: -1, y: 1)
            default:
                break
            }
            pdfPage.draw(with: .cropBox, to: ctx.cgContext)
        }
    }
    

    override func viewDidLoad() {
        tremendous.viewDidLoad()
        let fileUrl = Bundle.predominant.url(forResource: "Doc", withExtension: "pdf")
        guard let fileUrl else {
            return
        }
        pdfDocument = PDFDocument(url: fileUrl)
        let firstPage: PDFPage? = pdfDocument?.web page(at: 0)
        guard let firstPage else {
            return
        }
        let pdfData = getData(pdfPage: firstPage)
        let canvasView = CanvasView(knowledge: pdfData)
        self.view.addSubview(canvasView)
        
        NSLayoutConstraint.activate([
            canvasView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor),
            canvasView.leadingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leadingAnchor, constant: 0),
            canvasView.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor),
            canvasView.trailingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.trailingAnchor, constant: 0)
        ])
    }
}

CanvasView.Swift

import UIKit
import PDFKit

class CanvasView: UIView {
    
    var web page: PDFPage?
    
    init(knowledge: Knowledge) {
        tremendous.init(body: .zero)
        translatesAutoresizingMaskIntoConstraints = false
        web page = loadPDFPage(pdfData: knowledge)
    }
    
    init(pdfPage: PDFPage){
        tremendous.init(body: .zero)
        translatesAutoresizingMaskIntoConstraints = false
        web page = pdfPage
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been carried out")
    }

    func loadPDFPage(pdfData: Knowledge) -> PDFPage? {
        
        guard let doc = PDFDocument(knowledge: pdfData) else {
            return nil
        }
        return doc.web page(at: 0)
    }

    override func draw(_ layer: CALayer, in ctx: CGContext) {
        guard let web page else {
            return
        }
        
        if let cgPDFPage = web page.pageRef {
            let cropBoxBounds = web page.bounds(for: .cropBox)
            print(web page.displaysAnnotations)
            
            let scaleX = layer.bounds.width / cropBoxBounds.width
            let scaleY = layer.bounds.peak / cropBoxBounds.peak
            
            ctx.saveGState()

            ctx.scaleBy(x: scaleX, y: scaleY)
            ctx.translateBy(x: -cropBoxBounds.origin.x, y: cropBoxBounds.peak + cropBoxBounds.origin.y )
            ctx.scaleBy(x: 1, y: -1)
            ctx.setFillColor(UIColor.white.cgColor)
            ctx.fill(cropBoxBounds)
            ctx.drawPDFPage(cgPDFPage)
            ctx.restoreGState()
        }
    }

    override func draw(_ rect: CGRect) {}
}

Right here the undertaking

Particular Be aware:
As soon as i loaded PDFPage from url, it has two hyperlink annotations.
As soon as i created PDFPage from Knowledge in CavansView, i can not see any PDF annotations and it’s empty.
So I assume, throughout knowledge conversion course of, annotations won’t be thought-about.
though i cross unique PDFPage object straight as an alternative of Knowledge, hyperlink click on actions should not labored.

So i must somebody assist from how we ship PDFAnnotation object knowledge to Knowledge object and How we will help net hyperlink behaviour as we anticipated.

Please, assist me to resolve this case and i actually respect your suggestions and assist.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles