internal static String GetCOName(PdfReader reader, PRIndirectReference refi) { String name = ""; while (refi != null) { PdfObject obj = PdfReader.GetPdfObject(refi); if (obj == null || obj.Type != PdfObject.DICTIONARY) { break; } PdfDictionary dic = (PdfDictionary)obj; PdfString t = dic.GetAsString(PdfName.T); if (t != null) { name = t.ToUnicodeString() + "." + name; } refi = (PRIndirectReference)dic.Get(PdfName.PARENT); } if (name.EndsWith(".")) { name = name.Substring(0, name.Length - 1); } return(name); }
protected virtual void ReadFields() { catalog = trailer.GetAsDict(PdfName.ROOT); PdfDictionary fdf = catalog.GetAsDict(PdfName.FDF); if (fdf == null) { return; } PdfString fs = fdf.GetAsString(PdfName.F); if (fs != null) { fileSpec = fs.ToUnicodeString(); } PdfArray fld = fdf.GetAsArray(PdfName.FIELDS); if (fld == null) { return; } encoding = fdf.GetAsName(PdfName.ENCODING); PdfDictionary merged = new PdfDictionary(); merged.Put(PdfName.KIDS, fld); KidNode(merged, ""); }
protected virtual void KidNode(PdfDictionary merged, String name) { PdfArray kids = merged.GetAsArray(PdfName.KIDS); if (kids == null || kids.Size == 0) { if (name.Length > 0) { name = name.Substring(1); } fields[name] = merged; } else { merged.Remove(PdfName.KIDS); for (int k = 0; k < kids.Size; ++k) { PdfDictionary dic = new PdfDictionary(); dic.Merge(merged); PdfDictionary newDic = kids.GetAsDict(k); PdfString t = newDic.GetAsString(PdfName.T); String newName = name; if (t != null) { newName += "." + t.ToUnicodeString(); } dic.Merge(newDic); dic.Remove(PdfName.T); KidNode(dic, newName); } } }
/** * Parses the ToUnicode entry, if present, and constructs a CMap for it * @since 2.1.7 */ private void ProcessToUnicode() { PdfObject toUni = PdfReader.GetPdfObjectRelease(fontDic.Get(PdfName.TOUNICODE)); if (toUni is PRStream) { try { byte[] touni = PdfReader.GetStreamBytes((PRStream)toUni); CidLocationFromByte lb = new CidLocationFromByte(touni); toUnicodeCmap = new CMapToUnicode(); CMapParserEx.ParseCid("", toUnicodeCmap, lb); uni2cid = toUnicodeCmap.CreateReverseMapping(); } catch { toUnicodeCmap = null; uni2cid = null; // technically, we should log this or provide some sort of feedback... but sometimes the cmap will be junk, but it's still possible to get text, so we don't want to throw an exception //throw new IllegalStateException("Unable to process ToUnicode map - " + e.GetMessage(), e); } } else if (isType0) { // fake a ToUnicode for CJK Identity-H fonts try { PdfName encodingName = fontDic.GetAsName(PdfName.ENCODING); if (encodingName == null) { return; } String enc = PdfName.DecodeName(encodingName.ToString()); if (!enc.Equals("Identity-H")) { return; } PdfArray df = (PdfArray)PdfReader.GetPdfObjectRelease(fontDic.Get(PdfName.DESCENDANTFONTS)); PdfDictionary cidft = (PdfDictionary)PdfReader.GetPdfObjectRelease(df[0]); PdfDictionary cidinfo = cidft.GetAsDict(PdfName.CIDSYSTEMINFO); if (cidinfo == null) { return; } PdfString ordering = cidinfo.GetAsString(PdfName.ORDERING); if (ordering == null) { return; } CMapToUnicode touni = IdentityToUnicode.GetMapFromOrdering(ordering.ToUnicodeString()); if (touni == null) { return; } toUnicodeCmap = touni; uni2cid = toUnicodeCmap.CreateReverseMapping(); } catch (IOException e) { toUnicodeCmap = null; uni2cid = null; } } }
private PdfName GetSignatureHashKey(String signatureName) { PdfDictionary dic = acroFields.GetSignatureDictionary(signatureName); PdfString contents = dic.GetAsString(PdfName.CONTENTS); byte[] bc = contents.GetOriginalBytes(); byte[] bt = null; if (PdfName.ETSI_RFC3161.Equals(dic.GetAsName(PdfName.SUBFILTER))) { Asn1InputStream din = new Asn1InputStream(new MemoryStream(bc)); Asn1Object pkcs = din.ReadObject(); bc = pkcs.GetEncoded(); } bt = HashBytesSha1(bc); return(new PdfName(ConvertToHex(bt))); }
public void DecodeGenericDictionary(PdfDictionary merged, BaseField tx) { int flags = 0; // the text size and color PdfString da = merged.GetAsString(PdfName.DA); if (da != null) { Object[] dab = SplitDAelements(da.ToUnicodeString()); if (dab[DA_SIZE] != null) tx.FontSize = (float)dab[DA_SIZE]; if (dab[DA_COLOR] != null) tx.TextColor = (Color)dab[DA_COLOR]; if (dab[DA_FONT] != null) { PdfDictionary font = merged.GetAsDict(PdfName.DR); if (font != null) { font = font.GetAsDict(PdfName.FONT); if (font != null) { PdfObject po = font.Get(new PdfName((String)dab[DA_FONT])); if (po != null && po.Type == PdfObject.INDIRECT) { PRIndirectReference por = (PRIndirectReference)po; BaseFont bp = new DocumentFont((PRIndirectReference)po); tx.Font = bp; int porkey = por.Number; BaseFont porf = (BaseFont)extensionFonts[porkey]; if (porf == null) { if (!extensionFonts.ContainsKey(porkey)) { PdfDictionary fo = (PdfDictionary)PdfReader.GetPdfObject(po); PdfDictionary fd = fo.GetAsDict(PdfName.FONTDESCRIPTOR); if (fd != null) { PRStream prs = (PRStream)PdfReader.GetPdfObject(fd.Get(PdfName.FONTFILE2)); if (prs == null) prs = (PRStream)PdfReader.GetPdfObject(fd.Get(PdfName.FONTFILE3)); if (prs == null) { extensionFonts[porkey] = null; } else { try { porf = BaseFont.CreateFont("font.ttf", BaseFont.IDENTITY_H, true, false, PdfReader.GetStreamBytes(prs), null); } catch { } extensionFonts[porkey] = porf; } } } } if (tx is TextField) ((TextField)tx).ExtensionFont = porf; } else { BaseFont bf = (BaseFont)localFonts[dab[DA_FONT]]; if (bf == null) { String[] fn = (String[])stdFieldFontNames[dab[DA_FONT]]; if (fn != null) { try { String enc = "winansi"; if (fn.Length > 1) enc = fn[1]; bf = BaseFont.CreateFont(fn[0], enc, false); tx.Font = bf; } catch { // empty } } } else tx.Font = bf; } } } } } //rotation, border and backgound color PdfDictionary mk = merged.GetAsDict(PdfName.MK); if (mk != null) { PdfArray ar = mk.GetAsArray(PdfName.BC); Color border = GetMKColor(ar); tx.BorderColor = border; if (border != null) tx.BorderWidth = 1; ar = mk.GetAsArray(PdfName.BG); tx.BackgroundColor = GetMKColor(ar); PdfNumber rotation = mk.GetAsNumber(PdfName.R); if (rotation != null) tx.Rotation = rotation.IntValue; } //flags PdfNumber nfl = merged.GetAsNumber(PdfName.F); flags = 0; tx.Visibility = BaseField.VISIBLE_BUT_DOES_NOT_PRINT; if (nfl != null) { flags = nfl.IntValue; if ((flags & PdfFormField.FLAGS_PRINT) != 0 && (flags & PdfFormField.FLAGS_HIDDEN) != 0) tx.Visibility = BaseField.HIDDEN; else if ((flags & PdfFormField.FLAGS_PRINT) != 0 && (flags & PdfFormField.FLAGS_NOVIEW) != 0) tx.Visibility = BaseField.HIDDEN_BUT_PRINTABLE; else if ((flags & PdfFormField.FLAGS_PRINT) != 0) tx.Visibility = BaseField.VISIBLE; } //multiline nfl = merged.GetAsNumber(PdfName.FF); flags = 0; if (nfl != null) flags = nfl.IntValue; tx.Options = flags; if ((flags & PdfFormField.FF_COMB) != 0) { PdfNumber maxLen = merged.GetAsNumber(PdfName.MAXLEN); int len = 0; if (maxLen != null) len = maxLen.IntValue; tx.MaxCharacterLength = len; } //alignment nfl = merged.GetAsNumber(PdfName.Q); if (nfl != null) { if (nfl.IntValue == PdfFormField.Q_CENTER) tx.Alignment = Element.ALIGN_CENTER; else if (nfl.IntValue == PdfFormField.Q_RIGHT) tx.Alignment = Element.ALIGN_RIGHT; } //border styles PdfDictionary bs = merged.GetAsDict(PdfName.BS); if (bs != null) { PdfNumber w = bs.GetAsNumber(PdfName.W); if (w != null) tx.BorderWidth = w.FloatValue; PdfName s = bs.GetAsName(PdfName.S); if (PdfName.D.Equals(s)) tx.BorderStyle = PdfBorderDictionary.STYLE_DASHED; else if (PdfName.B.Equals(s)) tx.BorderStyle = PdfBorderDictionary.STYLE_BEVELED; else if (PdfName.I.Equals(s)) tx.BorderStyle = PdfBorderDictionary.STYLE_INSET; else if (PdfName.U.Equals(s)) tx.BorderStyle = PdfBorderDictionary.STYLE_UNDERLINE; } else { PdfArray bd = merged.GetAsArray(PdfName.BORDER); if (bd != null) { if (bd.Size >= 3) tx.BorderWidth = bd.GetAsNumber(2).FloatValue; if (bd.Size >= 4) tx.BorderStyle = PdfBorderDictionary.STYLE_DASHED; } } }
// --------------------------------------------------------------------------- public void AddKids(PdfArray dests, PdfDictionary outline) { while (outline != null) { dests.Add(outline.GetAsString(PdfName.TITLE)); dests.Add(outline.GetAsArray(PdfName.DEST)); AddKids(dests, outline.GetAsDict(PdfName.FIRST)); outline = outline.GetAsDict(PdfName.NEXT); } }
/// <summary> /// Checks if an OCG dictionary is on the list for removal. </summary> /// <param name="ocg"> a dictionary </param> /// <param name="names"> the removal list /// @return true if the dictionary should be removed </param> private bool IsToBeRemoved(PdfDictionary ocg, ICollection<string> names) { if (ocg == null) { return false; } PdfString n = ocg.GetAsString(PdfName.NAME); if (n == null) { return false; } return names.Contains(n.ToString()); }
private String RenameField(Object obj, Dictionary<String, Object> map, PdfDictionary merged) { String fieldN = null; if (obj != null) { PdfString fieldName = merged.GetAsString(PdfName.T); if (fieldName != null) { fieldN = fieldName.ToUnicodeString(); } } if (fieldN != null) { for (int i = 1; i < int.MaxValue; i++) { String tmpFieldName = String.Format("{0}_{1}", fieldN, i); if (!map.ContainsKey(tmpFieldName)) { fieldN = tmpFieldName; break; } } merged.Put(PdfName.T, new PdfString(fieldN)); } return fieldN; }