Example #1
0
        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);
            }
        }
Example #2
0
 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;
         }
     }
 }
Example #3
0
 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{}
     }
 }
Example #4
0
 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;
 }
Example #5
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);
        }
Example #6
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);
         }
     }
 }
Example #7
0
        /**
         * Gets the button appearance.
         * @throws IOException on error
         * @throws DocumentException on error
         * @return the button appearance
         */
        public PdfAppearance GetAppearance()
        {
            PdfAppearance app = GetBorderAppearance();
            Rectangle     box = new Rectangle(app.BoundingBox);

            if ((text == null || text.Length == 0) && (layout == LAYOUT_LABEL_ONLY || (image == null && template == null && iconReference == null)))
            {
                return(app);
            }
            if (layout == LAYOUT_ICON_ONLY && image == null && template == null && iconReference == null)
            {
                return(app);
            }
            BaseFont ufont       = RealFont;
            bool     borderExtra = borderStyle == PdfBorderDictionary.STYLE_BEVELED || borderStyle == PdfBorderDictionary.STYLE_INSET;
            float    h           = box.Height - borderWidth * 2;
            float    bw2         = borderWidth;

            if (borderExtra)
            {
                h   -= borderWidth * 2;
                bw2 *= 2;
            }
            float offsetX = (borderExtra ? 2 * borderWidth : borderWidth);

            offsetX = Math.Max(offsetX, 1);
            float offX = Math.Min(bw2, offsetX);

            tp = null;
            float textX   = float.NaN;
            float textY   = 0;
            float fsize   = fontSize;
            float wt      = box.Width - 2 * offX - 2;
            float ht      = box.Height - 2 * offX;
            float adj     = (iconFitToBounds ? 0 : offX + 1);
            int   nlayout = layout;

            if (image == null && template == null && iconReference == null)
            {
                nlayout = LAYOUT_LABEL_ONLY;
            }
            Rectangle iconBox = null;

            while (true)
            {
                switch (nlayout)
                {
                case LAYOUT_LABEL_ONLY:
                case LAYOUT_LABEL_OVER_ICON:
                    if (text != null && text.Length > 0 && wt > 0 && ht > 0)
                    {
                        fsize = CalculateFontSize(wt, ht);
                        textX = (box.Width - ufont.GetWidthPoint(text, fsize)) / 2;
                        textY = (box.Height - ufont.GetFontDescriptor(BaseFont.ASCENT, fsize)) / 2;
                    }
                    goto case LAYOUT_ICON_ONLY;

                case LAYOUT_ICON_ONLY:
                    if (nlayout == LAYOUT_LABEL_OVER_ICON || nlayout == LAYOUT_ICON_ONLY)
                    {
                        iconBox = new Rectangle(box.Left + adj, box.Bottom + adj, box.Right - adj, box.Top - adj);
                    }
                    break;

                case LAYOUT_ICON_TOP_LABEL_BOTTOM:
                    if (text == null || text.Length == 0 || wt <= 0 || ht <= 0)
                    {
                        nlayout = LAYOUT_ICON_ONLY;
                        continue;
                    }
                    float nht = box.Height * 0.35f - offX;
                    if (nht > 0)
                    {
                        fsize = CalculateFontSize(wt, nht);
                    }
                    else
                    {
                        fsize = 4;
                    }
                    textX   = (box.Width - ufont.GetWidthPoint(text, fsize)) / 2;
                    textY   = offX - ufont.GetFontDescriptor(BaseFont.DESCENT, fsize);
                    iconBox = new Rectangle(box.Left + adj, textY + fsize, box.Right - adj, box.Top - adj);
                    break;

                case LAYOUT_LABEL_TOP_ICON_BOTTOM:
                    if (text == null || text.Length == 0 || wt <= 0 || ht <= 0)
                    {
                        nlayout = LAYOUT_ICON_ONLY;
                        continue;
                    }
                    nht = box.Height * 0.35f - offX;
                    if (nht > 0)
                    {
                        fsize = CalculateFontSize(wt, nht);
                    }
                    else
                    {
                        fsize = 4;
                    }
                    textX = (box.Width - ufont.GetWidthPoint(text, fsize)) / 2;
                    textY = box.Height - offX - fsize;
                    if (textY < offX)
                    {
                        textY = offX;
                    }
                    iconBox = new Rectangle(box.Left + adj, box.Bottom + adj, box.Right - adj, textY + ufont.GetFontDescriptor(BaseFont.DESCENT, fsize));
                    break;

                case LAYOUT_LABEL_LEFT_ICON_RIGHT:
                    if (text == null || text.Length == 0 || wt <= 0 || ht <= 0)
                    {
                        nlayout = LAYOUT_ICON_ONLY;
                        continue;
                    }
                    float nw = box.Width * 0.35f - offX;
                    if (nw > 0)
                    {
                        fsize = CalculateFontSize(wt, nw);
                    }
                    else
                    {
                        fsize = 4;
                    }
                    if (ufont.GetWidthPoint(text, fsize) >= wt)
                    {
                        nlayout = LAYOUT_LABEL_ONLY;
                        fsize   = fontSize;
                        continue;
                    }
                    textX   = offX + 1;
                    textY   = (box.Height - ufont.GetFontDescriptor(BaseFont.ASCENT, fsize)) / 2;
                    iconBox = new Rectangle(textX + ufont.GetWidthPoint(text, fsize), box.Bottom + adj, box.Right - adj, box.Top - adj);
                    break;

                case LAYOUT_ICON_LEFT_LABEL_RIGHT:
                    if (text == null || text.Length == 0 || wt <= 0 || ht <= 0)
                    {
                        nlayout = LAYOUT_ICON_ONLY;
                        continue;
                    }
                    nw = box.Width * 0.35f - offX;
                    if (nw > 0)
                    {
                        fsize = CalculateFontSize(wt, nw);
                    }
                    else
                    {
                        fsize = 4;
                    }
                    if (ufont.GetWidthPoint(text, fsize) >= wt)
                    {
                        nlayout = LAYOUT_LABEL_ONLY;
                        fsize   = fontSize;
                        continue;
                    }
                    textX   = box.Width - ufont.GetWidthPoint(text, fsize) - offX - 1;
                    textY   = (box.Height - ufont.GetFontDescriptor(BaseFont.ASCENT, fsize)) / 2;
                    iconBox = new Rectangle(box.Left + adj, box.Bottom + adj, textX - 1, box.Top - adj);
                    break;
                }
                break;
            }
            if (textY < box.Bottom + offX)
            {
                textY = box.Bottom + offX;
            }
            if (iconBox != null && (iconBox.Width <= 0 || iconBox.Height <= 0))
            {
                iconBox = null;
            }
            bool     haveIcon          = false;
            float    boundingBoxWidth  = 0;
            float    boundingBoxHeight = 0;
            PdfArray matrix            = null;

            if (iconBox != null)
            {
                if (image != null)
                {
                    tp             = new PdfTemplate(writer);
                    tp.BoundingBox = new Rectangle(image);
                    writer.AddDirectTemplateSimple(tp, PdfName.FRM);
                    tp.AddImage(image, image.Width, 0, 0, image.Height, 0, 0);
                    haveIcon          = true;
                    boundingBoxWidth  = tp.BoundingBox.Width;
                    boundingBoxHeight = tp.BoundingBox.Height;
                }
                else if (template != null)
                {
                    tp             = new PdfTemplate(writer);
                    tp.BoundingBox = new Rectangle(template.Width, template.Height);
                    writer.AddDirectTemplateSimple(tp, PdfName.FRM);
                    tp.AddTemplate(template, template.BoundingBox.Left, template.BoundingBox.Bottom);
                    haveIcon          = true;
                    boundingBoxWidth  = tp.BoundingBox.Width;
                    boundingBoxHeight = tp.BoundingBox.Height;
                }
                else if (iconReference != null)
                {
                    PdfDictionary dic = (PdfDictionary)PdfReader.GetPdfObject(iconReference);
                    if (dic != null)
                    {
                        Rectangle r2 = PdfReader.GetNormalizedRectangle(dic.GetAsArray(PdfName.BBOX));
                        matrix            = dic.GetAsArray(PdfName.MATRIX);
                        haveIcon          = true;
                        boundingBoxWidth  = r2.Width;
                        boundingBoxHeight = r2.Height;
                    }
                }
            }
            if (haveIcon)
            {
                float icx = iconBox.Width / boundingBoxWidth;
                float icy = iconBox.Height / boundingBoxHeight;
                if (proportionalIcon)
                {
                    switch (scaleIcon)
                    {
                    case SCALE_ICON_IS_TOO_BIG:
                        icx = Math.Min(icx, icy);
                        icx = Math.Min(icx, 1);
                        break;

                    case SCALE_ICON_IS_TOO_SMALL:
                        icx = Math.Min(icx, icy);
                        icx = Math.Max(icx, 1);
                        break;

                    case SCALE_ICON_NEVER:
                        icx = 1;
                        break;

                    default:
                        icx = Math.Min(icx, icy);
                        break;
                    }
                    icy = icx;
                }
                else
                {
                    switch (scaleIcon)
                    {
                    case SCALE_ICON_IS_TOO_BIG:
                        icx = Math.Min(icx, 1);
                        icy = Math.Min(icy, 1);
                        break;

                    case SCALE_ICON_IS_TOO_SMALL:
                        icx = Math.Max(icx, 1);
                        icy = Math.Max(icy, 1);
                        break;

                    case SCALE_ICON_NEVER:
                        icx = icy = 1;
                        break;

                    default:
                        break;
                    }
                }
                float xpos = iconBox.Left + (iconBox.Width - (boundingBoxWidth * icx)) * iconHorizontalAdjustment;
                float ypos = iconBox.Bottom + (iconBox.Height - (boundingBoxHeight * icy)) * iconVerticalAdjustment;
                app.SaveState();
                app.Rectangle(iconBox.Left, iconBox.Bottom, iconBox.Width, iconBox.Height);
                app.Clip();
                app.NewPath();
                if (tp != null)
                {
                    app.AddTemplate(tp, icx, 0, 0, icy, xpos, ypos);
                }
                else
                {
                    float cox = 0;
                    float coy = 0;
                    if (matrix != null && matrix.Size == 6)
                    {
                        PdfNumber nm = matrix.GetAsNumber(4);
                        if (nm != null)
                        {
                            cox = nm.FloatValue;
                        }
                        nm = matrix.GetAsNumber(5);
                        if (nm != null)
                        {
                            coy = nm.FloatValue;
                        }
                    }
                    app.AddTemplateReference(iconReference, PdfName.FRM, icx, 0, 0, icy, xpos - cox * icx, ypos - coy * icy);
                }
                app.RestoreState();
            }
            if (!float.IsNaN(textX))
            {
                app.SaveState();
                app.Rectangle(offX, offX, box.Width - 2 * offX, box.Height - 2 * offX);
                app.Clip();
                app.NewPath();
                if (textColor == null)
                {
                    app.ResetGrayFill();
                }
                else
                {
                    app.SetColorFill(textColor);
                }
                app.BeginText();
                app.SetFontAndSize(ufont, fsize);
                app.SetTextMatrix(textX, textY);
                app.ShowText(text);
                app.EndText();
                app.RestoreState();
            }
            return(app);
        }
Example #8
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;
         }
     }
 }
Example #9
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));
        }