How Can I Add a Transparent PNG Stamp Annotation?

We recommend using vector stamps with transparent PDFs as a source. Please refer to the Appearance Streams guide for more information about the the benefits of using transparent PDFs instead of transparent PNGs for vector stamps.

However, if your use case requires you to use transparent PNG files instead, you still use vector stamps with a UIImage as the source, by using the PSPDFFileAppearanceStreamGenerator's drawingBlock property to draw the image, like so:

Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
let stampAnnotation = PSPDFStampAnnotation()
stampAnnotation.boundingBox = CGRect(x: 180.0, y: 150.0, width: 444.0, height: 500.0)

let image = PSPDFKit.imageNamed("logo-transparent-small@2x.png")

// Create the appearance stream generator and setup its drawing block.
let appearanceStreamGenerator = PSPDFFileAppearanceStreamGenerator(fileURL: URL(fileURLWithPath: ""))
appearanceStreamGenerator.drawingBlock = { context in
    guard let cgImage = image?.cgImage else {
		return;
    }
    context.draw(cgImage, in: stampAnnotation.boundingBox)
}

// Set the annotation's appearance stream generator.
stampAnnotation.appearanceStreamGenerator = appearanceStreamGenerator
document?.add([stampAnnotation])

For more details about how to add additional custom drawings for a vector stamp annotations, take a look at AppearanceStreamGeneratorWithCustomDrawingExample from our Catalog app.