Console: Flatten annotations in PDF document.

Code samples for VintaSoft PDF .NET Plug-in. Here you can request a code sample.

Moderator: Alex

Post Reply
Alex
Site Admin
Posts: 2003
Joined: Thu Jul 10, 2008 2:21 pm

Console: Flatten annotations in PDF document.

Post by Alex »

Here is an example that shows how to flatten annotations in PDF document:

Code: Select all

static void Main(string[] args)
{
    FlattenMarkupAnnotationsInPdfDocument("PdfAnnotations.pdf", "PdfAnnotations_FlatteningMarkup.pdf");
}

public static void FlattenMarkupAnnotationsInPdfDocument(string inPdfFilename, string outPdfFilename)
{
    // open PDF document
    using (Vintasoft.Imaging.Pdf.PdfDocument document = new Vintasoft.Imaging.Pdf.PdfDocument(inPdfFilename))
    {
        // for each PDF page
        foreach (Vintasoft.Imaging.Pdf.Tree.PdfPage page in document.Pages)
        {
            // if page has annotations
            if (page.Annotations != null)
            {
                // open PDF graphics on current page
                using (Vintasoft.Imaging.Pdf.Drawing.PdfGraphics graphics = page.GetGraphics())
                {
                    // for each annotation
                    foreach (Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotation annotation in page.Annotations.ToArray())
                    {
                        // if annotation is not markup annotation AND annotation is not link-annotation
                        if (annotation.AnnotationType != Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotationType.Widget &&
                            annotation.AnnotationType != Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotationType.Link)
                        {
                            // draw annotation on PDF page
                            graphics.DrawAnnotation(annotation);
                            // remove annotation from annotation collection
                            page.Annotations.Remove(annotation);
                        }
                    }
                }
            }
        }
        // pack document to output file
        document.Pack(outPdfFilename);
    }
}
Post Reply