Resize and Rotate image Annotation added in pdf

978 Views Asked by At

Using this Link I have added signature image annotation in PDF file

But i could not find any guide for how to rotate and resize image annotation using the button added on top of annotation image like shown in the image.

What i want to do is:

  1. want to scale/resize signature image(make it small or large by adding this resize button)
  2. want to rotate signature image

Rotate using the annotation top button

For Pinch to zoom i am adding pinch gesture to PDFView but that gesture zoom in / zoom out the main pdf.tried to fix it by below code but not worked.

@objc func scale(sender : UIPinchGestureRecognizer) {
    print("----------Scale----------")
    let touchLocation = sender.location(in: pdfContainerView)
    
    guard let page = pdfContainerView.page(for: touchLocation, nearest: true)
    else {
        return
    }
    let locationOnPage = pdfContainerView.convert(touchLocation, to: page)
    switch sender.state {
    case .began:
        guard let annotation = page.annotation(at: locationOnPage) else {
            return
        }
        
        if annotation.isKind(of: ImageStampAnnotation.self) {
            currentlySelectedAnnotation = annotation
            // to disable pinch gesture for pdfview but it is not working
            pdfContainerView.minScaleFactor = pdfContainerView.scaleFactor
            pdfContainerView.maxScaleFactor = pdfContainerView.scaleFactor
        }
        
    case .changed,.ended:
        guard let annotation = currentlySelectedAnnotation else {
            return
        }
        let initialBounds = annotation.bounds
        //scale annotation
    case .cancelled:
        break
    default:
        break
    }
}

Thanks in advance!!

1

There are 1 best solutions below

0
On

I tried to implement rotation, zoom, pan on PDFview. I created a new view above the PDFAnnotation and then moved, rotated, and scaled the view as above.

about PDFAnnotation:

class PDFImageAnnotation: PDFAnnotation {
    let image: UIImage
    let originalBounds: CGRect
    /// 0 - 360
    var angle: CGFloat = 0 {
        didSet {
            // reload annotation
            shouldDisplay = true
        }
    }
    /// scale annotation
    var scale: CGFloat = 1 {
        didSet {
            // Scale on the original size
            let width = originalBounds.width * scale
            let height = originalBounds.height * scale
            // move origin
            let x = bounds.origin.x - (width - bounds.width)/2
            let y = bounds.origin.y - (height - bounds.height)/2
            print("new ---- \(CGRect(x: x, y: y, width: width, height: height))")
            // Setting the bounds will automatically re-render
            bounds = CGRect(x: x, y: y, width: width, height: height)
        }
    }
    /// move center point
    var center: CGPoint = .zero {
        didSet {
            let x = center.x - bounds.width/2.0
            let y = center.y - bounds.height/2.0
            // Setting the bounds will automatically re-render
            bounds = CGRect(origin: CGPoint(x: x, y: y), size: bounds.size)
        }
    }

    public init(bounds: CGRect, image: UIImage) {
        self.image = image
        originalBounds = bounds
        super.init(bounds: bounds, forType: .ink, withProperties: nil)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func draw(with box: PDFDisplayBox, in context: CGContext) {
        super.draw(with: box, in: context)
        print("PDFImageAnnotation bounds - \(bounds)")
        guard let page = page else {
            return
        }
        UIGraphicsPushContext(context)
        context.saveGState()
        
        // rotate annotation
        // The origin of the annotation is always at the initial position
        let translateX = bounds.width/2 + bounds.origin.x
        let translateY = bounds.height/2 + bounds.origin.y
        // The page has its own rotation Angle
        let newAngle = angle + CGFloat(page.rotation)
        context.translateBy(x: translateX, y: translateY)
        context.rotate(by: newAngle*(CGFloat.pi/180.0))
        context.translateBy(x: -translateX, y: -translateY)

        // draw image
        if let cgImage = image.cgImage {
            context.draw(cgImage, in: bounds)
        }

        context.restoreGState()
        UIGraphicsPopContext()
    }
}

use:

extension ViewController: PDFSignAnnotationViewDelegate {
    func signAnnotationView(_ view: PDFSignAnnotationView, didMove point: CGPoint) {
        guard let page = pdfContainerView.currentPage,
              let imageAnnotation = page.annotations.filter({$0.userName == view.identity}).first as? PDFImageAnnotation else {
            return
        }
        let locationOnPage = self.pdfContainerView.convert(point, to: page)
        imageAnnotation.center = locationOnPage
    }
    
    func signAnnotationView(_ view: PDFSignAnnotationView, didScale scale: CGFloat) {
        guard let page = pdfContainerView.currentPage,
              let imageAnnotation = page.annotations.filter({$0.userName == view.identity}).first as? PDFImageAnnotation else {
            return
        }
        imageAnnotation.scale = scale
    }
    
    func signAnnotationView(_ view: PDFSignAnnotationView, didRotate angle: CGFloat) {
        guard let page = pdfContainerView.currentPage,
              let imageAnnotation = page.annotations.filter({$0.userName == view.identity}).first as? PDFImageAnnotation else {
            return
        }
        print("didRotate - \(angle)")
        imageAnnotation.angle = -angle
    }
    func signAnnotationView(_ view: PDFSignAnnotationView, close identity: String) {
        guard let page = pdfContainerView.currentPage else {
            return
        }
        guard let annotation = page.annotations.filter({$0.userName == identity}).first else {
            return
        }
        page.removeAnnotation(annotation)
    }
}

TODO:

  1. Move and zoom the page without sending changes to the upper view

I refer to here: https://medium.com/@rajejones/add-a-signature-to-pdf-using-pdfkit-with-swift-7f13f7faad3e

Demo https://github.com/roMummy/PDFSignature/tree/master/PDFSignature