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

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

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);
}

A more efficient option is the following one liner:

page.GetSDFObj().Erase(“Annots”);

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
Am Dienstag, 6. Mai 2014 20:52:07 UTC+2 schrieb Support:

A more efficient option is the following one liner:

page.GetSDFObj().Erase(“Annots”);

On Tuesday, May 6, 2014 10:59:33 AM UTC-7, Aaron Gravesdale wrote:

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);
}

`

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.

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");