c# - PdfArray.remove() is not removing all annotations -
i want remove annotations pdf. i'm using code:
void removeannotations(string inputpath,string outputpath) { pdfreader pdfreader = new pdfreader(inputpath); pdfstamper pdfstamper = new pdfstamper(pdfreader, new filestream(outputpath, filemode.create)); pdfdictionary pagedict = pdfreader.getpagen(1); pdfarray annotarray = pagedict.getasarray(pdfname.annots); (int = 0; < annotarray.size; i++) { annotarray.remove(i); } pdfstamper.close(); }
when first create annotarray
, has 217 items. after for-loop of annotarray.remove()
, has 108 items, , can still see callouts , lines on pdf generated @ outputpath
. it's not clear me remaining items have in common, why skipped annotarray.remove()
. how can remove every annotation?
suppose have 10 elements in array:
array = [a, b, c, d, e, f, g, h, i, j]
you loop on array this:
for (int = 0; < array.size; i++) { array.remove(i); }
then happens step step:
step 0
remove element 0. result: [b, c, d, e, f, g, h, i, j]
step 1
remove element 1. result: [b, d, e, f, g, h, i, j]
step 2
remove element 2. result: [b, d, f, g, h, i, j]
step 3
remove element 3. result: [b, d, f, h, i, j]
step 4
remove element 4. result: [b, d, f, h, j]
step 5
remove element 5. there no element 5, there's nothing remove. result: [b, d, f, h, j]
step 6 9
remove element 6 9. there no element 6 9, there's nothing remove. result: [b, d, f, h, j]
although array counts 10 elements whereas yours counts 128, principle same: aren't removing annotations due logical error in code. type of array throw array out of bounds exception, pdfarray
doesn't because more tolerant towards faulty pdfs incomplete arrays.
you fix code this:
int n = annotarray.size; (int = 0; < n; i++) { annotarray.remove(0); }
or, found out yourself, can remove annotations @ once:
pdfreader.removeannotations();
Comments
Post a Comment