Flatten annotations in a PDF in C#
Make review marks part of the page, so nobody can move, edit or hide them.
Short answer
Section titled “Short answer”PdfDocument.FlattenAnnotations draws the appearance of every markup annotation (notes, text
markup, free text, stamps, ink, shapes and file attachments) into its page’s content and removes
the annotation with its pop up. Links and form fields stay unless you ask for them.
PdfDocument doc = PdfDocument.Open("contract-reviewed.pdf");int flattened = doc.FlattenAnnotations(); // every page; links and form fields staydoc.Save("contract-flat.pdf");Console.WriteLine($"{flattened} annotations are now page content");To flatten a single annotation, pass it to its page’s collection:
PdfDocument other = PdfDocument.Open("contract-reviewed.pdf");PdfPage page = other.Pages[0];PdfAnnotation stamp = page.Annotations.First(a => a.Subtype == "Stamp");page.Annotations.Flatten(stamp); // only the stamp; the rest stay editableother.Save("contract-stamped.pdf");new PdfFlattenOptions { IncludeLinks = true }also flattens links: they keep their look and stop working.IncludeWidgets = truealso flattens form fields, which show their current value and leave the form.doc.Form.Flatten()flattens a whole form.- Hidden annotations, and annotations without an appearance, stay as they are.
Related
Section titled “Related”- Annotations: adding, reading and flattening annotations
- Fill and flatten a PDF form
- API: PdfDocument, PdfAnnotationCollection, PdfFlattenOptions