Patagames Software Support Forum
»
Pdfium.Net SDK
»
Common Questions
»
Removing and cleaning-up annotation resources when an annotation is removed
Rank: Newbie
Groups: Registered
Joined: 6/17/2019(UTC) Posts: 5  Location: Sydney Thanks: 1 times
|
Hi Support, Can you confirm the recommended approach to cleaning up annotation object resources when an annotation is removed from a pages Annots collection. For example: We create a PdfFreeTextAnnotation, setting various objects to it's normal appearance. Code:myAnnot.CreateEmptyAppearance(AppearanceStreamModes.Normal);
...
myAnnot.NormalAppearance.Add(...);
...
myAnnot.GenerateAppearance(AppearanceStreamModes.Normal); // This creates several object resources in the PdfIndirectList and PdfCrossReferenceTable.
page.Annots.Add(myAnnot);
Then when we want to delete the annotation, we call: Code:page.Annots.remove(myAnnot);
????? However at this point, objects that were created in the PdfIndirectList and PdfCrossReferenceTable collections are NOT removed. What is the recommended approach to remove these unused object references? We currently remove objects from the PdfIndirectList and PdfCrossReferenceTable collections whose ObjectNumber does NOT exist in the PdfRefObjectsCollection. Code:
//Get a cross-reference table and a list of indirect objects in the document
var list = PdfIndirectList.FromPdfDocument(document);
var cross = PdfCrossReferenceTable.FromPdfDocument(document);
//Receive a list of objects that have at least one reference in the document
var refObjects = PdfRefObjectsCollection.FromPdfDocument(document);
var objectsToRemove = new List<int>();
foreach( var item in list)
{
if ( refObjects.FirstOrDefault(a=>a.ObjectNumber == item.ObjectNumber) == null )
objectsToRemove.Add(item.ObjectNumber);
}
foreach (var objectNumber in objectsToRemove)
list.Remove(objectNumber);
objectsToRemove = new List<int>();
foreach (var item in cross)
{
if (refObjects.FirstOrDefault(a => a.ObjectNumber == item.ObjectNumber) == null)
objectsToRemove.Add(item.ObjectNumber);
}
foreach (var objectNumber in objectsToRemove)
cross.Remove(objectNumber);
Edited by user Tuesday, August 6, 2019 6:38:51 PM(UTC)
| Reason: Not specified
|
|
|
|
Rank: Administration
Groups: Administrators
Joined: 1/5/2016(UTC) Posts: 935
Thanks: 3 times Was thanked: 112 time(s) in 109 post(s)
|
Hi,
Try to save document with RemoveUnusedObjects flag
NoIncremenal | RemoveUnusedObjects
|
|
|
|
Rank: Newbie
Groups: Registered
Joined: 6/17/2019(UTC) Posts: 5  Location: Sydney Thanks: 1 times
|
Perfect. that worked a treat. thankyou.
|
|
|
|
Patagames Software Support Forum
»
Pdfium.Net SDK
»
Common Questions
»
Removing and cleaning-up annotation resources when an annotation is removed
Forum Jump
You can post new topics in this forum.
You can reply to topics in this forum.
You can delete your posts in this forum.
You can edit your posts in this forum.
You cannot create polls in this forum.
You can vote in polls in this forum.
Important Information:
The Patagames Software Support Forum uses cookies. By continuing to browse this site, you are agreeing to our use of cookies.
More Details
Close