Blend Modes

In PSPDFKit 7.6 for iOS, we introduced the ability to set blend modes for annotations. Blend modes let you decide how different layers of color will interact with each other and what the end result will be. One of the main use cases for this is to increase the legibility of markup annotations covering text. You can check out this Wikipedia entry to learn more about blend modes and their use.

Blend Modes Supported by PSPDFKit Annotations

Name Description Example
Normal Uses only the color of the annotation and ignores the background color.
Multiply Multiplies the background content color with the annotation color.
Screen Inverse values of the color component of both the annotation and background contents are multiplied with each other.
Overlay The annotation color is laid over the background content color, and it’s either multiplied or screened based on the background content color.
Darken The background content color is replaced with the annotation color if the annotation color is darker.
Lighten The background content color is replaced with the annotation color if the annotation color is lighter.
Color Dodge The annotation color is reflected by brightening the color of the background content.
Color Burn The annotation color is reflected by darkening the color of the background content.
Soft Light Darkening or lightening of colors is based on the background content color. The effect is similar to that of shining a diffuse spotlight on background content.
Hard Light Multiplies or screens the colors, depending on the background content color. The effect is similar to that of shining a harsh spotlight on the background content.
Difference The darker color of the background content color and the annotation color is subtracted from the lighter color.
Exclusion This is the same effect as difference but with low contrast.

iOS References

Name Enumeration Case
Normal kCGBlendModeNormal
Multiple kCGBlendModeMultiply
Screen kCGBlendModeScreen
Overlay kCGBlendModeOverlay
Darken kCGBlendModeDarken
Lighten kCGBlendModeLighten
Color Dodge kCGBlendModeColorDodge
Color Burn kCGBlendModeColorBurn
Soft Light kCGBlendModeSoftLight
Hard Light kCGBlendModeHardLight
Difference kCGBlendModeDifference
Exclusion kCGBlendModeExclusion

Supported Annotations

The blendMode is a property defined on PSPDFAnnotation, and PSPDFKit supports blend modes on a few selected annotations. All types of ink, shape, markup, and free text annotations support blend modes. Stamp and image annotations support blend modes only via the use of the blend mode API. Other annotations — such as the redaction, sound, and link annotations — do not support styling with blend modes.

How to Use Blend Modes

Blend modes work like any other style property on annotations: They can be applied onto any annotation (with the exception of the annotation types mentioned in the previous section) by using the API or via the UI.

API Usage

Blend modes can easily be used on supported annotations by simply setting the blendMode property on PSPDFAnnotation:

1
2
3
let inkAnnotation = PSPDFInkAnnotation()
// Set other required properties.
inkAnnotation.blendMode = .multiply
Copy
1
2
3
PSPDFInkAnnotation *inkAnnotation = [PSPDFInkAnnotation new];
// Set other required properties.
inkAnnotation.blendMode = kCGBlendModeMultiply;

UI Usage

The blend mode can also be changed via the UI using the annotation inspector (PSPDFAnnotationStyleViewController). You can enable blend mode selection for annotation types by customizing the annotation inspector. This can be achieved by adding the PSPDFAnnotationStyleKeyBlendMode key to the propertiesForAnnotations array for the desired annotation:

Copy
1
2
3
4
5
6
7
8
9
let configuration = PSPDFConfiguration { builder in
  var annotationProperties = builder.propertiesForAnnotations

  annotationProperties[.ink] = [[AnnotationStyleKey.blendMode, AnnotationStyleKey.lineWidth, AnnotationStyleKey.color]]
  annotationProperties[.underline] = [[AnnotationStyleKey.blendMode, AnnotationStyleKey.alpha]]
  annotationProperties[.line] = [[AnnotationStyleKey.blendMode, AnnotationStyleKey.color]]

  builder.propertiesForAnnotations = annotationProperties
}
Copy
1
2
3
4
5
6
7
8
9
PSPDFConfiguration *configuration = [PSPDFConfiguration configurationWithBuilder: ^(PSPDFConfigurationBuilder *builder) {
  NSMutableDictionary *annotationProperties = [builder.propertiesForAnnotations mutableCopy];

  annotationProperties[PSPDFAnnotationStringInk] = @[@[PSPDFAnnotationStyleKeyBlendMode, PSPDFAnnotationStyleKeyLineWidth, PSPDFAnnotationStyleKeyColor]];
  annotationProperties[PSPDFAnnotationStringUnderline] = @[@[PSPDFAnnotationStyleKeyBlendMode, PSPDFAnnotationStyleKeyAlpha]];
  annotationProperties[PSPDFAnnotationStringLine] = @[@[PSPDFAnnotationStyleKeyBlendMode, PSPDFAnnotationStyleKeyColor]];

  builder.propertiesForAnnotations = annotationProperties;
}];

The blend mode style option is, by default, only configured for ink annotations. You can also remove the blend mode style option for ink annotations by modifying the propertiesForAnnotations dictionary and removing the PSPDFAnnotationStyleKeyBlendMode key from it.

PDF Storage

The annotation blend mode is not a standard key of the annotation dictionary in a PDF. Instead, PSPDFKit uses the defined blend mode when generating an annotation’s appearance stream. The blend mode is encoded into the appearance stream drawing commands, from where PSPDFKit can also later decode it. Third-party PDF viewers that support annotation appearance stream rendering should be able to honor the set blend mode. If, however, the annotation is modified in a third-party editor or viewer, the annotation stream could be regenerated by that editor. In that case, the blend mode setting might be lost.