PdfPig icon indicating copy to clipboard operation
PdfPig copied to clipboard

Clarification Needed: Is there anyway to remove annotations?

Open rajasekarshanmugam opened this issue 3 years ago • 2 comments

Is there any way to remove all annotations or a specific annotation?

We could also use a pagebuilder and try to copy the page manually to a copy of the same PDF, post which if annotations are accessible, then it can be removed. Is there any other harder way (lookup any dictionary to process those), please let know.

rajasekarshanmugam avatar Jan 20 '22 13:01 rajasekarshanmugam

PdfPig doesn't expose an easy way to modify existing pdfs / pages. The pagebuilder lets you add to existing pages but the the lower level pdf objects aren't exposed to be modified before saving.

You could probably accomplish it by:

  • Opening the pdf and accessing the raw PdfObjects through the Document.Structure.Catalog.PageTree
  • Modifying the pdf dictionaries for the pdf pages, removing the annotations
  • Replace pdf page dicts by xref using Document.Advanced.ReplaceIndirectObject(xRefToken, newObject)
  • Use PdfDocBuilder.AddPage(pdfDoc, pageNum) to copy the pages over ... should pick up the replaced objects

Honestly PdfSharp or other modification libraries are probably a better fit for this type of thing. At one point I was looking at adjusting PdfPig to allow easier modification but would take a significant refactor.

plaisted avatar Jan 22 '22 18:01 plaisted

@plaisted Thank you for the notes. Yes, modifying page builder seems limited now. However, tried the #2, basically dropping the ANNOTS token from the dictionary after copying the page seems to help. Would check if #3 helps.

Please close this ticket if it's not a planned item since the above will help.

rajasekarshanmugam avatar Jan 23 '22 07:01 rajasekarshanmugam