⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 formchoice.java

📁 Java生成PDF Java生成PDF Java生成PDF
💻 JAVA
字号:
// $Id: FormChoice.java,v 1.4 2003/10/06 12:40:06 mike Exp $package org.faceless.pdf;import java.util.*;import java.awt.Color;/** * A type of form element representing a list of values in the form * of a scrollable or drop-down list - the same as an HTML <code>&lt;select&gt;</code> * </p><p> * There are three different types of choice field - a "scrollable" list, which * as the name suggests appears as a scrolling window over a list of values; a * "dropdown" list, where the list looks like a drop-down menu, or a "combo", * which is identical to a dropdown list but allows the value to be typed in directly * as well as being selected from the list. * </p><p> * Each drop down list may have one or more values, which are set by adding * values to the {@link java.util.Map} returned by {@link #getOptions}. The * values are displayed in the order they're added to the map. * </p><p> * Here's an example creating a simple list of values: * </p> * <pre> *   Form form = pdf.getForm(); *   FormChoice colors = new FormChoice(FormChoice.TYPE_SCROLLABLE, page, 100,100,300,300); *   Map vals = colors.getOptions(); *   vals.put("Red", null); *   vals.put("Green", null); *   vals.put("Blue", null); *   colors.setValue("Green"); *   form.addElement("FavoriteColor", colors); * </pre> * <p>and here's an example showing how to retrieve the value of an element</p> * <pre> *   Form form = pdf.getForm(); *   FormChoice choice = (FormChoice)form.getElement("FavoriteColor"); *   String value = choice.getValue(); * </pre> *  * @since 1.1.23 */public final class FormChoice extends FormElement{    FormChoice(org.faceless.pdf2.FormChoice b)    {        super(b);    }    /**     * A type passed to the constructor representing a dropdown list,     * similar to a drop-down menu     */    public static final int TYPE_DROPDOWN=131272;    /**     * A type passed to the constructor representing a scollable list, which     * displays one or more lines at once.     */    public static final int TYPE_SCROLLABLE=0;    /**     * A type passed to the constructor representing a dropdown list where     * the value can also be edited like a text field.     */    public static final int TYPE_COMBO=393216;    /**     * Create a new FormChoice element. With this constructor the page annotation     * must be positioned explicitly by calling the {@link PDFAnnotation#setPage}     * and {@link PDFAnnotation#setRectangle} methods.     * @param type one of {@link #TYPE_DROPDOWN}, {@link #TYPE_SCROLLABLE} or {@link #TYPE_COMBO}     * @since 1.1.26     */    public FormChoice(int type)    {	this(null, type, 0,0,0,0);    }    /**     * Create a new FormChoice     * @param page the page to place the field on     * @param type one of {@link #TYPE_DROPDOWN}, {@link #TYPE_SCROLLABLE} or {@link #TYPE_COMBO}     * @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 FormChoice(PDFPage page, int type, float x1, float y1, float x2, float y2)    {	super(new org.faceless.pdf2.FormChoice(type==TYPE_COMBO ? org.faceless.pdf2.FormChoice.TYPE_COMBO : (type==TYPE_SCROLLABLE ? org.faceless.pdf2.FormChoice.TYPE_SCROLLABLE : org.faceless.pdf2.FormChoice.TYPE_DROPDOWN), 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 should be a {@link StandardFont} for Acrobat 4 compatibility,     * or a {@link TrueTypeFont} or {@link StandardCJKFont} for Acrobat 5),     * and the background style may specify a line and/or fill color, or may     * be <code>null</code> to use the defaults.     * </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 #getOptions     * @see #getValue     * @see PDFStyle#setFormStyle     */    public void setStyle(PDFStyle text, PDFStyle background)        throws IllegalArgumentException    {	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);	}    }    /**     * Return the type of choice field this object represents -     * one of {@link #TYPE_SCROLLABLE}, {@link #TYPE_DROPDOWN} or {@link #TYPE_COMBO}     * @return the type of choice field     */    public int getType()    {	int style = ((org.faceless.pdf2.FormChoice)element).getType();	int oldtype=TYPE_DROPDOWN;	if (style==org.faceless.pdf2.FormChoice.TYPE_COMBO) oldtype=TYPE_COMBO;	if (style==org.faceless.pdf2.FormChoice.TYPE_SCROLLABLE) oldtype=TYPE_SCROLLABLE;	return oldtype;    }    /**     * <p>     * Return a <code>Map</code> listing the values available in this     * choice field.     * </p><p>     * A <code>Map</code> contains keys and their corresponding values,     * which is the way the choice fields are done in PDF (and HTML too). They     * key is displayed to the user, and the value, if specified, is what's     * included when the form is submitted. So, for example, the line     * <code>choice.getOptions().put("Red", "1")</code> will display the value     * "Red" on screen, but send the value of "1" when the form is submitted.     * If the submitted value should be the same as the displayed value, just     * do <code>choice.getOptions().put("Red", null)</code>.     * </p><p>     * Values are displayed in the field in the order that they are added to     * the list, <code>key</code> may not be null, and both key and value must     * be Strings, otherwise an {@link java.lang.IllegalArgumentException} is thrown     * when the values are added to the list.     * </p>     * @return a Map which should be modified to set the values of the choice field     */    public Map getOptions()    {	return ((org.faceless.pdf2.FormChoice)element).getOptions();    }    /**     * <p>     * Set the value of the choice field. This is the currently selected entry, and     * must be in the list of all values returned from {@link #getOptions} unless     * the field is a combo-type field. The value may be <code>null</code> to select     * no entry at all.     * </p>     * @param value the value to set the choice field to     */     public void setValue(String value)    {	((org.faceless.pdf2.FormChoice)element).setValue(value);    }    /**     * Return the current value of the choice field if set, or <code>null</code>     * otherwise.     * @return the value of the field     * @see #setValue     */    public String getValue()    {	return ((org.faceless.pdf2.FormChoice)element).getValue();    }    /**      * Set the default value of the choice field, which is what the     * value is set to if the form is reset. A default value is optional.     * The value may be <code>null</code> to specify no default     * @param value the default value of the field     */    public void setDefaultValue(String value)    {	((org.faceless.pdf2.FormChoice)element).setDefaultValue(value);    }    /**     * Return the default value of the choice field if set, or <code>null</code>     * otherwise.     * @return the default value of the field     * @see #setDefaultValue     */    public String getDefaultValue()    {	return ((org.faceless.pdf2.FormChoice)element).getDefaultValue();    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -