[Best Practics] How i can remove all annots from document?

253 views
Skip to first unread message

Daniel Lutz

unread,
May 6, 2014, 4:15:10 AM5/6/14
to pdfne...@googlegroups.com
Hello

i try to remove all annots from the document and save. I use the following snippet in a derived class of PdfDoc to remove the annots:

var pageCount = GetPageCount();
 
               
for (int i = 1; i <= pageCount; i++)
               
{
                   
var page = GetPage(i);
 
                   
if (!page.IsValid()) continue;
 
                   
var annotsCount = page.GetNumAnnots();
 
                   
for (int j = 0; j < annotsCount; j++)
                   
{
                       
var p = GetPage(i);
                        p
.AnnotRemove(j);
                   
}
               
}

Before i start the function i have on page 1 10 annots. After the functions the annot count of page 1 is 5. The snippet delete the half of annots correctly but not all. I try this:

 for (int j = 0; j < annotsCount; j++)
                   
{
                        page
.AnnotRemove(j);

 for (int j = 0; j < annotsCount; j++)
                   
{
var annot = page.GetAnnot(j);

if(!annot.IsValid()) continue;

                        page
.AnnotRemove(j);
                   
}
 

But the result is the same. With every document i tried the function remove only the half of annots from the page!

Is there any better practics to remove all annots from document?

Best regards

Daniel

Aaron Gravesdale

unread,
May 6, 2014, 2:48:57 PM5/6/14
to
When you delete the 0'th annotation, the indices of all remaining annotations shift downwards.  That's why only half of the annotations are deleted.  To avoid this, simply change your loop to count downwards:

for (int j = page.GetNumAnnots() - 1; j >= 0; j--)
{
   
if (page.GetAnnot(j).IsValid()) page.AnnotRemove(j);
}

Support

unread,
May 6, 2014, 2:52:07 PM5/6/14
to pdfne...@googlegroups.com

A more efficient option is the following one liner:

    page.GetSDFObj().Erase("Annots");

Daniel Lutz

unread,
May 7, 2014, 2:27:41 AM5/7/14
to pdfne...@googlegroups.com
Thank you very much for your answers! I decide to use the code below it works very well. Is the way to remove something from a list in pdf from up to down (for(var i = obj.Count; i>=0;i--)) everytime the best way?

Regards
Daniel

Support

unread,
May 7, 2014, 12:38:25 PM5/7/14
to pdfne...@googlegroups.com

> Is the way to remove something from a list in pdf from up to down (for(var i = obj.Count; i>=0;i--)) everytime the best way?


If you are dealing with 'array' data structure (i.e. Obj.IsArray() -> true), the answer is yes. For dictionaries (e,g, when you traverse pages), it does not matter.

Ryan

unread,
Apr 4, 2017, 8:34:49 PM4/4/17
to pdfne...@googlegroups.com
For other people reading this thread, this is the fastest way to remove all annotations from a document.

PageIterator itr = doc.GetPageIterator();
for (; itr.HasNext(); itr.Next()) {
  itr
.Current().GetSDFObj().Erase("Annots");
}

Since this also erases all Widgets, which are the annotation that represents Form Fields, it is a good idea to also delete all Form Fields, since the above action breaks them anyway.

doc.GetRoot().Erase("AcroForm");

Reply all
Reply to author
Forward
0 new messages