📄 formtext.java
字号:
// $Id: FormText.java,v 1.3 2003/10/06 12:40:06 mike Exp $package org.faceless.pdf;import java.io.*;import java.util.*;import java.awt.Color;/** * <p> * A type of form element representing a Text Field. Text fields may be single * or multi-line, or may represent a password or (in Acrobat 5.0) a filename. * </p><p> * Here's an example showing how to create a new single-line text field in a form. * </p> * <pre> * Form form = pdf.getForm(); * FormText text = new FormText(pdf.getLastPage(), 100,100,300,120); * form.addElement("AccountNumber", text);</pre> * <p>And here's how to extract the value from an existing text field in a form</p> * <pre> * Form form = pdf.getForm(); * FormText text = (FormText)form.getElement("AccountNumber"); * String account = text.getValue();</pre> * <p> * To add validation to a field isn't difficult either - here's how to use * two of the built-in JavaScript methods in Adobe Acrobat to limit the keypresses * in the field to digits only, and to limit the final value to between 1930 and 1985. * </p> * <pre> * FormText text = new FormText(pdf.getLastPage(), 100,100,300,120); * PDFAnnotation annot = text.getAnnotations()[0]; * PDFAction onkey = PDFAction.formJavaScript("AFNumber_Keystroke(0,1,1,0,'',true);"); * PDFAction onchg = PDFAction.formJavaScript("AFRange_Validate(true,1930,true,1985);"); * annot.setEventAction(PDFAnnotation.EVENT_ONKEYPRESS, onkey); * annot.setEventAction(PDFAnnotation.EVENT_ONCHANGE, onchg); * </pre> * @since 1.1.23 */public final class FormText extends FormElement{ FormText(org.faceless.pdf2.FormText b) { super(b); } /** * Create a new FormText element. With this constructor the page annotation * must be positioned explicitly by calling the {@link PDFAnnotation#setPage} * and {@link PDFAnnotation#setRectangle} methods. * @since 1.1.26 */ public FormText() { this(null,0,0,0,0); } /** * Create a new FormText element and place it in the specified location * @param page the page to place the field on * @param x1 the left-most X co-ordinate of the field * @param y1 the top-most Y co-ordinate of the field * @param x2 the right-most X co-ordinate of the field * @param x2 the bottom-most Y co-ordinate of the field */ public FormText(PDFPage page, float x1, float y1, float x2, float y2) { super(new org.faceless.pdf2.FormText(page==null ? null : page.page, x1, y1, x2, y2)); } /** * <p> * Set the style of the field. The two parameters control the * style of the text and the background style of the field. The text * style must be specified, and must define a font and a fill color. * The font must be a {@link StandardFont} for Acrobat 4 compatibility, * or a {@link TrueTypeFont} or {@link StandardCJKFont} for Acrobat 5. * The background style may specify a line and/or fill color as well as * other options like * {@link PDFStyle#setLineWeighting line thickness} or * {@link PDFStyle#setFormStyle field style}, or may be <code>null</code> * to use the defaults. * </p><p> * The {@link PDFStyle#setTextAlign alignment} of the text style may * be left (the default), centered or right. * </p> * @param text the style to draw the text of the button in * @param background the style to draw the background of the button in * @throws IllegalArgumentException if text is <tt>null</tt> or doesn't * meet the criteria above * @see #setValue * @see PDFStyle#setFormStyle */ public void setStyle(PDFStyle text, PDFStyle background) { List l = element.getAnnotations(); for (int i=0;i<l.size();i++) { org.faceless.pdf2.WidgetAnnotation annot = element.getAnnotation(i); annot.setTextStyle(text==null ? null : text.style); annot.setBackgroundStyle(background==null ? null : background.style); } } /** * Set whether this text field is a multiline text field or not. * Newly created text fields are single line fields by default. * A field can only be one of password, multiline or a filename. * @param multiline whether to make this field a multiline field */ public void setMultiline(boolean multiline) { ((org.faceless.pdf2.FormText)element).setType(multiline ? org.faceless.pdf2.FormText.TYPE_MULTILINE : org.faceless.pdf2.FormText.TYPE_NORMAL); } /** * Get whether this field is a multiline text field or not. * @return true if this text field is a multiline field * @see #setMultiline */ public boolean isMultiline() { return ((org.faceless.pdf2.FormText)element).getType()==org.faceless.pdf2.FormText.TYPE_MULTILINE; } /** * For multiline text fields, set whether the field can be scrolled * to enter more text than can be displayed in the form. The default * value is <code>true</code>. For non-multiline text fields this * method has no effect * @see #isMultilineScrollable * @since 1.2.1 */ public void setMultilineScrollable(boolean scrollable) { if (isMultiline()) { ((org.faceless.pdf2.FormText)element).setScrollable(scrollable); } } /** * Get whether this multiline field is scrollable or not. For single * line text fields, this method always returns false * @see #setMultilineScrollable * @since 1.2.1 */ public boolean isMultilineScrollable() { return ((org.faceless.pdf2.FormText)element).getType()==org.faceless.pdf2.FormText.TYPE_MULTILINE && ((org.faceless.pdf2.FormText)element).isScrollable(); } /** * Set whether this text field is a password text field or not (the default). * Password fields never return a value from {@link #getValue}, as the value * is never saved in the file. * A field can only be one of password, multiline or a filename. * @param password whether to make this field a password field */ public void setPassword(boolean password) { ((org.faceless.pdf2.FormText)element).setType(password ? org.faceless.pdf2.FormText.TYPE_PASSWORD : org.faceless.pdf2.FormText.TYPE_NORMAL); } /** * Get whether this field is a password field or not. * @return true if this text field is a password field * @see #setPassword */ public boolean isPassword() { return ((org.faceless.pdf2.FormText)element).getType()==org.faceless.pdf2.FormText.TYPE_PASSWORD; } /** * Set whether this field represents the name of a file, or not (the * default). This feature, which is ignored in viewers prior to Acrobat * 5.0 (PDF 1.4), causes the field to be treated as the name of a file, * the contents of which are uploaded as the value of the field when the * form is submitted. Earlier viewers will presumably just submit the * filename as entered. A field can only be one of password, multiline * or a filename. * @param filename whether this field is a filename field */ public void setFilename(boolean filename) { ((org.faceless.pdf2.FormText)element).setType(filename ? org.faceless.pdf2.FormText.TYPE_FILESELECT : org.faceless.pdf2.FormText.TYPE_NORMAL); } /** * Get whether this field is a filename field or not. * @return true if this text field is a filename field * @see #setFilename */ public boolean isFilename() { return ((org.faceless.pdf2.FormText)element).getType()==org.faceless.pdf2.FormText.TYPE_FILESELECT; } /** * Set the maximum length of the field. Passing in a value of * zero sets the field to have no maximum length, which is the default. * @param maxlen the maximum number of characters in the field, or zero for no maximum */ public void setMaxLength(int maxlen) { ((org.faceless.pdf2.FormText)element).setMaxLength(maxlen); } /** * Return the maximum size of the text field, or zero if there is no maximum * @return the maximum length of the text field, or zero for no maximum */ public int getMaxLength() { return ((org.faceless.pdf2.FormText)element).getMaxLength(); } /** * <p> * Set the value of the text field. The specified text may only contain * newlines if the {@link #isMultiline} method returns true. If the * {@link #isPassword} method returns true, this method throws an * <code>IllegalStateException</code> - passwords may not be stored in * the document, but can only be entered from Acrobat. * </p> * <p> * Note setting fields this way will bypass any JavaScript validation * that may be in effect. * </p> * @param value the value to set the text field to * @throws IllegalArgumentException if the value contains newlines and the * field is not a multiline field, or if the length is longer than * <tt>getMaxLength()</tt> * @throws IllegalStateException if the field is a password field */ public void setValue(String value) throws IllegalArgumentException, IllegalStateException { ((org.faceless.pdf2.FormText)element).setValue(value); } /** * <p> * Set the default value of the text field. This is the value the field * is set to if the form is reset. It does not have to be specified. * </p> * @param value the new default value of the text field * @throws IllegalArgumentException if the value contains newlines and the field is not a multiline text field, or if the string length is longer than <tt>getMaxLength()</tt> */ public void setDefaultValue(String value) { ((org.faceless.pdf2.FormText)element).setDefaultValue(value); } /** * Return the value of the text field, or <code>null</code> if no * value has been specified. * @return the value of the text field or <code>null</code> if no value is set */ public String getValue() { return ((org.faceless.pdf2.FormText)element).getValue(); } /** * Return the default value of the text field, or <code>null</code> if no * default value has been specified. * @return the default value of the text field or <code>null</code> if no default is set */ public String getDefaultValue() { return ((org.faceless.pdf2.FormText)element).getDefaultValue(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -