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.
//page.Markups[0].Flatten(page.Overlay);
//page.Markups.RemoveAt(0);

ImageShape imageShape = new ImageShape("tallcomponents-logo.png");
imageShape.KeepAspectRatio = true;
imageShape.Height = 100;
imageShape.Transform = new TranslateTransform(80, 630);
document.Pages[0].Overlay.Add(imageShape);

using(FileStream fileOut = new FileStream(@"temp.pdf", FileMode.Create, FileAccess.Write))
{
    document.Write(fileOut);
}
Process.Start("temp.pdf");
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.
'page.Markups[0].Flatten(page.Overlay);
'page.Markups.RemoveAt(0);

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

Using fileOut As New FileStream("..\..\temp.pdf", FileMode.Create, FileAccess.Write)
   document.Write(fileOut)
End Using
Download PDFKit.NET 4.0
We will send you a download link
  • This field is for validation purposes and should be left unchanged.
Why do we ask your email address?
We send tips that speed up your evaluation
We let you know about bug fixes
You can always unsubscribe with one click
We never share your address with a 3rd party
Thank you for your download

We have sent an email with a download link.