iTextSharp.text.pdf.AcroFields.DecodeGenericDictionary C# (CSharp) Метод

DecodeGenericDictionary() публичный Метод

public DecodeGenericDictionary ( PdfDictionary merged, BaseField tx ) : void
merged PdfDictionary
tx BaseField
Результат void
        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;
                }
            }
        }

Usage Example

 /// <summary>
 /// Get a Standard Type 1 (14) font from a text field
 /// </summary>
 /// <param name="acroFields">AcroFields</param>
 /// <param name="fieldName">Text field name</param>
 /// <returns>
 /// Standard Type 1 (14) font if possible, or BaseFont.HELVETICA
 /// </returns>
 public static BaseFont GetStandardFont(AcroFields acroFields, string fieldName)
 {
     var baseFont = BaseFont.CreateFont();
     try
     {
         var item = acroFields.GetFieldItem(fieldName);
         var pdfDictionary = item.GetMerged(0);
         var textField = new TextField(null, null, null);
         acroFields.DecodeGenericDictionary(pdfDictionary, textField);
         baseFont = BaseFont.CreateFont(
             // keep next line for reference: Google and StackOverflow aren't always right...
             // textField.Font.FullFontName[0][3],
             textField.Font.PostscriptFontName,
             BaseFont.WINANSI,
             false
         );
     }
     catch
     {
         // iTextSharp.text.DocumentException, but we don't care
     }
     return baseFont;
 }
All Usage Examples Of iTextSharp.text.pdf.AcroFields::DecodeGenericDictionary