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

Notification

Icon
Error

New Topic Post Reply
Options
Go to last post Go to first unread
simon.molino@smokeball.com  
#1 Posted : Tuesday, August 6, 2019 6:35:27 PM(UTC)
Quote
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)
Quote
Paul Rayman

Rank: Administration

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

Thanks: 3 times
Was thanked: 111 time(s) in 108 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)
Quote
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.
Quick Reply Show Quick Reply
Users browsing this topic
New Topic Post Reply
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.