GetAsArray() public method

public GetAsArray ( PdfName key ) : PdfArray
key PdfName
return PdfArray
Ejemplo n.º 1
0
        private void FillDiffMap(PdfDictionary encDic, CMapToUnicode toUnicode)
        {
            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;
                            byte2uni[currentNumber] = c[0];
                            diffmap[c[0]]           = currentNumber;
                        }
                        else
                        {
                            if (toUnicode == null)
                            {
                                toUnicode = ProcessToUnicode();
                                if (toUnicode == null)
                                {
                                    toUnicode = new CMapToUnicode();
                                }
                            }
                            string unicode = toUnicode.Lookup(new byte[] { (byte)currentNumber }, 0, 1);
                            if ((unicode != null) && (unicode.Length == 1))
                            {
                                this.uni2byte[unicode[0]]    = currentNumber;
                                this.byte2uni[currentNumber] = unicode[0];
                                this.diffmap[unicode[0]]     = currentNumber;
                            }
                        }
                        ++currentNumber;
                    }
                }
            }
        }
Ejemplo n.º 2
0
        void Manipulate(ip.PdfDictionary element)
        {
            if (element == null)
            {
                return;
            }

            if (ip.PdfName.FIGURE.Equals(element.Get(ip.PdfName.S)))
            {
                element.Put(ip.PdfName.ALT, new ip.PdfString("Image"));
            }

            var kids = element.GetAsArray(ip.PdfName.K);

            if (kids == null)
            {
                return;
            }
            for (var i = 0; i < kids.Size; i++)
            {
                Manipulate(kids.GetAsDict(i));
            }
        }
Ejemplo n.º 3
0
        virtual protected internal void UpdateCalculationOrder(PdfReader reader)
        {
            PdfDictionary catalog = reader.Catalog;
            PdfDictionary acro    = catalog.GetAsDict(PdfName.ACROFORM);

            if (acro == null)
            {
                return;
            }
            PdfArray co = acro.GetAsArray(PdfName.CO);

            if (co == null || co.Size == 0)
            {
                return;
            }
            AcroFields af = reader.AcroFields;

            for (int k = 0; k < co.Size; ++k)
            {
                PdfObject obj = co[k];
                if (obj == null || !obj.IsIndirect())
                {
                    continue;
                }
                String name = GetCOName(reader, (PRIndirectReference)obj);
                if (af.GetFieldItem(name) == null)
                {
                    continue;
                }
                name = "." + name;
                if (calculationOrder.Contains(name))
                {
                    continue;
                }
                calculationOrder.Add(name);
            }
        }
 private void UpdateByteRange(PdfPKCS7 pkcs7, PdfDictionary v)
 {
     PdfArray b = v.GetAsArray(PdfName.BYTERANGE);
     RandomAccessFileOrArray rf = reader.SafeFile;
     try {
         rf.ReOpen();
         byte[] buf = new byte[8192];
         for (int k = 0; k < b.Size; ++k) {
             int start = b.GetAsNumber(k).IntValue;
             int length = b.GetAsNumber(++k).IntValue;
             rf.Seek(start);
             while (length > 0) {
                 int rd = rf.Read(buf, 0, Math.Min(length, buf.Length));
                 if (rd <= 0)
                     break;
                 length -= rd;
                 pkcs7.Update(buf, 0, rd);
             }
         }
     }
     finally {
         try{rf.Close();}catch{}
     }
 }
 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;
 }
Ejemplo n.º 6
0
 /**
  * Adds keys to the signature dictionary that define
  * the field permissions.
  * This method is only used for signatures that lock fields.
  * @param crypto the signature dictionary
  */
 private void AddFieldMDP(PdfDictionary crypto, PdfDictionary fieldLock) {
     PdfDictionary reference = new PdfDictionary();
     PdfDictionary transformParams = new PdfDictionary();
     transformParams.Merge(fieldLock);
     transformParams.Put(PdfName.TYPE, PdfName.TRANSFORMPARAMS);
     transformParams.Put(PdfName.V, new PdfName("1.2"));
     reference.Put(PdfName.TRANSFORMMETHOD, PdfName.FIELDMDP);
     reference.Put(PdfName.TYPE, PdfName.SIGREF);
     reference.Put(PdfName.TRANSFORMPARAMS, transformParams);
     reference.Put(new PdfName("DigestValue"), new PdfString("aa"));
     PdfArray loc = new PdfArray();
     loc.Add(new PdfNumber(0));
     loc.Add(new PdfNumber(0));
     reference.Put(new PdfName("DigestLocation"), loc);
     reference.Put(new PdfName("DigestMethod"), new PdfName("MD5"));
     reference.Put(PdfName.DATA, writer.reader.Trailer.Get(PdfName.ROOT));
     PdfArray types = crypto.GetAsArray(PdfName.REFERENCE);
     if (types == null)
 	    types = new PdfArray();
     types.Add(reference);
     crypto.Put(PdfName.REFERENCE, types);
 }
Ejemplo n.º 7
0
 internal static PdfDictionary GetKDict(PdfDictionary obj) {
     PdfDictionary k = obj.GetAsDict(PdfName.K);
     if (k != null) {
         if (PdfName.OBJR.Equals(k.GetAsName(PdfName.TYPE))) {
             return k;
         }
     } else {
         PdfArray k1 = obj.GetAsArray(PdfName.K);
         if (k1 == null)
             return null;
         for (int i = 0; i < k1.Size; i++) {
             k = k1.GetAsDict(i);
             if (k != null) {
                 if (PdfName.OBJR.Equals(k.GetAsName(PdfName.TYPE))) {
                     return k;
                 }
             }
         }
     }
     return null;
 }
Ejemplo n.º 8
0
        protected PdfArray BranchForm(Hashtable level, PdfIndirectReference parent, String fname)
        {
            PdfArray arr = new PdfArray();

            foreach (DictionaryEntry entry in level)
            {
                String name = (String)entry.Key;
                Object obj  = entry.Value;
                PdfIndirectReference ind = PdfIndirectReference;
                PdfDictionary        dic = new PdfDictionary();
                if (parent != null)
                {
                    dic.Put(PdfName.PARENT, parent);
                }
                dic.Put(PdfName.T, new PdfString(name, PdfObject.TEXT_UNICODE));
                String fname2 = fname + "." + name;
                int    coidx  = calculationOrder.IndexOf(fname2);
                if (coidx >= 0)
                {
                    calculationOrderRefs[coidx] = ind;
                }
                if (obj is Hashtable)
                {
                    dic.Put(PdfName.KIDS, BranchForm((Hashtable)obj, ind, fname2));
                    arr.Add(ind);
                    AddToBody(dic, ind);
                }
                else
                {
                    ArrayList list = (ArrayList)obj;
                    dic.MergeDifferent((PdfDictionary)list[0]);
                    if (list.Count == 3)
                    {
                        dic.MergeDifferent((PdfDictionary)list[2]);
                        int           page    = (int)list[1];
                        PdfDictionary pageDic = (PdfDictionary)pageDics[page - 1];
                        PdfArray      annots  = pageDic.GetAsArray(PdfName.ANNOTS);
                        if (annots == null)
                        {
                            annots = new PdfArray();
                            pageDic.Put(PdfName.ANNOTS, annots);
                        }
                        PdfNumber nn = (PdfNumber)dic.Get(iTextTag);
                        dic.Remove(iTextTag);
                        AdjustTabOrder(annots, ind, nn);
                    }
                    else
                    {
                        PdfArray kids = new PdfArray();
                        for (int k = 1; k < list.Count; k += 2)
                        {
                            int           page    = (int)list[k];
                            PdfDictionary pageDic = (PdfDictionary)pageDics[page - 1];
                            PdfArray      annots  = pageDic.GetAsArray(PdfName.ANNOTS);
                            if (annots == null)
                            {
                                annots = new PdfArray();
                                pageDic.Put(PdfName.ANNOTS, annots);
                            }
                            PdfDictionary widget = new PdfDictionary();
                            widget.Merge((PdfDictionary)list[k + 1]);
                            widget.Put(PdfName.PARENT, ind);
                            PdfNumber nn = (PdfNumber)widget.Get(iTextTag);
                            widget.Remove(iTextTag);
                            PdfIndirectReference wref = AddToBody(widget).IndirectReference;
                            AdjustTabOrder(annots, wref, nn);
                            kids.Add(wref);
                            Propagate(widget, null, false);
                        }
                        dic.Put(PdfName.KIDS, kids);
                    }
                    arr.Add(ind);
                    AddToBody(dic, ind);
                    Propagate(dic, null, false);
                }
            }
            return(arr);
        }
Ejemplo n.º 9
0
        /**
         * Extracts locations from the concrete annotation.
         * Note: annotation can consist not only of one area specified by the RECT entry, but also of multiple areas specified
         * by the QuadPoints entry in the annotation dictionary.
         */
        private IList<PdfCleanUpLocation> ExtractLocationsFromRedactAnnot(int page, int annotIndex, PdfDictionary annotDict) {
            IList<PdfCleanUpLocation> locations = new List<PdfCleanUpLocation>();
            List<Rectangle> markedRectangles = new List<Rectangle>();
            PdfArray quadPoints = annotDict.GetAsArray(PdfName.QUADPOINTS);

            if (quadPoints.Size != 0) {
                markedRectangles.AddRange(TranslateQuadPointsToRectangles(quadPoints));
            } else {
                PdfArray annotRect = annotDict.GetAsArray(PdfName.RECT);
                markedRectangles.Add(new Rectangle(annotRect.GetAsNumber(0).FloatValue,
                                                   annotRect.GetAsNumber(1).FloatValue,
                                                   annotRect.GetAsNumber(2).FloatValue,
                                                   annotRect.GetAsNumber(3).FloatValue));
            }

            clippingRects.Add(annotIndex, markedRectangles);

            BaseColor cleanUpColor = null;
            PdfArray ic = annotDict.GetAsArray(PdfName.IC);

            if (ic != null) {
                cleanUpColor = new BaseColor(
                    ic.GetAsNumber(0).FloatValue,
                    ic.GetAsNumber(1).FloatValue,
                    ic.GetAsNumber(2).FloatValue
                );
            }


            PdfStream ro = annotDict.GetAsStream(PdfName.RO);

            if (ro != null) {
                cleanUpColor = null;
            }

            foreach (Rectangle rect in markedRectangles) {
                locations.Add(new PdfCleanUpLocation(page, rect, cleanUpColor));
            }

            return locations;
        }
Ejemplo n.º 10
0
        /**
         * Extracts locations from the redact annotations contained in the document and applied to the given page.
         */
        private IList<PdfCleanUpLocation> ExtractLocationsFromRedactAnnots(int page, PdfDictionary pageDict) {
            List<PdfCleanUpLocation> locations = new List<PdfCleanUpLocation>();

            if (pageDict.Contains(PdfName.ANNOTS)) {
                PdfArray annotsArray = pageDict.GetAsArray(PdfName.ANNOTS);

                for (int i = 0; i < annotsArray.Size; ++i) {
                    PdfIndirectReference annotIndirRef = annotsArray.GetAsIndirectObject(i);
                    PdfDictionary annotDict = annotsArray.GetAsDict(i);
                    PdfName annotSubtype = annotDict.GetAsName(PdfName.SUBTYPE);

                    if (annotSubtype.Equals(PdfName.REDACT)) {
                        SaveRedactAnnotIndirRef(page, annotIndirRef.ToString());
                        locations.AddRange(ExtractLocationsFromRedactAnnot(page, i, annotDict));
                    }
                }
            }

            return locations;
        }
        /**
         * @param colorSpaceName the name of the color space. If null, a bi-tonal (black and white) color space is assumed.
         * @return the components per pixel for the specified color space
         */
        private static int GetComponentsPerPixel(PdfName colorSpaceName, PdfDictionary colorSpaceDic)
        {
            if (colorSpaceName == null)
                return 1;
            if (colorSpaceName.Equals(PdfName.DEVICEGRAY))
                return 1;
            if (colorSpaceName.Equals(PdfName.DEVICERGB))
                return 3;
            if (colorSpaceName.Equals(PdfName.DEVICECMYK))
                return 4;

            if (colorSpaceDic != null){
                PdfArray colorSpace = colorSpaceDic.GetAsArray(colorSpaceName);
                if (colorSpace != null){
                    if (PdfName.INDEXED.Equals(colorSpace.GetAsName(0))){
                        return 1;
                    }
                }
            }

            throw new ArgumentException("Unexpected color space " + colorSpaceName);
        }
Ejemplo n.º 12
0
 private void FillDiffMap(PdfDictionary encDic, CMapToUnicode toUnicode) {
     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;
                     byte2uni[currentNumber] = c[0];
                     diffmap[c[0]] = currentNumber;
                 }
                 else {
                     if (toUnicode == null) {
                         toUnicode = ProcessToUnicode();
                         if (toUnicode == null) {
                             toUnicode = new CMapToUnicode();
                         }
                     }
                     string unicode = toUnicode.Lookup(new byte[]{(byte) currentNumber}, 0, 1);
                     if ((unicode != null) && (unicode.Length == 1)) {
                         this.uni2byte[unicode[0]] = currentNumber;
                         this.byte2uni[currentNumber] = unicode[0];
                         this.diffmap[unicode[0]] = currentNumber;
                     }
                 }
                 ++currentNumber;
             }
         }
     }
 }
Ejemplo n.º 13
0
 /// <summary>
 /// Gets an array from a dictionary and checks if it contains references to OCGs that need to be removed </summary>
 /// <param name="dict">	the dictionary </param>
 /// <param name="name">	the name of an array entry </param>
 /// <param name="ocgs">	the removal list </param>
 private void RemoveOCGsFromArray(PdfDictionary dict, PdfName name, ICollection<string> ocgs)
 {
     if (dict == null)
     {
         return;
     }
     PdfArray array = dict.GetAsArray(name);
     if (array == null)
     {
         return;
     }
     RemoveOCGsFromArray(array, ocgs);
 }
Ejemplo n.º 14
0
 /// <summary>
 /// Removes annotations from a page dictionary </summary>
 /// <param name="page">	a page dictionary </param>
 /// <param name="ocgs">	a set of names of OCG layers </param>
 private void RemoveAnnots(PdfDictionary page, ICollection<string> ocgs)
 {
     PdfArray annots = page.GetAsArray(PdfName.ANNOTS);
     if (annots == null)
     {
         return;
     }
     IList<int?> remove = new List<int?>();
     for (int i = annots.Size; i > 0; )
     {
         PdfDictionary annot = annots.GetAsDict(--i);
         if (IsToBeRemoved(annot.GetAsDict(PdfName.OC), ocgs))
         {
             remove.Add(i);
         }
         else
         {
             RemoveOCGsFromArray(annot.GetAsDict(PdfName.A), PdfName.STATE, ocgs);
         }
     }
     foreach (int i in remove)
     {
         annots.Remove(i);
     }
 }
Ejemplo n.º 15
0
 private PdfArray BranchForm(Dictionary<String, Object> level, PdfIndirectReference parent, String fname) {
     PdfArray arr = new PdfArray();
     foreach (KeyValuePair<String, Object> entry in level) {
         String name = entry.Key;
         Object obj = entry.Value;
         PdfIndirectReference ind = PdfIndirectReference;
         PdfDictionary dic = new PdfDictionary();
         if (parent != null)
             dic.Put(PdfName.PARENT, parent);
         dic.Put(PdfName.T, new PdfString(name, PdfObject.TEXT_UNICODE));
         String fname2 = fname + "." + name;
         int coidx = calculationOrder.IndexOf(fname2);
         if (coidx >= 0)
             calculationOrderRefs[coidx] = ind;
         if (obj is Dictionary<String, Object>) {
             dic.Put(PdfName.KIDS, BranchForm((Dictionary<String, Object>) obj, ind, fname2));
             arr.Add(ind);
             AddToBody(dic, ind, true);
         } else {
             List<Object> list = (List<Object>)obj;
             dic.MergeDifferent((PdfDictionary) list[0]);
             if (list.Count == 3) {
                 dic.MergeDifferent((PdfDictionary)list[2]);
                 int page = (int)list[1];
                 PdfArray annots = importedPages[page - 1].mergedFields;
                 PdfNumber nn = (PdfNumber)dic.Get(iTextTag);
                 dic.Remove(iTextTag);
                 dic.Put(PdfName.TYPE, PdfName.ANNOT);
                 AdjustTabOrder(annots, ind, nn);
             } else {
                 PdfDictionary field = (PdfDictionary)list[0];
                 PdfArray kids = new PdfArray();
                 for (int k = 1; k < list.Count; k += 2) {
                     int page = (int)list[k];
                     PdfArray annots = importedPages[page - 1].mergedFields;
                     PdfDictionary widget = new PdfDictionary();
                     widget.Merge((PdfDictionary)list[k + 1]);
                     widget.Put(PdfName.PARENT, ind);
                     PdfNumber nn = (PdfNumber)widget.Get(iTextTag);
                     widget.Remove(iTextTag);
                     if (PdfCopy.IsTextField(field)) {
                         PdfString v = field.GetAsString(PdfName.V);
                         PdfObject ap = widget.GetDirectObject(PdfName.AP);
                         if (v != null && ap != null) {
                             if (!mergedTextFields.ContainsKey(list)) {
                                 mergedTextFields[list] = v;
                             } else {
                                 try {
                                     TextField tx = new TextField(this, null, null);
                                     fields[0].DecodeGenericDictionary(widget, tx);
                                     Rectangle box =
                                         PdfReader.GetNormalizedRectangle(widget.GetAsArray(PdfName.RECT));
                                     if (tx.Rotation == 90 || tx.Rotation == 270) {
                                         box = box.Rotate();
                                     }
                                     tx.Box = box;
                                     tx.Text = mergedTextFields[list].ToUnicodeString();
                                     PdfAppearance app = tx.GetAppearance();
                                     ((PdfDictionary) ap).Put(PdfName.N, app.IndirectReference);
                                 }
                                 catch (DocumentException ex) {
                                     //do nothing
                                 }
                             }
                         }
                     } else if (PdfCopy.IsCheckButton(field)) {
                         PdfName v = field.GetAsName(PdfName.V);
                         PdfName as_ = widget.GetAsName(PdfName.AS);
                         if (v != null && as_ != null)
                             widget.Put(PdfName.AS, v);
                     } else if (PdfCopy.IsRadioButton(field)) {
                         PdfName v = field.GetAsName(PdfName.V);
                         PdfName as_ = widget.GetAsName(PdfName.AS);
                         if (v != null && as_ != null && !as_.Equals(GetOffStateName(widget))) {
                             if (!mergedRadioButtons.Contains(list)) {
                                 mergedRadioButtons.Add(list);
                                 widget.Put(PdfName.AS, v);
                             } else {
                                 widget.Put(PdfName.AS, GetOffStateName(widget));
                             }
                         }
                     }
                     widget.Put(PdfName.TYPE, PdfName.ANNOT);
                     PdfIndirectReference wref = AddToBody(widget, PdfIndirectReference, true).IndirectReference;
                     AdjustTabOrder(annots, wref, nn);
                     kids.Add(wref);
                 }
                 dic.Put(PdfName.KIDS, kids);
             }
             arr.Add(ind);
             AddToBody(dic, ind, true);
         }
     }
     return arr;
 }
 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;
         }
     }
 }
Ejemplo n.º 17
0
 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;
         }
     }
 }
Ejemplo n.º 18
0
 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);
         }
     }
 }
Ejemplo n.º 19
0
        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));
        }
Ejemplo n.º 20
0
// ---------------------------------------------------------------------------    
    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);
      }
    }    
Ejemplo n.º 21
0
        virtual protected PdfArray BranchForm(Dictionary <string, object> level, PdfIndirectReference parent, String fname)
        {
            PdfArray arr = new PdfArray();

            foreach (KeyValuePair <string, object> entry in level)
            {
                String name = entry.Key;
                Object obj  = entry.Value;
                PdfIndirectReference ind = PdfIndirectReference;
                PdfDictionary        dic = new PdfDictionary();
                if (parent != null)
                {
                    dic.Put(PdfName.PARENT, parent);
                }
                dic.Put(PdfName.T, new PdfString(name, PdfObject.TEXT_UNICODE));
                String fname2 = fname + "." + name;
                int    coidx  = calculationOrder.IndexOf(fname2);
                if (coidx >= 0)
                {
                    calculationOrderRefs[coidx] = ind;
                }
                if (obj is Dictionary <string, object> )
                {
                    dic.Put(PdfName.KIDS, BranchForm((Dictionary <string, object>)obj, ind, fname2));
                    arr.Add(ind);
                    AddToBody(dic, ind);
                }
                else
                {
                    List <object> list = (List <object>)obj;
                    dic.MergeDifferent((PdfDictionary)list[0]);
                    if (list.Count == 3)
                    {
                        dic.MergeDifferent((PdfDictionary)list[2]);
                        int           page    = (int)list[1];
                        PdfDictionary pageDic = pageDics[page - 1];
                        PdfArray      annots  = pageDic.GetAsArray(PdfName.ANNOTS);
                        if (annots == null)
                        {
                            annots = new PdfArray();
                            pageDic.Put(PdfName.ANNOTS, annots);
                        }
                        PdfNumber nn = (PdfNumber)dic.Get(iTextTag);
                        dic.Remove(iTextTag);
                        AdjustTabOrder(annots, ind, nn);
                    }
                    else
                    {
                        PdfDictionary field = (PdfDictionary)list[0];
                        PdfName       v     = field.GetAsName(PdfName.V);
                        PdfArray      kids  = new PdfArray();
                        for (int k = 1; k < list.Count; k += 2)
                        {
                            int           page    = (int)list[k];
                            PdfDictionary pageDic = pageDics[page - 1];
                            PdfArray      annots  = pageDic.GetAsArray(PdfName.ANNOTS);
                            if (annots == null)
                            {
                                annots = new PdfArray();
                                pageDic.Put(PdfName.ANNOTS, annots);
                            }
                            PdfDictionary widget = new PdfDictionary();
                            widget.Merge((PdfDictionary)list[k + 1]);
                            widget.Put(PdfName.PARENT, ind);
                            PdfNumber nn = (PdfNumber)widget.Get(iTextTag);
                            widget.Remove(iTextTag);
                            if (PdfCopy.IsCheckButton(field))
                            {
                                PdfName _as = widget.GetAsName(PdfName.AS);
                                if (v != null && _as != null)
                                {
                                    widget.Put(PdfName.AS, v);
                                }
                            }
                            else if (PdfCopy.IsRadioButton(field))
                            {
                                PdfName _as = widget.GetAsName(PdfName.AS);
                                if (v != null && _as != null && !_as.Equals(GetOffStateName(widget)))
                                {
                                    if (!mergedRadioButtons.ContainsKey(list))
                                    {
                                        mergedRadioButtons[list] = null;
                                        widget.Put(PdfName.AS, v);
                                    }
                                    else
                                    {
                                        widget.Put(PdfName.AS, GetOffStateName(widget));
                                    }
                                }
                            }
                            PdfIndirectReference wref = AddToBody(widget).IndirectReference;
                            AdjustTabOrder(annots, wref, nn);
                            kids.Add(wref);
                            Propagate(widget, null, false);
                        }
                        dic.Put(PdfName.KIDS, kids);
                    }
                    arr.Add(ind);
                    AddToBody(dic, ind);
                    Propagate(dic, null, false);
                }
            }
            return(arr);
        }
Ejemplo n.º 22
0
 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;
     v = fontDesc.GetAsNumber(PdfName.FONTWEIGHT);
     if (v != null) {
         fontWeight = 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;
         }
     }
     float maxAscent = Math.Max(ury, Ascender);
     float minDescent = Math.Min(lly, Descender);
     Ascender = maxAscent * 1000 / (maxAscent - minDescent);
     Descender = minDescent * 1000 / (maxAscent - minDescent);
 }
Ejemplo n.º 23
0
        private void DoType1TT()
        {
            CMapToUnicode toUnicode = null;
            PdfObject     enc       = PdfReader.GetPdfObject(font.Get(PdfName.ENCODING));

            if (enc == null)
            {
                PdfName baseFont = font.GetAsName(PdfName.BASEFONT);
                if (BuiltinFonts14.ContainsKey(fontName) &&
                    (PdfName.SYMBOL.Equals(baseFont) || PdfName.ZAPFDINGBATS.Equals(baseFont)))
                {
                    FillEncoding(baseFont);
                }
                else
                {
                    FillEncoding(null);
                }
                toUnicode = ProcessToUnicode();
                if (toUnicode != null)
                {
                    IDictionary <int, int> rm = toUnicode.CreateReverseMapping();
                    foreach (KeyValuePair <int, int> kv in rm)
                    {
                        uni2byte[kv.Key]   = kv.Value;
                        byte2uni[kv.Value] = kv.Key;
                    }
                }
            }
            else
            {
                if (enc.IsName())
                {
                    FillEncoding((PdfName)enc);
                }
                else if (enc.IsDictionary())
                {
                    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;
                                    byte2uni[currentNumber] = c[0];
                                    diffmap[c[0]]           = currentNumber;
                                }
                                else
                                {
                                    if (toUnicode == null)
                                    {
                                        toUnicode = ProcessToUnicode();
                                        if (toUnicode == null)
                                        {
                                            toUnicode = new CMapToUnicode();
                                        }
                                    }
                                    string unicode = toUnicode.Lookup(new byte[] { (byte)currentNumber }, 0, 1);
                                    if ((unicode != null) && (unicode.Length == 1))
                                    {
                                        this.uni2byte[unicode[0]]    = currentNumber;
                                        this.byte2uni[currentNumber] = unicode[0];
                                        this.diffmap[unicode[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 = 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);
                fontWeight  = bf.GetFontDescriptor(FONT_WEIGHT, 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;
                int nSize = f + newWidths.Size;
                if (widths.Length < nSize)
                {
                    int[] tmp = new int[nSize];
                    System.Array.Copy(widths, 0, tmp, 0, f);
                    widths = tmp;
                }
                for (int k = 0; k < newWidths.Size; ++k)
                {
                    widths[f + k] = newWidths.GetAsNumber(k).IntValue;
                }
            }
            FillFontDesc(font.GetAsDict(PdfName.FONTDESCRIPTOR));
        }
Ejemplo n.º 24
0
 /**
 * Gets the page from a page dictionary
 * @param page the page dictionary
 * @return the page
 */    
 virtual public Rectangle GetPageSize(PdfDictionary page) {
     PdfArray mediaBox = page.GetAsArray(PdfName.MEDIABOX);
     return GetNormalizedRectangle(mediaBox);
 }
Ejemplo n.º 25
0
        /**
         * Gets the button appearance.
         * @throws IOException on error
         * @throws DocumentException on error
         * @return the button appearance
         */
        virtual 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 = 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));
        }