Flatten Markup Annotation

Convert PDF, Manipulate PDF
10/14/2011

Downloads

[Markup flatten - expect result 2.png]: /support/articles/130/Markup flatten - expect result 2.png "Markup flatten - expect result 2.png" [Markup flatten - actural result 2.png]: /support/articles/130/Markup flatten - actural result 2.png "Markup flatten - actural result 2.png" [Visual Studio 2010 Project]: /support/articles/130/MarkupFlattenSample.zip "Visual Studio 2010 Project" 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:

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