Flatten Markup Annotation

Convert PDF, Manipulate PDF
10/14/2011

Downloads

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.png
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.png
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:

C# code sample

1 Document document = new Document( 2 new FileStream(@"markupFlattenExample.pdf", FileMode.Open, FileAccess.Read)); 3 4 Page page = document.Pages[0]; 5 // Unmark the following statements will flatten the first markup 6 // and remove it. 7 //page.Markups[0].Flatten(page.Overlay); 8 //page.Markups.RemoveAt(0); 9 10 ImageShape imageShape = new ImageShape("tallcomponents-logo.png"); 11 imageShape.KeepAspectRatio = true; 12 imageShape.Height = 100; 13 imageShape.Transform = new TranslateTransform(80, 630); 14 document.Pages[0].Overlay.Add(imageShape); 15 16 using(FileStream fileOut = new FileStream(@"temp.pdf", FileMode.Create, FileAccess.Write)) 17 { 18 document.Write(fileOut); 19 } 20 Process.Start("temp.pdf");

VB.NET code sample

1 Dim document As New Document(New FileStream("..\..\markupFlattenExample.pdf", FileMode.Open, FileAccess.Read)) 2 3 Dim page As Page = document.Pages(0) 4 ' Unmark the following statements will flatten the first markup 5 ' and remove it. 6 'page.Markups[0].Flatten(page.Overlay); 7 'page.Markups.RemoveAt(0); 8 9 Dim imageShape As New ImageShape("..\..\tallcomponents-logo.png") 10 imageShape.KeepAspectRatio = True 11 imageShape.Height = 100 12 imageShape.Transform = New TranslateTransform(80, 630) 13 document.Pages(0).Overlay.Add(imageShape) 14 15 Using fileOut As New FileStream("..\..\temp.pdf", FileMode.Create, FileAccess.Write) 16 document.Write(fileOut) 17 End Using