Search code examples
iosswiftannotationspdfkit

PDFKit iOS 11: How to change the line width of Ink annotation?


I'm drawing some ink annotations on a PDF file using PDFKit. But I can't change the width of the lines. I thought that doing:

let path = UIBezierPath()
path.lineWidth = 20 // important line
path.move(to: originPoint)
path.addLine(...)
annotation.add(path)

would be enough since modifying the lineWidth of a Bezier path works when drawing in Core Graphics. But here, it does not change anything, so how to change the line width of an annotation ?


Solution

  • Use border property of PDFAnnotation to change thickness of UIBezierPath added to it.

    let p = UIBezierPath()
    p.move(to: CGPoint(x: 400, y: 200))
    p.addLine(to: CGPoint(x: 500, y: 100))
    p.addLine(to: CGPoint(x: 400, y: 0))
    p.close()
    
    let b = PDFBorder()
    b.lineWidth = 10.0
    
    let pageBounds = page.bounds(for: .artBox)
    let inkAnnotation = PDFAnnotation(bounds: pageBounds, forType: PDFAnnotationSubtype.ink, withProperties: nil)
    inkAnnotation.add(p)
    inkAnnotation.border = b
    inkAnnotation.color = .green
    
    page.addAnnotation(inkAnnotation)