internal PdfImportedLink(PdfDictionary annotation) { parameters = (Hashtable)annotation.hashMap.Clone(); try { destination = (PdfArray)parameters[PdfName.DEST]; parameters.Remove(PdfName.DEST); } catch (Exception) { throw new ArgumentException("You have to consolidate the named destinations of your reader."); } if (destination != null) { destination = new PdfArray(destination); } PdfArray rc = (PdfArray)parameters[PdfName.RECT]; parameters.Remove(PdfName.RECT); llx = rc.GetAsNumber(0).FloatValue; lly = rc.GetAsNumber(1).FloatValue; urx = rc.GetAsNumber(2).FloatValue; ury = rc.GetAsNumber(3).FloatValue; }
internal Color GetMKColor(PdfArray ar) { if (ar == null) return null; switch (ar.Size) { case 1: return new GrayColor(ar.GetAsNumber(0).FloatValue); case 3: return new Color(ExtendedColor.Normalize(ar.GetAsNumber(0).FloatValue), ExtendedColor.Normalize(ar.GetAsNumber(1).FloatValue), ExtendedColor.Normalize(ar.GetAsNumber(2).FloatValue)); case 4: return new CMYKColor(ar.GetAsNumber(0).FloatValue, ar.GetAsNumber(1).FloatValue, ar.GetAsNumber(2).FloatValue, ar.GetAsNumber(3).FloatValue); default: return null; } }
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); }
/** * Gets the button appearance. * @throws IOException on error * @throws DocumentException on error * @return the button appearance */ public PdfAppearance GetAppearance() { PdfAppearance app = GetBorderAppearance(); Rectangle box = new Rectangle(app.BoundingBox); if ((text == null || text.Length == 0) && (layout == LAYOUT_LABEL_ONLY || (image == null && template == null && iconReference == null))) { return(app); } if (layout == LAYOUT_ICON_ONLY && image == null && template == null && iconReference == null) { return(app); } BaseFont ufont = RealFont; bool borderExtra = borderStyle == PdfBorderDictionary.STYLE_BEVELED || borderStyle == PdfBorderDictionary.STYLE_INSET; float h = box.Height - borderWidth * 2; float bw2 = borderWidth; if (borderExtra) { h -= borderWidth * 2; bw2 *= 2; } float offsetX = (borderExtra ? 2 * borderWidth : borderWidth); offsetX = Math.Max(offsetX, 1); float offX = Math.Min(bw2, offsetX); tp = null; float textX = float.NaN; float textY = 0; float fsize = fontSize; float wt = box.Width - 2 * offX - 2; float ht = box.Height - 2 * offX; float adj = (iconFitToBounds ? 0 : offX + 1); int nlayout = layout; if (image == null && template == null && iconReference == null) { nlayout = LAYOUT_LABEL_ONLY; } Rectangle iconBox = null; while (true) { switch (nlayout) { case LAYOUT_LABEL_ONLY: case LAYOUT_LABEL_OVER_ICON: if (text != null && text.Length > 0 && wt > 0 && ht > 0) { fsize = CalculateFontSize(wt, ht); textX = (box.Width - ufont.GetWidthPoint(text, fsize)) / 2; textY = (box.Height - ufont.GetFontDescriptor(BaseFont.ASCENT, fsize)) / 2; } goto case LAYOUT_ICON_ONLY; case LAYOUT_ICON_ONLY: if (nlayout == LAYOUT_LABEL_OVER_ICON || nlayout == LAYOUT_ICON_ONLY) { iconBox = new Rectangle(box.Left + adj, box.Bottom + adj, box.Right - adj, box.Top - adj); } break; case LAYOUT_ICON_TOP_LABEL_BOTTOM: if (text == null || text.Length == 0 || wt <= 0 || ht <= 0) { nlayout = LAYOUT_ICON_ONLY; continue; } float nht = box.Height * 0.35f - offX; if (nht > 0) { fsize = CalculateFontSize(wt, nht); } else { fsize = 4; } textX = (box.Width - ufont.GetWidthPoint(text, fsize)) / 2; textY = offX - ufont.GetFontDescriptor(BaseFont.DESCENT, fsize); iconBox = new Rectangle(box.Left + adj, textY + fsize, box.Right - adj, box.Top - adj); break; case LAYOUT_LABEL_TOP_ICON_BOTTOM: if (text == null || text.Length == 0 || wt <= 0 || ht <= 0) { nlayout = LAYOUT_ICON_ONLY; continue; } nht = box.Height * 0.35f - offX; if (nht > 0) { fsize = CalculateFontSize(wt, nht); } else { fsize = 4; } textX = (box.Width - ufont.GetWidthPoint(text, fsize)) / 2; textY = box.Height - offX - fsize; if (textY < offX) { textY = offX; } iconBox = new Rectangle(box.Left + adj, box.Bottom + adj, box.Right - adj, textY + ufont.GetFontDescriptor(BaseFont.DESCENT, fsize)); break; case LAYOUT_LABEL_LEFT_ICON_RIGHT: if (text == null || text.Length == 0 || wt <= 0 || ht <= 0) { nlayout = LAYOUT_ICON_ONLY; continue; } float nw = box.Width * 0.35f - offX; if (nw > 0) { fsize = CalculateFontSize(wt, nw); } else { fsize = 4; } if (ufont.GetWidthPoint(text, fsize) >= wt) { nlayout = LAYOUT_LABEL_ONLY; fsize = fontSize; continue; } textX = offX + 1; textY = (box.Height - ufont.GetFontDescriptor(BaseFont.ASCENT, fsize)) / 2; iconBox = new Rectangle(textX + ufont.GetWidthPoint(text, fsize), box.Bottom + adj, box.Right - adj, box.Top - adj); break; case LAYOUT_ICON_LEFT_LABEL_RIGHT: if (text == null || text.Length == 0 || wt <= 0 || ht <= 0) { nlayout = LAYOUT_ICON_ONLY; continue; } nw = box.Width * 0.35f - offX; if (nw > 0) { fsize = CalculateFontSize(wt, nw); } else { fsize = 4; } if (ufont.GetWidthPoint(text, fsize) >= wt) { nlayout = LAYOUT_LABEL_ONLY; fsize = fontSize; continue; } textX = box.Width - ufont.GetWidthPoint(text, fsize) - offX - 1; textY = (box.Height - ufont.GetFontDescriptor(BaseFont.ASCENT, fsize)) / 2; iconBox = new Rectangle(box.Left + adj, box.Bottom + adj, textX - 1, box.Top - adj); break; } break; } if (textY < box.Bottom + offX) { textY = box.Bottom + offX; } if (iconBox != null && (iconBox.Width <= 0 || iconBox.Height <= 0)) { iconBox = null; } bool haveIcon = false; float boundingBoxWidth = 0; float boundingBoxHeight = 0; PdfArray matrix = null; if (iconBox != null) { if (image != null) { tp = new PdfTemplate(writer); tp.BoundingBox = new Rectangle(image); writer.AddDirectTemplateSimple(tp, PdfName.FRM); tp.AddImage(image, image.Width, 0, 0, image.Height, 0, 0); haveIcon = true; boundingBoxWidth = tp.BoundingBox.Width; boundingBoxHeight = tp.BoundingBox.Height; } else if (template != null) { tp = new PdfTemplate(writer); tp.BoundingBox = new Rectangle(template.Width, template.Height); writer.AddDirectTemplateSimple(tp, PdfName.FRM); tp.AddTemplate(template, template.BoundingBox.Left, template.BoundingBox.Bottom); haveIcon = true; boundingBoxWidth = tp.BoundingBox.Width; boundingBoxHeight = tp.BoundingBox.Height; } else if (iconReference != null) { PdfDictionary dic = (PdfDictionary)PdfReader.GetPdfObject(iconReference); if (dic != null) { Rectangle r2 = PdfReader.GetNormalizedRectangle(dic.GetAsArray(PdfName.BBOX)); matrix = dic.GetAsArray(PdfName.MATRIX); haveIcon = true; boundingBoxWidth = r2.Width; boundingBoxHeight = r2.Height; } } } if (haveIcon) { float icx = iconBox.Width / boundingBoxWidth; float icy = iconBox.Height / boundingBoxHeight; if (proportionalIcon) { switch (scaleIcon) { case SCALE_ICON_IS_TOO_BIG: icx = Math.Min(icx, icy); icx = Math.Min(icx, 1); break; case SCALE_ICON_IS_TOO_SMALL: icx = Math.Min(icx, icy); icx = Math.Max(icx, 1); break; case SCALE_ICON_NEVER: icx = 1; break; default: icx = Math.Min(icx, icy); break; } icy = icx; } else { switch (scaleIcon) { case SCALE_ICON_IS_TOO_BIG: icx = Math.Min(icx, 1); icy = Math.Min(icy, 1); break; case SCALE_ICON_IS_TOO_SMALL: icx = Math.Max(icx, 1); icy = Math.Max(icy, 1); break; case SCALE_ICON_NEVER: icx = icy = 1; break; default: break; } } float xpos = iconBox.Left + (iconBox.Width - (boundingBoxWidth * icx)) * iconHorizontalAdjustment; float ypos = iconBox.Bottom + (iconBox.Height - (boundingBoxHeight * icy)) * iconVerticalAdjustment; app.SaveState(); app.Rectangle(iconBox.Left, iconBox.Bottom, iconBox.Width, iconBox.Height); app.Clip(); app.NewPath(); if (tp != null) { app.AddTemplate(tp, icx, 0, 0, icy, xpos, ypos); } else { float cox = 0; float coy = 0; if (matrix != null && matrix.Size == 6) { PdfNumber nm = matrix.GetAsNumber(4); if (nm != null) { cox = nm.FloatValue; } nm = matrix.GetAsNumber(5); if (nm != null) { coy = nm.FloatValue; } } app.AddTemplateReference(iconReference, PdfName.FRM, icx, 0, 0, icy, xpos - cox * icx, ypos - coy * icy); } app.RestoreState(); } if (!float.IsNaN(textX)) { app.SaveState(); app.Rectangle(offX, offX, box.Width - 2 * offX, box.Height - 2 * offX); app.Clip(); app.NewPath(); if (textColor == null) { app.ResetGrayFill(); } else { app.SetColorFill(textColor); } app.BeginText(); app.SetFontAndSize(ufont, fsize); app.SetTextMatrix(textX, textY); app.ShowText(text); app.EndText(); app.RestoreState(); } return(app); }
private void DoType1TT() { PdfObject enc = PdfReader.GetPdfObject(font.Get(PdfName.ENCODING)); if (enc == null) { FillEncoding(null); } else { if (enc.IsName()) { FillEncoding((PdfName)enc); } else { PdfDictionary encDic = (PdfDictionary)enc; enc = PdfReader.GetPdfObject(encDic.Get(PdfName.BASEENCODING)); if (enc == null) { FillEncoding(null); } else { FillEncoding((PdfName)enc); } PdfArray diffs = encDic.GetAsArray(PdfName.DIFFERENCES); if (diffs != null) { diffmap = new IntHashtable(); int currentNumber = 0; for (int k = 0; k < diffs.Size; ++k) { PdfObject obj = diffs[k]; if (obj.IsNumber()) { currentNumber = ((PdfNumber)obj).IntValue; } else { int[] c = GlyphList.NameToUnicode(PdfName.DecodeName(((PdfName)obj).ToString())); if (c != null && c.Length > 0) { uni2byte[c[0]] = currentNumber; diffmap[c[0]] = currentNumber; } ++currentNumber; } } } } } PdfArray newWidths = font.GetAsArray(PdfName.WIDTHS); PdfNumber first = font.GetAsNumber(PdfName.FIRSTCHAR); PdfNumber last = font.GetAsNumber(PdfName.LASTCHAR); if (BuiltinFonts14.ContainsKey(fontName)) { BaseFont bf; bf = BaseFont.CreateFont(fontName, WINANSI, false); int[] e = uni2byte.ToOrderedKeys(); for (int k = 0; k < e.Length; ++k) { int n = uni2byte[e[k]]; widths[n] = bf.GetRawWidth(n, GlyphList.UnicodeToName(e[k])); } if (diffmap != null) //widths for differences must override existing ones { e = diffmap.ToOrderedKeys(); for (int k = 0; k < e.Length; ++k) { int n = diffmap[e[k]]; widths[n] = bf.GetRawWidth(n, GlyphList.UnicodeToName(e[k])); } diffmap = null; } Ascender = bf.GetFontDescriptor(ASCENT, 1000); CapHeight = bf.GetFontDescriptor(CAPHEIGHT, 1000); Descender = bf.GetFontDescriptor(DESCENT, 1000); ItalicAngle = bf.GetFontDescriptor(ITALICANGLE, 1000); llx = bf.GetFontDescriptor(BBOXLLX, 1000); lly = bf.GetFontDescriptor(BBOXLLY, 1000); urx = bf.GetFontDescriptor(BBOXURX, 1000); ury = bf.GetFontDescriptor(BBOXURY, 1000); } if (first != null && last != null && newWidths != null) { int f = first.IntValue; for (int k = 0; k < newWidths.Size; ++k) { widths[f + k] = newWidths.GetAsNumber(k).IntValue; } } FillFontDesc(font.GetAsDict(PdfName.FONTDESCRIPTOR)); }