Console: Flatten annotations in PDF document.
Posted: Tue Jun 01, 2021 11:02 am
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);
}
}