PDFKit Swapping Content of Annotation
Asked Answered
L

2

9

Can the text (i.e. contents) of a FreeText annotation be changed in PDFKit without deleting an annotation / building a new annotation?

The following snippet does not change an annotation's contents when viewing in a PDFView:

let url = Bundle.main.url(forResource: "Test", withExtension: "pdf")!
let document = PDFDocument(url: url)!

for index in 0..<document.pageCount {
    let page: PDFPage = document.page(at: index)!
    let annotations = page.annotations
    for annotation in annotations {
        annotation.contents = "[REPLACED]"
    }
}
mainPDFView.document = document

This works - but requires replacing an annotation (and thus having to copy over all the other details of the annotation):

let url = Bundle.main.url(forResource: "Test", withExtension: "pdf")!
let document = PDFDocument(url: url)!

for index in 0..<document.pageCount {
    let page: PDFPage = document.page(at: index)!
    let annotations = page.annotations
    for annotation in annotations {
        print(annotation)
        page.removeAnnotation(annotation)
        let replacement = PDFAnnotation(bounds: annotation.bounds,
                                        forType: .freeText,
                                        withProperties: nil)

        replacement.contents = "[REPLACED]"
        page.addAnnotation(replacement)
    }
}

mainPDFView.document = document

Note: adding / removing the same annotation also does not help.

Lissa answered 23/7, 2018 at 23:46 Comment(10)
developer.apple.com/documentation/pdfkit/pdfannotation/… , It has a function setContent, try that!Currier
In the developer docs the setContents you are referencing is just the getter / setter contents. My example is using that.Lissa
I am unable to reproduce your issue as the first code snippet works for me: replacing already created annotations with Replaced contents.Spasm
You probably need to check that the page's annotations are indeed all .freeText otherwise changing the contents variable won't change anything.Spasm
@PranavKasetti confirmed it is .freeText. Is the sample you are running on iOS or Mac?Lissa
@KevinSylvestre I’m running the sample on iOSSpasm
@PranavKasetti Thanks, I've uploaded a sample demonstrating the bug. Confirmed the type is 'FreeText' and assigning the contents. ksylvest.s3.amazonaws.com/pdfkit-sample.zip - does this also work for you?Lissa
@KevinSylvestre I'm seeing the issue now... I think this is because you added the pdfannotation on a mac, so its not editable in ios. It definitely looks like a bug.Spasm
Did you solve this?Greaser
Nope - did not solve this...Lissa
L
2

I suggest you iterate over the annotations array using a classic for loop and find the index of the annotation you want to modify, after that subscripting the array should modify the annotation "in place".

Here's an example which modifies all annotations:

let url = Bundle.main.url(forResource: "Test", withExtension: "pdf")!
let document = PDFDocument(url: url)!

for index1 in 0..<document.pageCount {
    let page: PDFPage = document.page(at: index)!
    let annotations = page.annotations
    for index2 in 0..<annotations.count {
        annotations[index2].contents = "[REPLACED]"
    }
}

Have a read about mutating arrays: http://kelan.io/2016/mutating-arrays-of-structs-in-swift/

Hope it helps, cheers!

LE: It's a bug actually, see this one: iOS 11 PDFKit not updating annotation position

Maybe Apple will find a way to update the PDFView on screen when you change the contents of an annotation SOON..

Localism answered 26/7, 2018 at 23:8 Comment(8)
I don't think this helps. Annotation is not a value type (Class). I also think the compiler will warn something like 'Cannot assign to 'let' constant property...' or something if that is the case...Lissa
I updated my answer, because I started to try your exact case as you we're right. Tested your and it works fine - in terms of mutating the contents property of the PDFAnnotation objects - your problem is actually that your PDFView (on screen) doesn't get refreshed and you don't see your changes, I get it now, but the changes happened.. This is a bug, plain and simple in my opinion.Significant
I don't think your other point is the issue either. I am not adding the document to a PDFView until I have changed the annotations.Lissa
What's your question again?Significant
I asked if the text of a FreeText annotation can be changed in PDFKit without deleting the annotation then building a new annotation. The original question has all the details.Lissa
And my answer basically says YES you can.Significant
Don't know how it doesn't work for you, cause it does for me.Significant
Note: it looks like this is a bug with the SDK confirmed by Pranav. Awarding the bounty to make sure it doesn't go to waste...Lissa
O
0

Have you tried calling pdfView.annotationsChanged(on: pdfPage) after updating the annotation's text?

Oolite answered 3/8, 2018 at 15:15 Comment(1)
Yeah - and this happens prior to adding the document to the page.Lissa

© 2022 - 2024 — McMap. All rights reserved.