Class TextField

java.lang.Object
com.aowagie.text.pdf.TextField

public class TextField extends Object
Supports text, combo and list fields generating the correct appearances. All the option in the Acrobat GUI are supported in an easy to use API.
Author:
Paulo Soares (psoares@consiste.pt)
  • Field Details

    • HIDDEN

      public static final int HIDDEN
      The field is hidden.
      See Also:
    • EDIT

      public static final int EDIT
      If set the combo box includes an editable text box as well as a drop list; if clear, it includes only a drop list. This flag is only meaningful with combo fields.
      See Also:
    • COMB

      public static final int COMB
      combo box flag.
      See Also:
    • borderWidth

      protected float borderWidth
    • borderStyle

      protected int borderStyle
    • borderColor

      protected Color borderColor
    • backgroundColor

      protected Color backgroundColor
    • textColor

      protected Color textColor
    • fontSize

      protected float fontSize
    • alignment

      protected int alignment
    • writer

      protected PdfWriter writer
    • text

      protected String text
    • box

      protected Rectangle box
    • rotation

      protected int rotation
      Holds value of property rotation.
    • visibility

      protected int visibility
      Holds value of property visibility.
    • fieldName

      protected String fieldName
      Holds value of property fieldName.
    • options

      protected int options
      Holds value of property options.
    • maxCharacterLength

      protected int maxCharacterLength
      Holds value of property maxCharacterLength.
  • Constructor Details

    • TextField

      public TextField(PdfWriter writer, Rectangle box, String fieldName)
      Creates a new TextField.
      Parameters:
      writer - the document PdfWriter
      box - the field location and dimensions
      fieldName - the field name. If null only the widget keys will be included in the field allowing it to be used as a kid field.
  • Method Details

    • removeCRLF

      public static String removeCRLF(String text)
      Removes CRLF from a String.
      Parameters:
      text - text to search
      Returns:
      String string with remove
      Since:
      2.1.5
    • obfuscatePassword

      public static String obfuscatePassword(String text)
      Obfuscates a password String. Every character is replaced by an asterisk (*).
      Parameters:
      text - text to be treated
      Returns:
      String text treated
      Since:
      2.1.5
    • getAppearance

      public com.aowagie.text.pdf.PdfAppearance getAppearance() throws IOException, DocumentException
      Get the PdfAppearance of a text or combo field
      Returns:
      A PdfAppearance
      Throws:
      IOException - on error
      DocumentException - on error
    • getTextField

      public PdfFormField getTextField() throws IOException, DocumentException
      Gets a new text field.
      Returns:
      a new text field
      Throws:
      IOException - on error
      DocumentException - on error
    • getComboField

      public PdfFormField getComboField() throws IOException, DocumentException
      Gets a new combo field.
      Returns:
      a new combo field
      Throws:
      IOException - on error
      DocumentException - on error
    • getListField

      public PdfFormField getListField() throws IOException, DocumentException
      Gets a new list field.
      Returns:
      a new list field
      Throws:
      IOException - on error
      DocumentException - on error
    • getChoiceField

      protected PdfFormField getChoiceField(boolean isList) throws IOException, DocumentException
      Throws:
      IOException
      DocumentException
    • getDefaultText

      public String getDefaultText()
      Gets the default text.
      Returns:
      the default text
    • setDefaultText

      public void setDefaultText(String defaultText)
      Sets the default text. It is only meaningful for text fields.
      Parameters:
      defaultText - the default text
    • getChoices

      public String[] getChoices()
      Gets the choices to be presented to the user in list/combo fields.
      Returns:
      the choices to be presented to the user
    • setChoices

      public void setChoices(String[] choices)
      Sets the choices to be presented to the user in list/combo fields.
      Parameters:
      choices - the choices to be presented to the user
    • getChoiceExports

      public String[] getChoiceExports()
      Gets the export values in list/combo fields.
      Returns:
      the export values in list/combo fields
    • setChoiceExports

      public void setChoiceExports(String[] choiceExports)
      Sets the export values in list/combo fields. If this array is null then the choice values will also be used as the export values.
      Parameters:
      choiceExports - the export values in list/combo fields
    • getChoiceSelection

      public int getChoiceSelection()
      Gets the zero based index of the selected item.
      Returns:
      the zero based index of the selected item
    • setChoiceSelection

      public void setChoiceSelection(int choiceSelection)
      Sets the zero based index of the selected item.
      Parameters:
      choiceSelection - the zero based index of the selected item
    • setExtraMargin

      public void setExtraMargin(float extraMarginLeft, float extraMarginTop)
      Sets extra margins in text fields to better mimic the Acrobat layout.
      Parameters:
      extraMarginLeft - the extra margin left
      extraMarginTop - the extra margin top
    • getSubstitutionFonts

      public ArrayList getSubstitutionFonts()
      Gets the list of substitution fonts. The list is composed of BaseFont and can be null. The fonts in this list will be used if the original font doesn't contain the needed glyphs.
      Returns:
      the list
    • setSubstitutionFonts

      public void setSubstitutionFonts(ArrayList substitutionFonts)
      Sets a list of substitution fonts. The list is composed of BaseFont and can also be null. The fonts in this list will be used if the original font doesn't contain the needed glyphs.
      Parameters:
      substitutionFonts - the list
    • getExtensionFont

      public BaseFont getExtensionFont()
      Gets the extensionFont. This font will be searched before the substitution fonts. It may be null.
      Returns:
      the extensionFont
    • setExtensionFont

      public void setExtensionFont(BaseFont extensionFont)
      Sets the extensionFont. This font will be searched before the substitution fonts. It may be null.
      Parameters:
      extensionFont - New value of property extensionFont.
    • getRealFont

      protected BaseFont getRealFont() throws IOException, DocumentException
      Throws:
      IOException
      DocumentException
    • getBorderAppearance

      protected com.aowagie.text.pdf.PdfAppearance getBorderAppearance()
    • getBorderWidth

      public float getBorderWidth()
      Gets the border width in points.
      Returns:
      the border width in points
    • setBorderWidth

      public void setBorderWidth(float borderWidth)
      Sets the border width in points. To eliminate the border set the border color to null.
      Parameters:
      borderWidth - the border width in points
    • getBorderStyle

      public int getBorderStyle()
      Gets the border style.
      Returns:
      the border style
    • setBorderStyle

      public void setBorderStyle(int borderStyle)
      Sets the border style. The styles are found in PdfBorderDictionary and can be STYLE_SOLID, STYLE_DASHED, STYLE_BEVELED, STYLE_INSET and STYLE_UNDERLINE.
      Parameters:
      borderStyle - the border style
    • getBorderColor

      public Color getBorderColor()
      Gets the border color.
      Returns:
      the border color
    • setBorderColor

      public void setBorderColor(Color borderColor)
      Sets the border color. Set to null to remove the border.
      Parameters:
      borderColor - the border color
    • getBackgroundColor

      public Color getBackgroundColor()
      Gets the background color.
      Returns:
      the background color
    • setBackgroundColor

      public void setBackgroundColor(Color backgroundColor)
      Sets the background color. Set to null for transparent background.
      Parameters:
      backgroundColor - the background color
    • getTextColor

      public Color getTextColor()
      Gets the text color.
      Returns:
      the text color
    • setTextColor

      public void setTextColor(Color textColor)
      Sets the text color. If null the color used will be black.
      Parameters:
      textColor - the text color
    • getFont

      public BaseFont getFont()
      Gets the text font.
      Returns:
      the text font
    • setFont

      public void setFont(BaseFont font)
      Sets the text font. If null then Helvetica will be used.
      Parameters:
      font - the text font
    • getFontSize

      public float getFontSize()
      Gets the font size.
      Returns:
      the font size
    • setFontSize

      public void setFontSize(float fontSize)
      Sets the font size. If 0 then auto-sizing will be used but only for text fields.
      Parameters:
      fontSize - the font size
    • getAlignment

      public int getAlignment()
      Gets the text horizontal alignment.
      Returns:
      the text horizontal alignment
    • setAlignment

      public void setAlignment(int alignment)
      Sets the text horizontal alignment. It can be Element.ALIGN_LEFT, Element.ALIGN_CENTER and Element.ALIGN_RIGHT.
      Parameters:
      alignment - the text horizontal alignment
    • getText

      public String getText()
      Gets the text.
      Returns:
      the text
    • setText

      public void setText(String text)
      Sets the text for text fields.
      Parameters:
      text - the text
    • getBox

      public Rectangle getBox()
      Gets the field dimension and position.
      Returns:
      the field dimension and position
    • setBox

      public void setBox(Rectangle box)
      Sets the field dimension and position.
      Parameters:
      box - the field dimension and position
    • getRotation

      public int getRotation()
      Gets the field rotation.
      Returns:
      the field rotation
    • setRotation

      public void setRotation(int rotation)
      Sets the field rotation. This value should be the same as the page rotation where the field will be shown.
      Parameters:
      rotation - the field rotation
    • setRotationFromPage

      public void setRotationFromPage(Rectangle page)
      Convenience method to set the field rotation the same as the page rotation.
      Parameters:
      page - the page
    • getVisibility

      public int getVisibility()
      Gets the field visibility flag.
      Returns:
      the field visibility flag
    • setVisibility

      public void setVisibility(int visibility)
      Sets the field visibility flag. This flags can be one of VISIBLE, HIDDEN, VISIBLE_BUT_DOES_NOT_PRINT and HIDDEN_BUT_PRINTABLE.
      Parameters:
      visibility - field visibility flag
    • getFieldName

      public String getFieldName()
      Gets the field name.
      Returns:
      the field name
    • setFieldName

      public void setFieldName(String fieldName)
      Sets the field name.
      Parameters:
      fieldName - the field name. If null only the widget keys will be included in the field allowing it to be used as a kid field.
    • getOptions

      public int getOptions()
      Gets the option flags.
      Returns:
      the option flags
    • setOptions

      public void setOptions(int options)
      Sets the option flags. The option flags can be a combination by oring of READ_ONLY, REQUIRED, MULTILINE, DO_NOT_SCROLL, PASSWORD, FILE_SELECTION, DO_NOT_SPELL_CHECK and EDIT.
      Parameters:
      options - the option flags
    • getMaxCharacterLength

      public int getMaxCharacterLength()
      Gets the maximum length of the field's text, in characters.
      Returns:
      the maximum length of the field's text, in characters.
    • setMaxCharacterLength

      public void setMaxCharacterLength(int maxCharacterLength)
      Sets the maximum length of the field's text, in characters. It is only meaningful for text fields.
      Parameters:
      maxCharacterLength - the maximum length of the field's text, in characters
    • getWriter

      public PdfWriter getWriter()
      Getter for property writer.
      Returns:
      Value of property writer.
    • setWriter

      public void setWriter(PdfWriter writer)
      Setter for property writer.
      Parameters:
      writer - New value of property writer.