/// <summary>Adds a new operand to the current visibility expression.</summary>
 /// <param name="layer">the layer operand to be added.</param>
 public virtual void AddOperand(PdfLayer layer)
 {
     GetPdfObject().Add(layer.GetPdfObject());
     GetPdfObject().SetModified();
 }
        /// <summary>Fills the underlying PdfDictionary object with the current layers and their settings.</summary>
        /// <remarks>
        /// Fills the underlying PdfDictionary object with the current layers and their settings.
        /// Note that it completely regenerates the dictionary, so your direct changes to the dictionary
        /// will not take any affect.
        /// </remarks>
        /// <returns>the resultant dictionary</returns>
        public virtual PdfObject FillDictionary()
        {
            PdfArray gr = new PdfArray();

            foreach (PdfLayer layer in layers)
            {
                if (layer.GetTitle() == null)
                {
                    gr.Add(layer.GetIndirectReference());
                }
            }
            GetPdfObject().Put(PdfName.OCGs, gr);
            // Save radio groups.
            PdfArray      rbGroups = null;
            PdfDictionary d        = GetPdfObject().GetAsDictionary(PdfName.D);

            if (d != null)
            {
                rbGroups = d.GetAsArray(PdfName.RBGroups);
            }
            d = new PdfDictionary();
            if (rbGroups != null)
            {
                d.Put(PdfName.RBGroups, rbGroups);
            }
            d.Put(PdfName.Name, new PdfString(CreateUniqueName(), PdfEncodings.UNICODE_BIG));
            GetPdfObject().Put(PdfName.D, d);
            IList <PdfLayer> docOrder = new List <PdfLayer>(layers);

            for (int i = 0; i < docOrder.Count; i++)
            {
                PdfLayer layer = docOrder[i];
                if (layer.GetParent() != null)
                {
                    docOrder.Remove(layer);
                    i--;
                }
            }
            PdfArray order = new PdfArray();

            foreach (Object element in docOrder)
            {
                PdfLayer layer = (PdfLayer)element;
                GetOCGOrder(order, layer);
            }
            d.Put(PdfName.Order, order);
            PdfArray off = new PdfArray();

            foreach (Object element in layers)
            {
                PdfLayer layer = (PdfLayer)element;
                if (layer.GetTitle() == null && !layer.IsOn())
                {
                    off.Add(layer.GetIndirectReference());
                }
            }
            if (off.Size() > 0)
            {
                d.Put(PdfName.OFF, off);
            }
            else
            {
                d.Remove(PdfName.OFF);
            }
            PdfArray locked = new PdfArray();

            foreach (PdfLayer layer in layers)
            {
                if (layer.GetTitle() == null && layer.IsLocked())
                {
                    locked.Add(layer.GetIndirectReference());
                }
            }
            if (locked.Size() > 0)
            {
                d.Put(PdfName.Locked, locked);
            }
            else
            {
                d.Remove(PdfName.Locked);
            }
            d.Remove(PdfName.AS);
            AddASEvent(PdfName.View, PdfName.Zoom);
            AddASEvent(PdfName.View, PdfName.View);
            AddASEvent(PdfName.Print, PdfName.Print);
            AddASEvent(PdfName.Export, PdfName.Export);
            return(GetPdfObject());
        }