PDFKIT and annotations

I have a PDF document which I want the user to sign and date. I can add the signature by searching for my Signature string "Sig" and place a graphic annotation on it. But I want to then replace or hide the "Sig" text. Also I have a date field and want to automatically add a date to the document by searching for "Date:" and then putting in a text annotation. I can't seem to find out how to annotate with text, only images. Here is what I have for the image:


let imageStamp = ImageStampAnnotation(with: signatureImage, forBounds: imageBounds, withProperties: nil)

let selections = page.document?.findString("Sig", withOptions: [.caseInsensitive])

guard let page1 = selections?.first?.pages.first else { return }

selections?.forEach({ selection in

page1.addAnnotation(imageStamp)

})


How do I do a text annotation so that my date string will appear after the word Date:?

Hi f15radar - I got around this by subclassing the ImageStampAnnotation, adding my own codable variables (ie dateStampString) that can be saved/retrieved as Key/Value pairs, and then overriding the draw method to add my image and text based on the variable. When I reload the PDF, I loops through all the annotations seeing which ones confirm to my subclass and reload them.

If you are interested in going this route, let me know and I can upload some details/code.

  • I word of warning, however...
  • It seems that PDFKit will not accept any Keys that have a data stream (ie image) created by another framework (ie PDFTron, iText from PHP, etc.) Key.Value data stream pairs can only recognized when the PDF is saved from PDFKit.