Defining annotation blend modes on iOS

Nutrient iOS SDK has 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(opens in a new tab) to learn more about blend modes and their use.

Blend modes supported by Nutrient annotations

NameDescriptionExample
NormalUses only the color of the annotation and ignores the background color.
MultiplyMultiplies the background content color with the annotation color.
ScreenInverse values of the color component of both the annotation and background contents are multiplied with each other.
OverlayThe annotation color is laid over the background content color, and it’s either multiplied or screened based on the background content color.
DarkenThe background content color is replaced with the annotation color if the annotation color is darker.
LightenThe background content color is replaced with the annotation color if the annotation color is lighter.
Color dodgeThe annotation color is reflected by brightening the color of the background content.
Color burnThe annotation color is reflected by darkening the color of the background content.
Soft lightDarkening 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 lightMultiplies 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.
DifferenceThe darker color of the background content color and the annotation color is subtracted from the lighter color.
ExclusionThis is the same effect as difference but with low contrast.

iOS references

NameEnumeration case
NormalkCGBlendModeNormal(opens in a new tab)
MultiplekCGBlendModeMultiply(opens in a new tab)
ScreenkCGBlendModeScreen(opens in a new tab)
OverlaykCGBlendModeOverlay(opens in a new tab)
DarkenkCGBlendModeDarken(opens in a new tab)
LightenkCGBlendModeLighten(opens in a new tab)
Color dodgekCGBlendModeColorDodge(opens in a new tab)
Color burnkCGBlendModeColorBurn(opens in a new tab)
Soft lightkCGBlendModeSoftLight(opens in a new tab)
Hard lightkCGBlendModeHardLight(opens in a new tab)
DifferencekCGBlendModeDifference(opens in a new tab)
ExclusionkCGBlendModeExclusion(opens in a new tab)

Supported annotations

The blendMode is a property defined on Annotation, and Nutrient 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 Annotation:

let inkAnnotation = InkAnnotation()
// Set other required properties.
inkAnnotation.blendMode = .multiply

UI usage

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

let configuration = PDFConfiguration { builder in
var annotationProperties = builder.propertiesForAnnotations
annotationProperties[.ink] = [[AnnotationStyle.Key.blendMode, AnnotationStyle.Key.lineWidth, AnnotationStyle.Key.color]]
annotationProperties[.underline] = [[AnnotationStyle.Key.blendMode, AnnotationStyle.Key.alpha]]
annotationProperties[.line] = [[AnnotationStyle.Key.blendMode, AnnotationStyle.Key.color]]
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 .blendMode key from it.

PDF storage

The annotation blend mode is not a standard key of the annotation dictionary in a PDF. Instead, Nutrient 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 Nutrient 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.