logo
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Options
Go to last post Go to first unread
simon.molino@smokeball.com  
#1 Posted : Tuesday, August 6, 2019 6:35:27 PM(UTC)
simon.molino@smokeball.com

Rank: Newbie

Groups: Registered
Joined: 6/17/2019(UTC)
Posts: 5
Australia
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

Paul Rayman  
#2 Posted : Tuesday, August 13, 2019 9:27:15 PM(UTC)
Paul Rayman

Rank: Administration

Groups: Administrators
Joined: 1/5/2016(UTC)
Posts: 789

Thanks: 1 times
Was thanked: 98 time(s) in 96 post(s)
Hi,

Try to save document with RemoveUnusedObjects flag

NoIncremenal | RemoveUnusedObjects
simon.molino@smokeball.com  
#3 Posted : Tuesday, August 13, 2019 10:04:02 PM(UTC)
simon.molino@smokeball.com

Rank: Newbie

Groups: Registered
Joined: 6/17/2019(UTC)
Posts: 5
Australia
Location: Sydney

Thanks: 1 times
Perfect. that worked a treat. thankyou.
Users browsing this topic
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.