PDF blend modes

A PDF blend mode is a method to combine 2 colors (source and background) in order to get a new color. XFINIUM.PDF 5.4 bring support for executing the blend modes when rendering a PDF page to image.
The simplest blend mode is Normal, the source color covers the background color. The other blend modes are: Multiply, Screen, Overlay, Darken, Lighten, ColorDodge, ColorBurn, HardLight, SoftLight, Difference, Exclusion, Hue, Saturation, Color, Luminosity.
They are all based on a mathematical formula that combine the color components from the source color and background color in order to generate the result color.

A blend mode is activated in the page content stream by setting a new extended graphics state that includes the desired blend mode. The blend mode is active until a new blend mode is set or the graphics state is restored.

PdfPage page = document.Pages.Add();
// Create a new extended graphics state
PdfExtendedGraphicState gs1 = new PdfExtendedGraphicState();
// Set the new blend mode
gs1.BlendMode = PdfBlendMode.Multiply;
// Activate the new extended graphics state
page.Graphics.SetExtendedGraphicState(gs1);
// 
// create page content
//
// Create a new extended graphics state
PdfExtendedGraphicState gs2 = new PdfExtendedGraphicState();
// Reset the blend mode to Normal
gs2.BlendMode = PdfBlendMode.Normal;
// Activate the new extended graphics state
page.Graphics.SetExtendedGraphicState(gs2);
Dim page As PdfPage = document.Pages.Add()
' Create a new extended graphics state
Dim gs1 As New PdfExtendedGraphicState()
' Set the new blend mode
gs1.BlendMode = PdfBlendMode.Multiply
' Activate the new extended graphics state
page.Graphics.SetExtendedGraphicState(gs1)
' 
' create page content
'
' Create a new extended graphics state
Dim gs2 As New PdfExtendedGraphicState()
' Reset the blend mode to Normal
gs2.BlendMode = PdfBlendMode.Normal
' Activate the new extended graphics state
page.Graphics.SetExtendedGraphicState(gs2)

The code above can be simplified and less objects created if the page graphics state is saved before setting the blend mode and then restored when it is no longer needed.

PdfPage page = document.Pages.Add();
// Create a new extended graphics state
PdfExtendedGraphicState gs1 = new PdfExtendedGraphicState();
// Set the new blend mode
gs1.BlendMode = PdfBlendMode.Multiply;
// Save the current graphics state
page.Graphics.SaveGraphicsState();
// Activate the new extended graphics state
page.Graphics.SetExtendedGraphicState(gs1);
// 
// create page content
//
// Restore the graphics state when the blend mode is no longer needed.
page.Graphics.RestoreGraphicsState();
Dim page As PdfPage = document.Pages.Add()
' Create a new extended graphics state
Dim gs1 As New PdfExtendedGraphicState()
' Set the new blend mode
gs1.BlendMode = PdfBlendMode.Multiply
' Save the current graphics state
page.Graphics.SaveGraphicsState()
' Activate the new extended graphics state
page.Graphics.SetExtendedGraphicState(gs1)
' 
' create page content
'
' Restore the graphics state when the blend mode is no longer needed.
page.Graphics.RestoreGraphicsState()

The image below (taken from PDF specification) shows the results of applying the PDF blend modes when creating content.
Blend Modes
PDF blend modes are handled automatically when rendering a page that includes them in its content, there is nothing you need to do in your code.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: