private static void MapGotoBookmark(Hashtable map, PdfObject dest, IntHashtable pages) { if (dest.IsString()) { map["Named"] = dest.ToString(); } else if (dest.IsName()) { map["Named"] = PdfName.DecodeName(dest.ToString()); } else if (dest.IsArray()) { map["Page"] = MakeBookmarkParam((PdfArray)dest, pages); //changed by ujihara 2004-06-13 } map["Action"] = "GoTo"; }
private void SerObject(PdfObject obj, int level, ByteBuffer bb) { if (level <= 0) { return; } if (obj == null) { bb.Append("$Lnull"); return; } obj = PdfReader.GetPdfObject(obj); if (obj.IsStream()) { bb.Append("$B"); SerDic((PdfDictionary)obj, level - 1, bb); if (level > 0) { md5.Initialize(); bb.Append(md5.ComputeHash(PdfReader.GetStreamBytesRaw((PRStream)obj))); } } else if (obj.IsDictionary()) { SerDic((PdfDictionary)obj, level - 1, bb); } else if (obj.IsArray()) { SerArray((PdfArray)obj, level - 1, bb); } else if (obj.IsString()) { bb.Append("$S").Append(obj.ToString()); } else if (obj.IsName()) { bb.Append("$N").Append(obj.ToString()); } else { bb.Append("$L").Append(obj.ToString()); } }
private void FillMetrics(byte[] touni, IntHashtable widths, int dw) { PdfContentParser ps = new PdfContentParser(new PRTokeniser(touni)); PdfObject ob = null; PdfObject last = null; while ((ob = ps.ReadPRObject()) != null) { if (ob.Type == PdfContentParser.COMMAND_TYPE) { if (ob.ToString().Equals("beginbfchar")) { int n = ((PdfNumber)last).IntValue; for (int k = 0; k < n; ++k) { String cid = DecodeString((PdfString)ps.ReadPRObject()); String uni = DecodeString((PdfString)ps.ReadPRObject()); if (uni.Length == 1) { int cidc = (int)cid[0]; int unic = (int)uni[uni.Length - 1]; int w = dw; if (widths.ContainsKey(cidc)) { w = widths[cidc]; } metrics[unic] = new int[] { cidc, w }; } } } else if (ob.ToString().Equals("beginbfrange")) { int n = ((PdfNumber)last).IntValue; for (int k = 0; k < n; ++k) { String cid1 = DecodeString((PdfString)ps.ReadPRObject()); String cid2 = DecodeString((PdfString)ps.ReadPRObject()); int cid1c = (int)cid1[0]; int cid2c = (int)cid2[0]; PdfObject ob2 = ps.ReadPRObject(); if (ob2.IsString()) { String uni = DecodeString((PdfString)ob2); if (uni.Length == 1) { int unic = (int)uni[uni.Length - 1]; for (; cid1c <= cid2c; cid1c++, unic++) { int w = dw; if (widths.ContainsKey(cid1c)) { w = widths[cid1c]; } metrics[unic] = new int[] { cid1c, w }; } } } else { PdfArray a = (PdfArray)ob2; for (int j = 0; j < a.Size; ++j, ++cid1c) { String uni = DecodeString(a.GetAsString(j)); if (uni.Length == 1) { int unic = (int)uni[uni.Length - 1]; int w = dw; if (widths.ContainsKey(cid1c)) { w = widths[cid1c]; } metrics[unic] = new int[] { cid1c, w }; } } } } } } else { last = ob; } } }
private static ArrayList BookmarkDepth(PdfReader reader, PdfDictionary outline, IntHashtable pages) { ArrayList list = new ArrayList(); while (outline != null) { Hashtable map = new Hashtable(); PdfString title = (PdfString)PdfReader.GetPdfObjectRelease(outline.Get(PdfName.TITLE)); map["Title"] = title.ToUnicodeString(); PdfArray color = (PdfArray)PdfReader.GetPdfObjectRelease(outline.Get(PdfName.C)); if (color != null && color.Size == 3) { ByteBuffer outp = new ByteBuffer(); outp.Append(color.GetAsNumber(0).FloatValue).Append(' '); outp.Append(color.GetAsNumber(1).FloatValue).Append(' '); outp.Append(color.GetAsNumber(2).FloatValue); map["Color"] = PdfEncodings.ConvertToString(outp.ToByteArray(), null); } PdfNumber style = (PdfNumber)PdfReader.GetPdfObjectRelease(outline.Get(PdfName.F)); if (style != null) { int f = style.IntValue; String s = ""; if ((f & 1) != 0) { s += "italic "; } if ((f & 2) != 0) { s += "bold "; } s = s.Trim(); if (s.Length != 0) { map["Style"] = s; } } PdfNumber count = (PdfNumber)PdfReader.GetPdfObjectRelease(outline.Get(PdfName.COUNT)); if (count != null && count.IntValue < 0) { map["Open"] = "false"; } try { PdfObject dest = PdfReader.GetPdfObjectRelease(outline.Get(PdfName.DEST)); if (dest != null) { MapGotoBookmark(map, dest, pages); //changed by ujihara 2004-06-13 } else { PdfDictionary action = (PdfDictionary)PdfReader.GetPdfObjectRelease(outline.Get(PdfName.A)); if (action != null) { if (PdfName.GOTO.Equals(PdfReader.GetPdfObjectRelease(action.Get(PdfName.S)))) { dest = PdfReader.GetPdfObjectRelease(action.Get(PdfName.D)); if (dest != null) { MapGotoBookmark(map, dest, pages); } } else if (PdfName.URI.Equals(PdfReader.GetPdfObjectRelease(action.Get(PdfName.S)))) { map["Action"] = "URI"; map["URI"] = ((PdfString)PdfReader.GetPdfObjectRelease(action.Get(PdfName.URI))).ToUnicodeString(); } else if (PdfName.GOTOR.Equals(PdfReader.GetPdfObjectRelease(action.Get(PdfName.S)))) { dest = PdfReader.GetPdfObjectRelease(action.Get(PdfName.D)); if (dest != null) { if (dest.IsString()) { map["Named"] = dest.ToString(); } else if (dest.IsName()) { map["NamedN"] = PdfName.DecodeName(dest.ToString()); } else if (dest.IsArray()) { PdfArray arr = (PdfArray)dest; StringBuilder s = new StringBuilder(); s.Append(arr[0].ToString()); s.Append(' ').Append(arr[1].ToString()); for (int k = 2; k < arr.Size; ++k) { s.Append(' ').Append(arr[k].ToString()); } map["Page"] = s.ToString(); } } map["Action"] = "GoToR"; PdfObject file = PdfReader.GetPdfObjectRelease(action.Get(PdfName.F)); if (file != null) { if (file.IsString()) { map["File"] = ((PdfString)file).ToUnicodeString(); } else if (file.IsDictionary()) { file = PdfReader.GetPdfObject(((PdfDictionary)file).Get(PdfName.F)); if (file.IsString()) { map["File"] = ((PdfString)file).ToUnicodeString(); } } } PdfObject newWindow = PdfReader.GetPdfObjectRelease(action.Get(PdfName.NEWWINDOW)); if (newWindow != null) { map["NewWindow"] = newWindow.ToString(); } } else if (PdfName.LAUNCH.Equals(PdfReader.GetPdfObjectRelease(action.Get(PdfName.S)))) { map["Action"] = "Launch"; PdfObject file = PdfReader.GetPdfObjectRelease(action.Get(PdfName.F)); if (file == null) { file = PdfReader.GetPdfObjectRelease(action.Get(PdfName.WIN)); } if (file != null) { if (file.IsString()) { map["File"] = ((PdfString)file).ToUnicodeString(); } else if (file.IsDictionary()) { file = PdfReader.GetPdfObjectRelease(((PdfDictionary)file).Get(PdfName.F)); if (file.IsString()) { map["File"] = ((PdfString)file).ToUnicodeString(); } } } } } } } catch { //empty on purpose } PdfDictionary first = (PdfDictionary)PdfReader.GetPdfObjectRelease(outline.Get(PdfName.FIRST)); if (first != null) { map["Kids"] = BookmarkDepth(reader, first, pages); } list.Add(map); outline = (PdfDictionary)PdfReader.GetPdfObjectRelease(outline.Get(PdfName.NEXT)); } return(list); }