private void FillFontDesc(PdfDictionary fontDesc) { if (fontDesc == null) { return; } PdfNumber v = fontDesc.GetAsNumber(PdfName.ASCENT); if (v != null) { Ascender = v.FloatValue; } v = fontDesc.GetAsNumber(PdfName.CAPHEIGHT); if (v != null) { CapHeight = v.FloatValue; } v = fontDesc.GetAsNumber(PdfName.DESCENT); if (v != null) { Descender = v.FloatValue; } v = fontDesc.GetAsNumber(PdfName.ITALICANGLE); if (v != null) { ItalicAngle = v.FloatValue; } PdfArray bbox = fontDesc.GetAsArray(PdfName.FONTBBOX); if (bbox != null) { llx = bbox.GetAsNumber(0).FloatValue; lly = bbox.GetAsNumber(1).FloatValue; urx = bbox.GetAsNumber(2).FloatValue; ury = bbox.GetAsNumber(3).FloatValue; if (llx > urx) { float t = llx; llx = urx; urx = t; } if (lly > ury) { float t = lly; lly = ury; ury = t; } } }
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; } } }
internal PdfAppearance GetAppearance(PdfDictionary merged, String text, String fieldName) { topFirst = 0; TextField tx = null; if (fieldCache == null || !fieldCache.ContainsKey(fieldName)) { tx = new TextField(writer, null, null); tx.SetExtraMargin(extraMarginLeft, extraMarginTop); tx.BorderWidth = 0; tx.SubstitutionFonts = substitutionFonts; DecodeGenericDictionary(merged, tx); //rect PdfArray rect = merged.GetAsArray(PdfName.RECT); Rectangle box = PdfReader.GetNormalizedRectangle(rect); if (tx.Rotation == 90 || tx.Rotation == 270) box = box.Rotate(); tx.Box = box; if (fieldCache != null) fieldCache[fieldName] = tx; } else { tx = (TextField)fieldCache[fieldName]; tx.Writer = writer; } PdfName fieldType = merged.GetAsName(PdfName.FT); if (PdfName.TX.Equals(fieldType)) { tx.Text = text; return tx.GetAppearance(); } if (!PdfName.CH.Equals(fieldType)) throw new DocumentException("An appearance was requested without a variable text field."); PdfArray opt = merged.GetAsArray(PdfName.OPT); int flags = 0; PdfNumber nfl = merged.GetAsNumber(PdfName.FF); if (nfl != null) flags = nfl.IntValue; if ((flags & PdfFormField.FF_COMBO) != 0 && opt == null) { tx.Text = text; return tx.GetAppearance(); } if (opt != null) { String[] choices = new String[opt.Size]; String[] choicesExp = new String[opt.Size]; for (int k = 0; k < opt.Size; ++k) { PdfObject obj = opt[k]; if (obj.IsString()) { choices[k] = choicesExp[k] = ((PdfString)obj).ToUnicodeString(); } else { PdfArray a = (PdfArray) obj; choicesExp[k] = a.GetAsString(0).ToUnicodeString(); choices[k] = a.GetAsString(1).ToUnicodeString(); } } if ((flags & PdfFormField.FF_COMBO) != 0) { for (int k = 0; k < choices.Length; ++k) { if (text.Equals(choicesExp[k])) { text = choices[k]; break; } } tx.Text = text; return tx.GetAppearance(); } int idx = 0; for (int k = 0; k < choicesExp.Length; ++k) { if (text.Equals(choicesExp[k])) { idx = k; break; } } tx.Choices = choices; tx.ChoiceExports = choicesExp; tx.ChoiceSelection = idx; } PdfAppearance app = tx.GetListAppearance(); topFirst = tx.TopFirst; return app; }
private void FillFontDesc(PdfDictionary fontDesc) { if (fontDesc == null) return; PdfNumber v = fontDesc.GetAsNumber(PdfName.ASCENT); if (v != null) Ascender = v.FloatValue; v = fontDesc.GetAsNumber(PdfName.CAPHEIGHT); if (v != null) CapHeight = v.FloatValue; v = fontDesc.GetAsNumber(PdfName.DESCENT); if (v != null) Descender = v.FloatValue; v = fontDesc.GetAsNumber(PdfName.ITALICANGLE); if (v != null) ItalicAngle = v.FloatValue; PdfArray bbox = fontDesc.GetAsArray(PdfName.FONTBBOX); if (bbox != null) { llx = bbox.GetAsNumber(0).FloatValue; lly = bbox.GetAsNumber(1).FloatValue; urx = bbox.GetAsNumber(2).FloatValue; ury = bbox.GetAsNumber(3).FloatValue; if (llx > urx) { float t = llx; llx = urx; urx = t; } if (lly > ury) { float t = lly; lly = ury; ury = t; } } }
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)); }