private static ICollection<PdfIndirectReference> GetAllUsedNonFlushedOCGs(IDictionary<PdfPage, PdfPage> page2page , PdfDictionary toOcProperties) { // NOTE: the PDF is considered to be valid and therefore the presence of OСG in OCProperties.OCGs is not checked ICollection<PdfIndirectReference> fromUsedOcgs = new LinkedHashSet<PdfIndirectReference>(); // Visit the pages in parallel to find non-flush OSGs PdfPage[] fromPages = page2page.Keys.ToArray(new PdfPage[0]); PdfPage[] toPages = page2page.Values.ToArray(new PdfPage[0]); for (int i = 0; i < toPages.Length; i++) { PdfPage fromPage = fromPages[i]; PdfPage toPage = toPages[i]; // Copy OCGs from annotations IList<PdfAnnotation> toAnnotations = toPage.GetAnnotations(); IList<PdfAnnotation> fromAnnotations = fromPage.GetAnnotations(); for (int j = 0; j < toAnnotations.Count; j++) { if (!toAnnotations[j].IsFlushed()) { PdfDictionary toAnnotDict = toAnnotations[j].GetPdfObject(); PdfDictionary fromAnnotDict = fromAnnotations[j].GetPdfObject(); PdfAnnotation toAnnot = toAnnotations[j]; PdfAnnotation fromAnnot = fromAnnotations[j]; if (!toAnnotDict.IsFlushed()) { iText.Kernel.Pdf.OcgPropertiesCopier.GetUsedNonFlushedOCGsFromOcDict(toAnnotDict.GetAsDictionary(PdfName.OC ), fromAnnotDict.GetAsDictionary(PdfName.OC), fromUsedOcgs, toOcProperties); iText.Kernel.Pdf.OcgPropertiesCopier.GetUsedNonFlushedOCGsFromXObject(toAnnot.GetNormalAppearanceObject(), fromAnnot.GetNormalAppearanceObject(), fromUsedOcgs, toOcProperties); iText.Kernel.Pdf.OcgPropertiesCopier.GetUsedNonFlushedOCGsFromXObject(toAnnot.GetRolloverAppearanceObject( ), fromAnnot.GetRolloverAppearanceObject(), fromUsedOcgs, toOcProperties); iText.Kernel.Pdf.OcgPropertiesCopier.GetUsedNonFlushedOCGsFromXObject(toAnnot.GetDownAppearanceObject(), fromAnnot .GetDownAppearanceObject(), fromUsedOcgs, toOcProperties); } } } PdfDictionary toResources = toPage.GetPdfObject().GetAsDictionary(PdfName.Resources); PdfDictionary fromResources = fromPage.GetPdfObject().GetAsDictionary(PdfName.Resources); iText.Kernel.Pdf.OcgPropertiesCopier.GetUsedNonFlushedOCGsFromResources(toResources, fromResources, fromUsedOcgs , toOcProperties); } return fromUsedOcgs; }