iTextSharp.text.pdf.SimpleBookmark.IterateOutlines C# (CSharp) Метод

IterateOutlines() публичный статический Метод

public static IterateOutlines ( PdfWriter writer, PdfIndirectReference parent, Object>.IList kids, bool namedAsNames ) : Object[]
writer PdfWriter
parent PdfIndirectReference
kids Object>.IList
namedAsNames bool
Результат Object[]
        public static Object[] IterateOutlines(PdfWriter writer, PdfIndirectReference parent, IList<Dictionary<String, Object>> kids, bool namedAsNames)
        {
            PdfIndirectReference[] refs = new PdfIndirectReference[kids.Count];
            for (int k = 0; k < refs.Length; ++k)
                refs[k] = writer.PdfIndirectReference;
            int ptr = 0;
            int count = 0;
            foreach (Dictionary<String, Object> map in kids) {
                Object[] lower = null;
                IList<Dictionary<String, Object>> subKid = null;
                if (map.ContainsKey("Kids"))
                    subKid = (IList<Dictionary<String, Object>>)map["Kids"];
                if (subKid != null && subKid.Count > 0)
                    lower = IterateOutlines(writer, refs[ptr], subKid, namedAsNames);
                PdfDictionary outline = new PdfDictionary();
                ++count;
                if (lower != null) {
                    outline.Put(PdfName.FIRST, (PdfIndirectReference)lower[0]);
                    outline.Put(PdfName.LAST, (PdfIndirectReference)lower[1]);
                    int n = (int)lower[2];
                    if (map.ContainsKey("Open") && "false".Equals(map["Open"])) {
                        outline.Put(PdfName.COUNT, new PdfNumber(-n));
                    }
                    else {
                        outline.Put(PdfName.COUNT, new PdfNumber(n));
                        count += n;
                    }
                }
                outline.Put(PdfName.PARENT, parent);
                if (ptr > 0)
                    outline.Put(PdfName.PREV, refs[ptr - 1]);
                if (ptr < refs.Length - 1)
                    outline.Put(PdfName.NEXT, refs[ptr + 1]);
                outline.Put(PdfName.TITLE, new PdfString((String)map["Title"], PdfObject.TEXT_UNICODE));
                String color = null;
                if (map.ContainsKey("Color"))
                    color = (String)map["Color"];
                if (color != null) {
                    try {
                        PdfArray arr = new PdfArray();
                        StringTokenizer tk = new StringTokenizer(color);
                        for (int k = 0; k < 3; ++k) {
                            float f = float.Parse(tk.NextToken(), System.Globalization.NumberFormatInfo.InvariantInfo);
                            if (f < 0) f = 0;
                            if (f > 1) f = 1;
                            arr.Add(new PdfNumber(f));
                        }
                        outline.Put(PdfName.C, arr);
                    } catch {} //in case it's malformed
                }
                String style = GetVal(map, "Style");
                if (style != null) {
                    style = style.ToLower(System.Globalization.CultureInfo.InvariantCulture);
                    int bits = 0;
                    if (style.IndexOf("italic") >= 0)
                        bits |= 1;
                    if (style.IndexOf("bold") >= 0)
                        bits |= 2;
                    if (bits != 0)
                        outline.Put(PdfName.F, new PdfNumber(bits));
                }
                CreateOutlineAction(outline, map, writer, namedAsNames);
                writer.AddToBody(outline, refs[ptr]);
                ++ptr;
            }
            return new Object[]{refs[0], refs[refs.Length - 1], count};
        }

Usage Example

Пример #1
0
        /*
         * the getCatalog method is part of PdfWriter.
         * we wrap this so that we can extend it
         */
        protected override PdfDictionary GetCatalog(PdfIndirectReference rootObj)
        {
            PdfDictionary theCat = ((PdfDocument)document).GetCatalog(rootObj);

            if (acroForm != null)
            {
                theCat.Put(PdfName.ACROFORM, acroForm);
            }
            if (newBookmarks == null || newBookmarks.Count == 0)
            {
                return(theCat);
            }
            PdfDictionary        top    = new PdfDictionary();
            PdfIndirectReference topRef = PdfIndirectReference;

            Object[] kids = SimpleBookmark.IterateOutlines(this, topRef, newBookmarks, false);
            top.Put(PdfName.FIRST, (PdfIndirectReference)kids[0]);
            top.Put(PdfName.LAST, (PdfIndirectReference)kids[1]);
            top.Put(PdfName.COUNT, new PdfNumber((int)kids[2]));
            AddToBody(top, topRef);
            theCat.Put(PdfName.OUTLINES, topRef);
            return(theCat);
        }