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};
}