Esempio n. 1
0
        public void DrawMultiLineOfText(PdfFormField field, string text, BaseFont font, float fontSize, float llx, float lly, float urx, float ury)
        {
            PdfAppearance tp  = PdfAppearance.CreateAppearance(writer, urx - llx, ury - lly);
            PdfAppearance tp2 = (PdfAppearance)tp.Duplicate;

            tp2.SetFontAndSize(font, fontSize);
            tp2.ResetRGBColorFill();
            field.DefaultAppearanceString = tp2;
            tp.DrawTextField(0f, 0f, urx - llx, ury - lly);
            tp.BeginVariableText();
            tp.SaveState();
            tp.Rectangle(3f, 3f, urx - llx - 6f, ury - lly - 6f);
            tp.Clip();
            tp.NewPath();
            tp.BeginText();
            tp.SetFontAndSize(font, fontSize);
            tp.ResetRGBColorFill();
            tp.SetTextMatrix(4, 5);
            var   tokenizer = new StringTokenizer(text, "\n");
            float yPos      = ury - lly;

            while (tokenizer.HasMoreTokens())
            {
                yPos -= fontSize * 1.2f;
                tp.ShowTextAligned(PdfContentByte.ALIGN_LEFT, tokenizer.NextToken(), 3, yPos, 0);
            }
            tp.EndText();
            tp.RestoreState();
            tp.EndVariableText();
            field.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, tp);
        }
Esempio n. 2
0
        public void DrawCheckBoxAppearences(PdfFormField field, string value, float llx, float lly, float urx, float ury)
        {
            BaseFont      font = BaseFont.CreateFont(BaseFont.ZAPFDINGBATS, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
            float         size = (ury - lly);
            PdfAppearance tpOn = PdfAppearance.CreateAppearance(writer, urx - llx, ury - lly);
            PdfAppearance tp2  = (PdfAppearance)tpOn.Duplicate;

            tp2.SetFontAndSize(font, size);
            tp2.ResetRGBColorFill();
            field.DefaultAppearanceString = tp2;
            tpOn.DrawTextField(0f, 0f, urx - llx, ury - lly);
            tpOn.SaveState();
            tpOn.ResetRGBColorFill();
            tpOn.BeginText();
            tpOn.SetFontAndSize(font, size);
            tpOn.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "4", (urx - llx) / 2, (ury - lly) / 2 - (size * 0.3f), 0);
            tpOn.EndText();
            tpOn.RestoreState();
            field.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, value, tpOn);
            PdfAppearance tpOff = PdfAppearance.CreateAppearance(writer, urx - llx, ury - lly);

            tpOff.DrawTextField(0f, 0f, urx - llx, ury - lly);
            field.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Off", tpOff);
        }
Esempio n. 3
0
        public void DrawSingleLineOfText(PdfFormField field, string text, BaseFont font, float fontSize, float llx, float lly, float urx, float ury)
        {
            PdfAppearance tp  = PdfAppearance.CreateAppearance(writer, urx - llx, ury - lly);
            PdfAppearance tp2 = (PdfAppearance)tp.Duplicate;

            tp2.SetFontAndSize(font, fontSize);
            tp2.ResetRGBColorFill();
            field.DefaultAppearanceString = tp2;
            tp.DrawTextField(0f, 0f, urx - llx, ury - lly);
            tp.BeginVariableText();
            tp.SaveState();
            tp.Rectangle(3f, 3f, urx - llx - 6f, ury - lly - 6f);
            tp.Clip();
            tp.NewPath();
            tp.BeginText();
            tp.SetFontAndSize(font, fontSize);
            tp.ResetRGBColorFill();
            tp.SetTextMatrix(4, (ury - lly) / 2 - (fontSize * 0.3f));
            tp.ShowText(text);
            tp.EndText();
            tp.RestoreState();
            tp.EndVariableText();
            field.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, tp);
        }
Esempio n. 4
0
        /**
         * Get the <code>PdfAppearance</code> of a text or combo field
         * @throws IOException on error
         * @throws DocumentException on error
         * @return A <code>PdfAppearance</code>
         */
        public PdfAppearance GetAppearance()
        {
            PdfAppearance app = GetBorderAppearance();

            app.BeginVariableText();
            if (text == null || text.Length == 0)
            {
                app.EndVariableText();
                return(app);
            }
            bool  borderExtra = borderStyle == PdfBorderDictionary.STYLE_BEVELED || borderStyle == PdfBorderDictionary.STYLE_INSET;
            float h           = box.Height - borderWidth * 2 - extraMarginTop;
            float bw2         = borderWidth;

            if (borderExtra)
            {
                h   -= borderWidth * 2;
                bw2 *= 2;
            }
            float offsetX = Math.Max(bw2, 1);
            float offX    = Math.Min(bw2, offsetX);

            app.SaveState();
            app.Rectangle(offX, offX, box.Width - 2 * offX, box.Height - 2 * offX);
            app.Clip();
            app.NewPath();
            String ptext;

            if ((options & PASSWORD) != 0)
            {
                ptext = ObfuscatePassword(text);
            }
            else if ((options & MULTILINE) == 0)
            {
                ptext = RemoveCRLF(text);
            }
            else
            {
                ptext = text; //fixed by Kazuya Ujihara (ujihara.jp)
            }
            BaseFont ufont  = RealFont;
            Color    fcolor = (textColor == null) ? GrayColor.GRAYBLACK : textColor;
            int      rtl    = CheckRTL(ptext) ? PdfWriter.RUN_DIRECTION_LTR : PdfWriter.RUN_DIRECTION_NO_BIDI;
            float    usize  = fontSize;
            Phrase   phrase = ComposePhrase(ptext, ufont, fcolor, usize);

            if ((options & MULTILINE) != 0)
            {
                float      width  = box.Width - 4 * offsetX - extraMarginLeft;
                float      factor = ufont.GetFontDescriptor(BaseFont.BBOXURY, 1) - ufont.GetFontDescriptor(BaseFont.BBOXLLY, 1);
                ColumnText ct     = new ColumnText(null);
                if (usize == 0)
                {
                    usize = h / factor;
                    if (usize > 4)
                    {
                        if (usize > 12)
                        {
                            usize = 12;
                        }
                        float step = Math.Max((usize - 4) / 10, 0.2f);
                        ct.SetSimpleColumn(0, -h, width, 0);
                        ct.Alignment    = alignment;
                        ct.RunDirection = rtl;
                        for (; usize > 4; usize -= step)
                        {
                            ct.YLine = 0;
                            ChangeFontSize(phrase, usize);
                            ct.SetText(phrase);
                            ct.Leading = factor * usize;
                            int status = ct.Go(true);
                            if ((status & ColumnText.NO_MORE_COLUMN) == 0)
                            {
                                break;
                            }
                        }
                    }
                    if (usize < 4)
                    {
                        usize = 4;
                    }
                }
                ChangeFontSize(phrase, usize);
                ct.Canvas = app;
                float leading = usize * factor;
                float offsetY = offsetX + h - ufont.GetFontDescriptor(BaseFont.BBOXURY, usize);
                ct.SetSimpleColumn(extraMarginLeft + 2 * offsetX, -20000, box.Width - 2 * offsetX, offsetY + leading);
                ct.Leading      = leading;
                ct.Alignment    = alignment;
                ct.RunDirection = rtl;
                ct.SetText(phrase);
                ct.Go();
            }
            else
            {
                if (usize == 0)
                {
                    float maxCalculatedSize = h / (ufont.GetFontDescriptor(BaseFont.BBOXURX, 1) - ufont.GetFontDescriptor(BaseFont.BBOXLLY, 1));
                    ChangeFontSize(phrase, 1);
                    float wd = ColumnText.GetWidth(phrase, rtl, 0);
                    if (wd == 0)
                    {
                        usize = maxCalculatedSize;
                    }
                    else
                    {
                        usize = Math.Min(maxCalculatedSize, (box.Width - extraMarginLeft - 4 * offsetX) / wd);
                    }
                    if (usize < 4)
                    {
                        usize = 4;
                    }
                }
                ChangeFontSize(phrase, usize);
                float offsetY = offX + ((box.Height - 2 * offX) - ufont.GetFontDescriptor(BaseFont.ASCENT, usize)) / 2;
                if (offsetY < offX)
                {
                    offsetY = offX;
                }
                if (offsetY - offX < -ufont.GetFontDescriptor(BaseFont.DESCENT, usize))
                {
                    float ny = -ufont.GetFontDescriptor(BaseFont.DESCENT, usize) + offX;
                    float dy = box.Height - offX - ufont.GetFontDescriptor(BaseFont.ASCENT, usize);
                    offsetY = Math.Min(ny, Math.Max(offsetY, dy));
                }
                if ((options & COMB) != 0 && maxCharacterLength > 0)
                {
                    int textLen  = Math.Min(maxCharacterLength, ptext.Length);
                    int position = 0;
                    if (alignment == Element.ALIGN_RIGHT)
                    {
                        position = maxCharacterLength - textLen;
                    }
                    else if (alignment == Element.ALIGN_CENTER)
                    {
                        position = (maxCharacterLength - textLen) / 2;
                    }
                    float step  = (box.Width - extraMarginLeft) / maxCharacterLength;
                    float start = step / 2 + position * step;
                    if (textColor == null)
                    {
                        app.SetGrayFill(0);
                    }
                    else
                    {
                        app.SetColorFill(textColor);
                    }
                    app.BeginText();
                    foreach (Chunk ck in phrase)
                    {
                        BaseFont bf = ck.Font.BaseFont;
                        app.SetFontAndSize(bf, usize);
                        StringBuilder sb = ck.Append("");
                        for (int j = 0; j < sb.Length; ++j)
                        {
                            String c  = sb.ToString(j, 1);
                            float  wd = bf.GetWidthPoint(c, usize);
                            app.SetTextMatrix(extraMarginLeft + start - wd / 2, offsetY - extraMarginTop);
                            app.ShowText(c);
                            start += step;
                        }
                    }
                    app.EndText();
                }
                else
                {
                    float x;
                    switch (alignment)
                    {
                    case Element.ALIGN_RIGHT:
                        x = extraMarginLeft + box.Width - (2 * offsetX);
                        break;

                    case Element.ALIGN_CENTER:
                        x = extraMarginLeft + (box.Width / 2);
                        break;

                    default:
                        x = extraMarginLeft + (2 * offsetX);
                        break;
                    }
                    ColumnText.ShowTextAligned(app, alignment, phrase, x, offsetY - extraMarginTop, 0, rtl, 0);
                }
            }
            app.RestoreState();
            app.EndVariableText();
            return(app);
        }
Esempio n. 5
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);
        }
Esempio n. 6
0
        /**
         * Gets the field appearance.
         * @param isRadio <CODE>true</CODE> for a radio field and <CODE>false</CODE>
         * for a check field
         * @param on <CODE>true</CODE> for the checked state, <CODE>false</CODE>
         * otherwise
         * @throws IOException on error
         * @throws DocumentException on error
         * @return the appearance
         */
        public PdfAppearance GetAppearance(bool isRadio, bool on)
        {
            if (isRadio && checkType == TYPE_CIRCLE)
            {
                return(GetAppearanceRadioCircle(on));
            }
            PdfAppearance app = GetBorderAppearance();

            if (!on)
            {
                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);
            float wt    = box.Width - 2 * offX;
            float ht    = box.Height - 2 * offX;
            float fsize = fontSize;

            if (fsize == 0)
            {
                float bw = ufont.GetWidthPoint(text, 1);
                if (bw == 0)
                {
                    fsize = 12;
                }
                else
                {
                    fsize = wt / bw;
                }
                float nfsize = h / (ufont.GetFontDescriptor(BaseFont.ASCENT, 1));
                fsize = Math.Min(fsize, nfsize);
            }
            app.SaveState();
            app.Rectangle(offX, offX, wt, ht);
            app.Clip();
            app.NewPath();
            if (textColor == null)
            {
                app.ResetGrayFill();
            }
            else
            {
                app.SetColorFill(textColor);
            }
            app.BeginText();
            app.SetFontAndSize(ufont, fsize);
            app.SetTextMatrix((box.Width - ufont.GetWidthPoint(text, fsize)) / 2,
                              (box.Height - ufont.GetAscentPoint(text, fsize)) / 2);
            app.ShowText(text);
            app.EndText();
            app.RestoreState();
            return(app);
        }