Example #1
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);
        }
Example #2
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);
        }
Example #3
0
 /**
 * Sets a field property. Valid property names are:
 * <p>
 * <ul>
 * <li>textfont - sets the text font. The value for this entry is a <CODE>BaseFont</CODE>.<br>
 * <li>textcolor - sets the text color. The value for this entry is a <CODE>java.awt.Color</CODE>.<br>
 * <li>textsize - sets the text size. The value for this entry is a <CODE>Float</CODE>.
 * <li>bgcolor - sets the background color. The value for this entry is a <CODE>java.awt.Color</CODE>.
 *     If <code>null</code> removes the background.<br>
 * <li>bordercolor - sets the border color. The value for this entry is a <CODE>java.awt.Color</CODE>.
 *     If <code>null</code> removes the border.<br>
 * </ul>
 * @param field the field name
 * @param name the property name
 * @param value the property value
 * @param inst an array of <CODE>int</CODE> indexing into <CODE>AcroField.Item.merged</CODE> elements to process.
 * Set to <CODE>null</CODE> to process all
 * @return <CODE>true</CODE> if the property exists, <CODE>false</CODE> otherwise
 */
 public bool SetFieldProperty(String field, String name, Object value, int[] inst)
 {
     if (writer == null)
         throw new Exception("This AcroFields instance is read-only.");
     Item item = (Item)fields[field];
     if (item == null)
         return false;
     InstHit hit = new InstHit(inst);
     PdfDictionary merged;
     PdfString da;
     if (Util.EqualsIgnoreCase(name, "textfont")) {
         for (int k = 0; k < item.Size; ++k) {
             if (hit.IsHit(k)) {
                 merged = item.GetMerged( k );
                 da = merged.GetAsString(PdfName.DA);
                 PdfDictionary dr = merged.GetAsDict(PdfName.DR);
                 if (da != null && dr != null) {
                     Object[] dao = SplitDAelements(da.ToUnicodeString());
                     PdfAppearance cb = new PdfAppearance();
                     if (dao[DA_FONT] != null) {
                         BaseFont bf = (BaseFont)value;
                         PdfName psn = (PdfName)PdfAppearance.stdFieldFontNames[bf.PostscriptFontName];
                         if (psn == null) {
                             psn = new PdfName(bf.PostscriptFontName);
                         }
                         PdfDictionary fonts = dr.GetAsDict(PdfName.FONT);
                         if (fonts == null) {
                             fonts = new PdfDictionary();
                             dr.Put(PdfName.FONT, fonts);
                         }
                         PdfIndirectReference fref = (PdfIndirectReference)fonts.Get(psn);
                         PdfDictionary top = reader.Catalog.GetAsDict(PdfName.ACROFORM);
                         MarkUsed(top);
                         dr = top.GetAsDict(PdfName.DR);
                         if (dr == null) {
                             dr = new PdfDictionary();
                             top.Put(PdfName.DR, dr);
                         }
                         MarkUsed(dr);
                         PdfDictionary fontsTop = dr.GetAsDict(PdfName.FONT);
                         if (fontsTop == null) {
                             fontsTop = new PdfDictionary();
                             dr.Put(PdfName.FONT, fontsTop);
                         }
                         MarkUsed(fontsTop);
                         PdfIndirectReference frefTop = (PdfIndirectReference)fontsTop.Get(psn);
                         if (frefTop != null) {
                             if (fref == null)
                                 fonts.Put(psn, frefTop);
                         }
                         else if (fref == null) {
                             FontDetails fd;
                             if (bf.FontType == BaseFont.FONT_TYPE_DOCUMENT) {
                                 fd = new FontDetails(null, ((DocumentFont)bf).IndirectReference, bf);
                             }
                             else {
                                 bf.Subset = false;
                                 fd = writer.AddSimple(bf);
                                 localFonts[psn.ToString().Substring(1)] = bf;
                             }
                             fontsTop.Put(psn, fd.IndirectReference);
                             fonts.Put(psn, fd.IndirectReference);
                         }
                         ByteBuffer buf = cb.InternalBuffer;
                         buf.Append(psn.GetBytes()).Append(' ').Append((float)dao[DA_SIZE]).Append(" Tf ");
                         if (dao[DA_COLOR] != null)
                             cb.SetColorFill((Color)dao[DA_COLOR]);
                         PdfString s = new PdfString(cb.ToString());
                         item.GetMerged(k).Put(PdfName.DA, s);
                         item.GetWidget(k).Put(PdfName.DA, s);
                         MarkUsed(item.GetWidget(k));
                     }
                 }
             }
         }
     }
     else if (Util.EqualsIgnoreCase(name, "textcolor")) {
         for (int k = 0; k < item.Size; ++k) {
             if (hit.IsHit(k)) {
                 merged = item.GetMerged( k );
                 da = merged.GetAsString(PdfName.DA);
                 if (da != null) {
                     Object[] dao = SplitDAelements(da.ToUnicodeString());
                     PdfAppearance cb = new PdfAppearance();
                     if (dao[DA_FONT] != null) {
                         ByteBuffer buf = cb.InternalBuffer;
                         buf.Append(new PdfName((String)dao[DA_FONT]).GetBytes()).Append(' ').Append((float)dao[DA_SIZE]).Append(" Tf ");
                         cb.SetColorFill((Color)value);
                         PdfString s = new PdfString(cb.ToString());
                         item.GetMerged(k).Put(PdfName.DA, s);
                         item.GetWidget(k).Put(PdfName.DA, s);
                         MarkUsed(item.GetWidget(k));
                     }
                 }
             }
         }
     }
     else if (Util.EqualsIgnoreCase(name, "textsize")) {
         for (int k = 0; k < item.Size; ++k) {
             if (hit.IsHit(k)) {
                 merged = item.GetMerged( k );
                 da = merged.GetAsString(PdfName.DA);
                 if (da != null) {
                     Object[] dao = SplitDAelements(da.ToUnicodeString());
                     PdfAppearance cb = new PdfAppearance();
                     if (dao[DA_FONT] != null) {
                         ByteBuffer buf = cb.InternalBuffer;
                         buf.Append(new PdfName((String)dao[DA_FONT]).GetBytes()).Append(' ').Append((float)value).Append(" Tf ");
                         if (dao[DA_COLOR] != null)
                             cb.SetColorFill((Color)dao[DA_COLOR]);
                         PdfString s = new PdfString(cb.ToString());
                         item.GetMerged(k).Put(PdfName.DA, s);
                         item.GetWidget(k).Put(PdfName.DA, s);
                         MarkUsed(item.GetWidget(k));
                     }
                 }
             }
         }
     }
     else if (Util.EqualsIgnoreCase(name, "bgcolor") || Util.EqualsIgnoreCase(name, "bordercolor")) {
         PdfName dname = (Util.EqualsIgnoreCase(name, "bgcolor") ? PdfName.BG : PdfName.BC);
         for (int k = 0; k < item.Size; ++k) {
             if (hit.IsHit(k)) {
                 merged = item.GetMerged( k );
                 PdfDictionary mk = merged.GetAsDict(PdfName.MK);
                 if (mk == null) {
                     if (value == null)
                         return true;
                     mk = new PdfDictionary();
                     item.GetMerged(k).Put(PdfName.MK, mk);
                     item.GetWidget(k).Put(PdfName.MK, mk);
                     MarkUsed(item.GetWidget(k));
                 } else {
                     MarkUsed( mk );
                 }
                 if (value == null)
                     mk.Remove(dname);
                 else
                     mk.Put(dname, PdfFormField.GetMKColor((Color)value));
             }
         }
     }
     else
         return false;
     return true;
 }
Example #4
0
        protected PdfFormField GetChoiceField(bool isList)
        {
            options &= (~MULTILINE) & (~COMB);
            String[] uchoices = choices;
            if (uchoices == null)
            {
                uchoices = new String[0];
            }
            int topChoice = choiceSelection;

            if (topChoice >= uchoices.Length)
            {
                topChoice = uchoices.Length - 1;
            }
            if (text == null)
            {
                text = "";               //fixed by Kazuya Ujihara (ujihara.jp)
            }
            if (topChoice >= 0)
            {
                text = uchoices[topChoice];
            }
            if (topChoice < 0)
            {
                topChoice = 0;
            }
            PdfFormField field = null;

            String[,] mix = null;
            if (choiceExports == null)
            {
                if (isList)
                {
                    field = PdfFormField.CreateList(writer, uchoices, topChoice);
                }
                else
                {
                    field = PdfFormField.CreateCombo(writer, (options & EDIT) != 0, uchoices, topChoice);
                }
            }
            else
            {
                mix = new String[uchoices.Length, 2];
                for (int k = 0; k < mix.GetLength(0); ++k)
                {
                    mix[k, 0] = mix[k, 1] = uchoices[k];
                }
                int top = Math.Min(uchoices.Length, choiceExports.Length);
                for (int k = 0; k < top; ++k)
                {
                    if (choiceExports[k] != null)
                    {
                        mix[k, 0] = choiceExports[k];
                    }
                }
                if (isList)
                {
                    field = PdfFormField.CreateList(writer, mix, topChoice);
                }
                else
                {
                    field = PdfFormField.CreateCombo(writer, (options & EDIT) != 0, mix, topChoice);
                }
            }
            field.SetWidget(box, PdfAnnotation.HIGHLIGHT_INVERT);
            if (rotation != 0)
            {
                field.MKRotation = rotation;
            }
            if (fieldName != null)
            {
                field.FieldName = fieldName;
                if (uchoices.Length > 0)
                {
                    if (mix != null)
                    {
                        field.ValueAsString        = mix[topChoice, 0];
                        field.DefaultValueAsString = mix[topChoice, 0];
                    }
                    else
                    {
                        field.ValueAsString        = text;
                        field.DefaultValueAsString = text;
                    }
                }
                if ((options & READ_ONLY) != 0)
                {
                    field.SetFieldFlags(PdfFormField.FF_READ_ONLY);
                }
                if ((options & REQUIRED) != 0)
                {
                    field.SetFieldFlags(PdfFormField.FF_REQUIRED);
                }
                if ((options & DO_NOT_SPELL_CHECK) != 0)
                {
                    field.SetFieldFlags(PdfFormField.FF_DONOTSPELLCHECK);
                }
            }
            field.BorderStyle = new PdfBorderDictionary(borderWidth, borderStyle, new PdfDashPattern(3));
            PdfAppearance tp;

            if (isList)
            {
                tp = GetListAppearance();
                if (topFirst > 0)
                {
                    field.Put(PdfName.TI, new PdfNumber(topFirst));
                }
            }
            else
            {
                tp = GetAppearance();
            }
            field.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, tp);
            PdfAppearance da = (PdfAppearance)tp.Duplicate;

            da.SetFontAndSize(RealFont, fontSize);
            if (textColor == null)
            {
                da.SetGrayFill(0);
            }
            else
            {
                da.SetColorFill(textColor);
            }
            field.DefaultAppearanceString = da;
            if (borderColor != null)
            {
                field.MKBorderColor = borderColor;
            }
            if (backgroundColor != null)
            {
                field.MKBackgroundColor = backgroundColor;
            }
            switch (visibility)
            {
            case HIDDEN:
                field.Flags = PdfAnnotation.FLAGS_PRINT | PdfAnnotation.FLAGS_HIDDEN;
                break;

            case VISIBLE_BUT_DOES_NOT_PRINT:
                break;

            case HIDDEN_BUT_PRINTABLE:
                field.Flags = PdfAnnotation.FLAGS_PRINT | PdfAnnotation.FLAGS_NOVIEW;
                break;

            default:
                field.Flags = PdfAnnotation.FLAGS_PRINT;
                break;
            }
            return(field);
        }
Example #5
0
        /**
         * Get the <code>PdfAppearance</code> of a list field
         * @throws IOException on error
         * @throws DocumentException on error
         * @return A <code>PdfAppearance</code>
         */
        internal PdfAppearance GetListAppearance()
        {
            PdfAppearance app = GetBorderAppearance();

            app.BeginVariableText();
            if (choices == null || choices.Length == 0)
            {
                app.EndVariableText();
                return(app);
            }
            int topChoice = choiceSelection;

            if (topChoice >= choices.Length)
            {
                topChoice = choices.Length - 1;
            }
            if (topChoice < 0)
            {
                topChoice = 0;
            }
            BaseFont ufont = RealFont;
            float    usize = fontSize;

            if (usize == 0)
            {
                usize = 12;
            }
            bool  borderExtra = borderStyle == PdfBorderDictionary.STYLE_BEVELED || borderStyle == PdfBorderDictionary.STYLE_INSET;
            float h           = box.Height - borderWidth * 2;
            float offsetX     = borderWidth;

            if (borderExtra)
            {
                h       -= borderWidth * 2;
                offsetX *= 2;
            }
            float leading = ufont.GetFontDescriptor(BaseFont.BBOXURY, usize) - ufont.GetFontDescriptor(BaseFont.BBOXLLY, usize);
            int   maxFit  = (int)(h / leading) + 1;
            int   first   = 0;
            int   last    = 0;

            last  = topChoice + maxFit / 2 + 1;
            first = last - maxFit;
            if (first < 0)
            {
                last += first;
                first = 0;
            }
            //        first = topChoice;
            last = first + maxFit;
            if (last > choices.Length)
            {
                last = choices.Length;
            }
            topFirst = first;
            app.SaveState();
            app.Rectangle(offsetX, offsetX, box.Width - 2 * offsetX, box.Height - 2 * offsetX);
            app.Clip();
            app.NewPath();
            Color fcolor = (textColor == null) ? GrayColor.GRAYBLACK : textColor;

            app.SetColorFill(new Color(10, 36, 106));
            app.Rectangle(offsetX, offsetX + h - (topChoice - first + 1) * leading, box.Width - 2 * offsetX, leading);
            app.Fill();
            float xp = offsetX * 2;
            float yp = offsetX + h - ufont.GetFontDescriptor(BaseFont.BBOXURY, usize);

            for (int idx = first; idx < last; ++idx, yp -= leading)
            {
                String ptext = choices[idx];
                int    rtl   = CheckRTL(ptext) ? PdfWriter.RUN_DIRECTION_LTR : PdfWriter.RUN_DIRECTION_NO_BIDI;
                ptext = RemoveCRLF(ptext);
                Phrase phrase = ComposePhrase(ptext, ufont, (idx == topChoice) ? GrayColor.GRAYWHITE : fcolor, usize);
                ColumnText.ShowTextAligned(app, Element.ALIGN_LEFT, phrase, xp, yp, 0, rtl, 0);
            }
            app.RestoreState();
            app.EndVariableText();
            return(app);
        }
Example #6
0
        /** Gets a new text field.
         * @throws IOException on error
         * @throws DocumentException on error
         * @return a new text field
         */
        public PdfFormField GetTextField()
        {
            if (maxCharacterLength <= 0)
            {
                options &= ~COMB;
            }
            if ((options & COMB) != 0)
            {
                options &= ~MULTILINE;
            }
            PdfFormField field = PdfFormField.CreateTextField(writer, false, false, maxCharacterLength);

            field.SetWidget(box, PdfAnnotation.HIGHLIGHT_INVERT);
            switch (alignment)
            {
            case Element.ALIGN_CENTER:
                field.Quadding = PdfFormField.Q_CENTER;
                break;

            case Element.ALIGN_RIGHT:
                field.Quadding = PdfFormField.Q_RIGHT;
                break;
            }
            if (rotation != 0)
            {
                field.MKRotation = rotation;
            }
            if (fieldName != null)
            {
                field.FieldName = fieldName;
                if (!"".Equals(text))
                {
                    field.ValueAsString = text;
                }
                if (defaultText != null)
                {
                    field.DefaultValueAsString = defaultText;
                }
                if ((options & READ_ONLY) != 0)
                {
                    field.SetFieldFlags(PdfFormField.FF_READ_ONLY);
                }
                if ((options & REQUIRED) != 0)
                {
                    field.SetFieldFlags(PdfFormField.FF_REQUIRED);
                }
                if ((options & MULTILINE) != 0)
                {
                    field.SetFieldFlags(PdfFormField.FF_MULTILINE);
                }
                if ((options & DO_NOT_SCROLL) != 0)
                {
                    field.SetFieldFlags(PdfFormField.FF_DONOTSCROLL);
                }
                if ((options & PASSWORD) != 0)
                {
                    field.SetFieldFlags(PdfFormField.FF_PASSWORD);
                }
                if ((options & FILE_SELECTION) != 0)
                {
                    field.SetFieldFlags(PdfFormField.FF_FILESELECT);
                }
                if ((options & DO_NOT_SPELL_CHECK) != 0)
                {
                    field.SetFieldFlags(PdfFormField.FF_DONOTSPELLCHECK);
                }
                if ((options & COMB) != 0)
                {
                    field.SetFieldFlags(PdfFormField.FF_COMB);
                }
            }
            field.BorderStyle = new PdfBorderDictionary(borderWidth, borderStyle, new PdfDashPattern(3));
            PdfAppearance tp = GetAppearance();

            field.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, tp);
            PdfAppearance da = (PdfAppearance)tp.Duplicate;

            da.SetFontAndSize(RealFont, fontSize);
            if (textColor == null)
            {
                da.SetGrayFill(0);
            }
            else
            {
                da.SetColorFill(textColor);
            }
            field.DefaultAppearanceString = da;
            if (borderColor != null)
            {
                field.MKBorderColor = borderColor;
            }
            if (backgroundColor != null)
            {
                field.MKBackgroundColor = backgroundColor;
            }
            switch (visibility)
            {
            case HIDDEN:
                field.Flags = PdfAnnotation.FLAGS_PRINT | PdfAnnotation.FLAGS_HIDDEN;
                break;

            case VISIBLE_BUT_DOES_NOT_PRINT:
                break;

            case HIDDEN_BUT_PRINTABLE:
                field.Flags = PdfAnnotation.FLAGS_PRINT | PdfAnnotation.FLAGS_NOVIEW;
                break;

            default:
                field.Flags = PdfAnnotation.FLAGS_PRINT;
                break;
            }
            return(field);
        }
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
        /**
         * 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);
        }
Example #9
0
        private void FlatFreeTextFields()
        {
            if (append)
                throw new ArgumentException("FreeText flattening is not supported in append mode.");

            for (int page = 1; page <= reader.NumberOfPages; ++page) {
                PdfDictionary pageDic = reader.GetPageN(page);
                PdfArray annots = pageDic.GetAsArray(PdfName.ANNOTS);
                if (annots == null)
                    continue;
                for (int idx = 0; idx < annots.Size; ++idx) {
                    PdfObject annoto = annots.GetDirectObject(idx);
                    if ((annoto is PdfIndirectReference) && !annoto.IsIndirect())
                        continue;

                    PdfDictionary annDic = (PdfDictionary)annoto;
                    if (!((PdfName)annDic.Get(PdfName.SUBTYPE)).Equals(PdfName.FREETEXT))
                        continue;
                    PdfNumber ff = annDic.GetAsNumber(PdfName.F);
                    int flags = (ff != null) ? ff.IntValue : 0;

                    if ( (flags & PdfFormField.FLAGS_PRINT) != 0 && (flags & PdfFormField.FLAGS_HIDDEN) == 0) {
                        PdfObject obj1 = annDic.Get(PdfName.AP);
                        if (obj1 == null)
                            continue;
                        PdfDictionary appDic = (obj1 is PdfIndirectReference) ?
                            (PdfDictionary) PdfReader.GetPdfObject(obj1) : (PdfDictionary) obj1;
                        PdfObject obj = appDic.Get(PdfName.N);
                        PdfAppearance app = null;
                        if (obj != null) {
                            PdfObject objReal = PdfReader.GetPdfObject(obj);

                            if (obj is PdfIndirectReference && !obj.IsIndirect())
                                app = new PdfAppearance((PdfIndirectReference)obj);
                            else if (objReal is PdfStream) {
                                ((PdfDictionary)objReal).Put(PdfName.SUBTYPE, PdfName.FORM);
                                app = new PdfAppearance((PdfIndirectReference)obj);
                            }
                            else {
                                if (objReal.IsDictionary()) {
                                    PdfName as_p = appDic.GetAsName(PdfName.AS);
                                    if (as_p != null) {
                                        PdfIndirectReference iref = (PdfIndirectReference)((PdfDictionary)objReal).Get(as_p);
                                        if (iref != null) {
                                            app = new PdfAppearance(iref);
                                            if (iref.IsIndirect()) {
                                                objReal = PdfReader.GetPdfObject(iref);
                                                ((PdfDictionary)objReal).Put(PdfName.SUBTYPE, PdfName.FORM);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        if (app != null) {
                            Rectangle box = PdfReader.GetNormalizedRectangle(annDic.GetAsArray(PdfName.RECT));
                            PdfContentByte cb = this.GetOverContent(page);
                            cb.SetLiteral("Q ");
                            cb.AddTemplate(app, box.Left, box.Bottom);
                            cb.SetLiteral("q ");
                        }
                    }
                }
                for (int idx = 0; idx < annots.Size; ++idx) {
                    PdfDictionary annot = annots.GetAsDict(idx);
                    if (annot != null) {
                        if (PdfName.FREETEXT.Equals(annot.Get(PdfName.SUBTYPE))) {
                            annots.Remove(idx);
                            --idx;
                        }
                    }
                }
                if (annots.IsEmpty()) {
                    PdfReader.KillIndirect(pageDic.Get(PdfName.ANNOTS));
                    pageDic.Remove(PdfName.ANNOTS);
                }
            }
        }
Example #10
0
 private void DrawTopFrame(PdfAppearance app)
 {
     app.MoveTo(borderWidth, borderWidth);
     app.LineTo(borderWidth, box.Height - borderWidth);
     app.LineTo(box.Width - borderWidth, box.Height - borderWidth);
     app.LineTo(box.Width - 2 * borderWidth, box.Height - 2 * borderWidth);
     app.LineTo(2 * borderWidth, box.Height - 2 * borderWidth);
     app.LineTo(2 * borderWidth, 2 * borderWidth);
     app.LineTo(borderWidth, borderWidth);
     app.Fill();
 }
Example #11
0
 internal void FlatFields()
 {
     if (append)
         throw new ArgumentException("Field flattening is not supported in append mode.");
     AcroFields af = AcroFields;
     Hashtable fields = acroFields.Fields;
     if (fieldsAdded && partialFlattening.Count == 0) {
         foreach (object obf in fields.Keys) {
             partialFlattening[obf] = null;
         }
     }
     PdfDictionary acroForm = reader.Catalog.GetAsDict(PdfName.ACROFORM);
     PdfArray acroFds = null;
     if (acroForm != null) {
         acroFds = (PdfArray)PdfReader.GetPdfObject(acroForm.Get(PdfName.FIELDS), acroForm);
     }
     foreach (DictionaryEntry entry in fields) {
         String name = (String)entry.Key;
         if (partialFlattening.Count != 0 && !partialFlattening.ContainsKey(name))
             continue;
         AcroFields.Item item = (AcroFields.Item)entry.Value;
         for (int k = 0; k < item.Size; ++k) {
             PdfDictionary merged = item.GetMerged(k);
             PdfNumber ff = merged.GetAsNumber(PdfName.F);
             int flags = 0;
             if (ff != null)
                 flags = ff.IntValue;
             int page = item.GetPage(k);
             PdfDictionary appDic = merged.GetAsDict(PdfName.AP);
             if (appDic != null && (flags & PdfFormField.FLAGS_PRINT) != 0 && (flags & PdfFormField.FLAGS_HIDDEN) == 0) {
                 PdfObject obj = appDic.Get(PdfName.N);
                 PdfAppearance app = null;
                 if (obj != null) {
                     PdfObject objReal = PdfReader.GetPdfObject(obj);
                     if (obj is PdfIndirectReference && !obj.IsIndirect())
                         app = new PdfAppearance((PdfIndirectReference)obj);
                     else if (objReal is PdfStream) {
                         ((PdfDictionary)objReal).Put(PdfName.SUBTYPE, PdfName.FORM);
                         app = new PdfAppearance((PdfIndirectReference)obj);
                     }
                     else {
                         if (objReal != null && objReal.IsDictionary()) {
                             PdfName as_p = merged.GetAsName(PdfName.AS);
                             if (as_p != null) {
                                 PdfIndirectReference iref = (PdfIndirectReference)((PdfDictionary)objReal).Get(as_p);
                                 if (iref != null) {
                                     app = new PdfAppearance(iref);
                                     if (iref.IsIndirect()) {
                                         objReal = PdfReader.GetPdfObject(iref);
                                         ((PdfDictionary)objReal).Put(PdfName.SUBTYPE, PdfName.FORM);
                                     }
                                 }
                             }
                         }
                     }
                 }
                 if (app != null) {
                     Rectangle box = PdfReader.GetNormalizedRectangle(merged.GetAsArray(PdfName.RECT));
                     PdfContentByte cb = GetOverContent(page);
                     cb.SetLiteral("Q ");
                     cb.AddTemplate(app, box.Left, box.Bottom);
                     cb.SetLiteral("q ");
                 }
             }
             if (partialFlattening.Count == 0)
                 continue;
             PdfDictionary pageDic = reader.GetPageN(page);
             PdfArray annots = pageDic.GetAsArray(PdfName.ANNOTS);
             if (annots == null)
                 continue;
             for (int idx = 0; idx < annots.Size; ++idx) {
                 PdfObject ran = annots[idx];
                 if (!ran.IsIndirect())
                     continue;
                 PdfObject ran2 = item.GetWidgetRef(k);
                 if (!ran2.IsIndirect())
                     continue;
                 if (((PRIndirectReference)ran).Number == ((PRIndirectReference)ran2).Number) {
                     annots.Remove(idx--);
                     PRIndirectReference wdref = (PRIndirectReference)ran2;
                     while (true) {
                         PdfDictionary wd = (PdfDictionary)PdfReader.GetPdfObject(wdref);
                         PRIndirectReference parentRef = (PRIndirectReference)wd.Get(PdfName.PARENT);
                         PdfReader.KillIndirect(wdref);
                         if (parentRef == null) { // reached AcroForm
                             for (int fr = 0; fr < acroFds.Size; ++fr) {
                                 PdfObject h = acroFds[fr];
                                 if (h.IsIndirect() && ((PRIndirectReference)h).Number == wdref.Number) {
                                     acroFds.Remove(fr);
                                     --fr;
                                 }
                             }
                             break;
                         }
                         PdfDictionary parent = (PdfDictionary)PdfReader.GetPdfObject(parentRef);
                         PdfArray kids = parent.GetAsArray(PdfName.KIDS);
                         for (int fr = 0; fr < kids.Size; ++fr) {
                             PdfObject h = kids[fr];
                             if (h.IsIndirect() && ((PRIndirectReference)h).Number == wdref.Number) {
                                 kids.Remove(fr);
                                 --fr;
                             }
                         }
                         if (!kids.IsEmpty())
                             break;
                         wdref = parentRef;
                     }
                 }
             }
             if (annots.IsEmpty()) {
                 PdfReader.KillIndirect(pageDic.Get(PdfName.ANNOTS));
                 pageDic.Remove(PdfName.ANNOTS);
             }
         }
     }
     if (!fieldsAdded && partialFlattening.Count == 0) {
         for (int page = 1; page <= reader.NumberOfPages; ++page) {
             PdfDictionary pageDic = reader.GetPageN(page);
             PdfArray annots = pageDic.GetAsArray(PdfName.ANNOTS);
             if (annots == null)
                 continue;
             for (int idx = 0; idx < annots.Size; ++idx) {
                 PdfObject annoto = annots.GetDirectObject(idx);
                 if ((annoto is PdfIndirectReference) && !annoto.IsIndirect())
                     continue;
                 if (!annoto.IsDictionary() || PdfName.WIDGET.Equals(((PdfDictionary)annoto).Get(PdfName.SUBTYPE))) {
                     annots.Remove(idx);
                     --idx;
                 }
             }
             if (annots.IsEmpty()) {
                 PdfReader.KillIndirect(pageDic.Get(PdfName.ANNOTS));
                 pageDic.Remove(PdfName.ANNOTS);
             }
         }
         EliminateAcroformObjects();
     }
 }
Example #12
0
        /**
         * Gets a radio or check field.
         * @param isRadio <CODE>true</CODE> to get a radio field, <CODE>false</CODE> to get
         * a check field
         * @throws IOException on error
         * @throws DocumentException on error
         * @return the field
         */
        protected PdfFormField GetField(bool isRadio)
        {
            PdfFormField field = null;

            if (isRadio)
            {
                field = PdfFormField.CreateEmpty(writer);
            }
            else
            {
                field = PdfFormField.CreateCheckBox(writer);
            }
            field.SetWidget(box, PdfAnnotation.HIGHLIGHT_INVERT);
            if (!isRadio)
            {
                field.FieldName = fieldName;
                if ((options & READ_ONLY) != 0)
                {
                    field.SetFieldFlags(PdfFormField.FF_READ_ONLY);
                }
                if ((options & REQUIRED) != 0)
                {
                    field.SetFieldFlags(PdfFormField.FF_REQUIRED);
                }
                field.ValueAsName = vchecked ? onValue : "Off";
            }
            if (text != null)
            {
                field.MKNormalCaption = text;
            }
            if (rotation != 0)
            {
                field.MKRotation = rotation;
            }
            field.BorderStyle = new PdfBorderDictionary(borderWidth, borderStyle, new PdfDashPattern(3));
            PdfAppearance tpon  = GetAppearance(isRadio, true);
            PdfAppearance tpoff = GetAppearance(isRadio, false);

            field.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, onValue, tpon);
            field.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Off", tpoff);
            field.AppearanceState = vchecked ? onValue : "Off";
            PdfAppearance da = (PdfAppearance)tpon.Duplicate;

            da.SetFontAndSize(RealFont, fontSize);
            if (textColor == null)
            {
                da.SetGrayFill(0);
            }
            else
            {
                da.SetColorFill(textColor);
            }
            field.DefaultAppearanceString = da;
            if (borderColor != null)
            {
                field.MKBorderColor = borderColor;
            }
            if (backgroundColor != null)
            {
                field.MKBackgroundColor = backgroundColor;
            }
            switch (visibility)
            {
            case HIDDEN:
                field.Flags = PdfAnnotation.FLAGS_PRINT | PdfAnnotation.FLAGS_HIDDEN;
                break;

            case VISIBLE_BUT_DOES_NOT_PRINT:
                break;

            case HIDDEN_BUT_PRINTABLE:
                field.Flags = PdfAnnotation.FLAGS_PRINT | PdfAnnotation.FLAGS_NOVIEW;
                break;

            default:
                field.Flags = PdfAnnotation.FLAGS_PRINT;
                break;
            }
            return(field);
        }
Example #13
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);
        }
Example #14
0
        protected PdfAppearance GetBorderAppearance()
        {
            PdfAppearance app = PdfAppearance.CreateAppearance(writer, box.Width, box.Height);

            switch (rotation)
            {
            case 90:
                app.SetMatrix(0, 1, -1, 0, box.Height, 0);
                break;

            case 180:
                app.SetMatrix(-1, 0, 0, -1, box.Width, box.Height);
                break;

            case 270:
                app.SetMatrix(0, -1, 1, 0, 0, box.Width);
                break;
            }
            app.SaveState();
            // background
            if (backgroundColor != null)
            {
                app.SetColorFill(backgroundColor);
                app.Rectangle(0, 0, box.Width, box.Height);
                app.Fill();
            }
            // border
            if (borderStyle == PdfBorderDictionary.STYLE_UNDERLINE)
            {
                if (borderWidth != 0 && borderColor != null)
                {
                    app.SetColorStroke(borderColor);
                    app.SetLineWidth(borderWidth);
                    app.MoveTo(0, borderWidth / 2);
                    app.LineTo(box.Width, borderWidth / 2);
                    app.Stroke();
                }
            }
            else if (borderStyle == PdfBorderDictionary.STYLE_BEVELED)
            {
                if (borderWidth != 0 && borderColor != null)
                {
                    app.SetColorStroke(borderColor);
                    app.SetLineWidth(borderWidth);
                    app.Rectangle(borderWidth / 2, borderWidth / 2, box.Width - borderWidth, box.Height - borderWidth);
                    app.Stroke();
                }
                // beveled
                Color actual = backgroundColor;
                if (actual == null)
                {
                    actual = Color.WHITE;
                }
                app.SetGrayFill(1);
                DrawTopFrame(app);
                app.SetColorFill(actual.Darker());
                DrawBottomFrame(app);
            }
            else if (borderStyle == PdfBorderDictionary.STYLE_INSET)
            {
                if (borderWidth != 0 && borderColor != null)
                {
                    app.SetColorStroke(borderColor);
                    app.SetLineWidth(borderWidth);
                    app.Rectangle(borderWidth / 2, borderWidth / 2, box.Width - borderWidth, box.Height - borderWidth);
                    app.Stroke();
                }
                // inset
                app.SetGrayFill(0.5f);
                DrawTopFrame(app);
                app.SetGrayFill(0.75f);
                DrawBottomFrame(app);
            }
            else
            {
                if (borderWidth != 0 && borderColor != null)
                {
                    if (borderStyle == PdfBorderDictionary.STYLE_DASHED)
                    {
                        app.SetLineDash(3, 0);
                    }
                    app.SetColorStroke(borderColor);
                    app.SetLineWidth(borderWidth);
                    app.Rectangle(borderWidth / 2, borderWidth / 2, box.Width - borderWidth, box.Height - borderWidth);
                    app.Stroke();
                    if ((options & COMB) != 0 && maxCharacterLength > 1)
                    {
                        float step = box.Width / maxCharacterLength;
                        float yb   = borderWidth / 2;
                        float yt   = box.Height - borderWidth / 2;
                        for (int k = 1; k < maxCharacterLength; ++k)
                        {
                            float x = step * k;
                            app.MoveTo(x, yb);
                            app.LineTo(x, yt);
                        }
                        app.Stroke();
                    }
                }
            }
            app.RestoreState();
            return(app);
        }
Example #15
0
 internal static PdfAppearance CreateAppearance(PdfWriter writer, float width, float height, PdfName forcedName)
 {
     PdfAppearance template = new PdfAppearance(writer);
     template.Width = width;
     template.Height = height;
     writer.AddDirectTemplateSimple(template, forcedName);
     return template;
 }