private void FlushDictRecursively(PdfDictionary dict, PageFlushingHelper.DeepFlushingContext context)
 {
     foreach (PdfName key in dict.KeySet())
     {
         PageFlushingHelper.DeepFlushingContext innerContext = null;
         if (context != null)
         {
             if (context.IsKeyInBlackList(key))
             {
                 continue;
             }
             innerContext = context.GetInnerContextFor(key);
         }
         PdfObject value = dict.Get(key, false);
         FlushObjectRecursively(value, innerContext);
     }
 }
 public DeepFlushingContext(PageFlushingHelper.DeepFlushingContext unconditionalInnerContext)
 {
     this.blackList                 = JavaCollectionsUtil.EmptySet <PdfName>();
     this.innerContexts             = null;
     this.unconditionalInnerContext = unconditionalInnerContext;
 }
        private static PageFlushingHelper.DeepFlushingContext InitPageFlushingContext()
        {
            ICollection <PdfName> ALL_KEYS_IN_BLACK_LIST = null;
            IDictionary <PdfName, PageFlushingHelper.DeepFlushingContext> NO_INNER_CONTEXTS = JavaCollectionsUtil.EmptyMap
                                                                                              <PdfName, PageFlushingHelper.DeepFlushingContext>();

            // --- action dictionary context ---
            PageFlushingHelper.DeepFlushingContext actionContext = new PageFlushingHelper.DeepFlushingContext(new LinkedHashSet
                                                                                                              <PdfName>(JavaUtil.ArraysAsList(PdfName.D, PdfName.SD, PdfName.Dp, PdfName.B, PdfName.Annotation, PdfName
                                                                                                                                              .T, PdfName.AN, PdfName.TA)), NO_INNER_CONTEXTS);
            // actions keys flushing blacklist
            PageFlushingHelper.DeepFlushingContext aaContext = new PageFlushingHelper.DeepFlushingContext(actionContext
                                                                                                          );
            // all inner entries leading to this context
            // ---
            // --- annotation dictionary context ---
            LinkedDictionary <PdfName, PageFlushingHelper.DeepFlushingContext> annotInnerContexts = new LinkedDictionary
                                                                                                    <PdfName, PageFlushingHelper.DeepFlushingContext>();

            PageFlushingHelper.DeepFlushingContext annotsContext = new PageFlushingHelper.DeepFlushingContext(new LinkedHashSet
                                                                                                              <PdfName>(JavaUtil.ArraysAsList(PdfName.P, PdfName.Popup, PdfName.Dest, PdfName.Parent, PdfName.V)), annotInnerContexts
                                                                                                              );
            // annotations flushing blacklist
            // keys that belong to form fields which can be merged with widget annotations
            annotInnerContexts.Put(PdfName.A, actionContext);
            annotInnerContexts.Put(PdfName.PA, actionContext);
            annotInnerContexts.Put(PdfName.AA, aaContext);
            // ---
            // --- separation info dictionary context ---
            PageFlushingHelper.DeepFlushingContext sepInfoContext = new PageFlushingHelper.DeepFlushingContext(new LinkedHashSet
                                                                                                               <PdfName>(JavaCollectionsUtil.SingletonList(PdfName.Pages)), NO_INNER_CONTEXTS);
            // separation info dict flushing blacklist
            // ---
            // --- bead dictionary context ---
            PageFlushingHelper.DeepFlushingContext bContext = new PageFlushingHelper.DeepFlushingContext(ALL_KEYS_IN_BLACK_LIST
                                                                                                         , NO_INNER_CONTEXTS);
            // bead dict flushing blacklist
            // ---
            // --- pres steps dictionary context ---
            LinkedDictionary <PdfName, PageFlushingHelper.DeepFlushingContext> presStepsInnerContexts = new LinkedDictionary
                                                                                                        <PdfName, PageFlushingHelper.DeepFlushingContext>();

            PageFlushingHelper.DeepFlushingContext presStepsContext = new PageFlushingHelper.DeepFlushingContext(new LinkedHashSet
                                                                                                                 <PdfName>(JavaCollectionsUtil.SingletonList(PdfName.Prev)), presStepsInnerContexts);
            // pres step dict flushing blacklist
            presStepsInnerContexts.Put(PdfName.NA, actionContext);
            presStepsInnerContexts.Put(PdfName.PA, actionContext);
            // ---
            // --- page dictionary context ---
            LinkedDictionary <PdfName, PageFlushingHelper.DeepFlushingContext> pageInnerContexts = new LinkedDictionary
                                                                                                   <PdfName, PageFlushingHelper.DeepFlushingContext>();

            PageFlushingHelper.DeepFlushingContext pageContext = new PageFlushingHelper.DeepFlushingContext(new LinkedHashSet
                                                                                                            <PdfName>(JavaUtil.ArraysAsList(PdfName.Parent, PdfName.DPart)), pageInnerContexts);
            pageInnerContexts.Put(PdfName.Annots, annotsContext);
            pageInnerContexts.Put(PdfName.B, bContext);
            pageInnerContexts.Put(PdfName.AA, aaContext);
            pageInnerContexts.Put(PdfName.SeparationInfo, sepInfoContext);
            pageInnerContexts.Put(PdfName.PresSteps, presStepsContext);
            // ---
            return(pageContext);
        }
        private void FlushObjectRecursively(PdfObject obj, PageFlushingHelper.DeepFlushingContext context)
        {
            if (obj == null)
            {
                return;
            }
            bool avoidReleaseForIndirectObjInstance = false;

            if (obj.IsIndirectReference())
            {
                PdfIndirectReference indRef = (PdfIndirectReference)obj;
                if (indRef.refersTo == null || indRef.CheckState(PdfObject.FLUSHED))
                {
                    return;
                }
                obj = indRef.GetRefersTo();
            }
            else
            {
                if (obj.IsFlushed())
                {
                    return;
                }
                else
                {
                    if (release && obj.IsIndirect())
                    {
                        // We should avoid the case when object is going to be released but is stored in containing object
                        // not as indirect reference. This can happen when containing object is somehow modified.
                        // Generally containing objects should not contain released read-only object instance.
                        System.Diagnostics.Debug.Assert(obj.IsReleaseForbidden() || obj.GetIndirectReference() == null);
                        avoidReleaseForIndirectObjInstance = true;
                    }
                }
            }
            if (pdfDoc.IsDocumentFont(obj.GetIndirectReference()) || layersRefs.Contains(obj.GetIndirectReference()))
            {
                return;
            }
            if (obj.IsDictionary() || obj.IsStream())
            {
                if (!currNestedObjParents.Add(obj))
                {
                    return;
                }
                FlushDictRecursively((PdfDictionary)obj, context);
                currNestedObjParents.Remove(obj);
            }
            else
            {
                if (obj.IsArray())
                {
                    if (!currNestedObjParents.Add(obj))
                    {
                        return;
                    }
                    PdfArray array = (PdfArray)obj;
                    for (int i = 0; i < array.Size(); ++i)
                    {
                        FlushObjectRecursively(array.Get(i, false), context);
                    }
                    currNestedObjParents.Remove(obj);
                }
            }
            if (!avoidReleaseForIndirectObjInstance)
            {
                FlushOrRelease(obj);
            }
        }
 static PageFlushingHelper()
 {
     pageContext = InitPageFlushingContext();
 }