Flatten Markup Annotation

Flatten Markup Annotation

This article illustrate how to turn markups to images by flatten markups.

Assume we have an existing PDF document with a highlight markup. When we add an image to it, we expect the image to be drawn on top of the markup because it is added later. Like this:

Markup Flatten Expect Result 2

However, what really happens is that the image is drawn where it should be, but the markup is drawn over it in a PDF reader. Like this:

Markup Flatten Actural Result 2

The reason is that, by design, annotations are drawn on top of the static graphics.

By flattening markups, they become part of the static context (and so no longer behave as markups) and then you can draw on top of them. The following code demonstrates this:

Document document = new Document(
    new FileStream(@"markupFlattenExample.pdf", FileMode.Open, FileAccess.Read));

Page page = document.Pages[0];
// Unmark the following statements will flatten the first markup 
// and remove it.

ImageShape imageShape = new ImageShape("tallcomponents-logo.png");
imageShape.KeepAspectRatio = true;
imageShape.Height = 100;
imageShape.Transform = new TranslateTransform(80, 630);

using(FileStream fileOut = new FileStream(@"temp.pdf", FileMode.Create, FileAccess.Write))
Dim document As New Document(New FileStream("..\..\markupFlattenExample.pdf", FileMode.Open, FileAccess.Read))

Dim page As Page = document.Pages(0)
' Unmark the following statements will flatten the first markup 
' and remove it.

Dim imageShape As New ImageShape("..\..\tallcomponents-logo.png")
imageShape.KeepAspectRatio = True
imageShape.Height = 100
imageShape.Transform = New TranslateTransform(80, 630)

Using fileOut As New FileStream("..\..\temp.pdf", FileMode.Create, FileAccess.Write)
End Using