📄 form.java
字号:
// $Id: Form.java,v 1.3 2003/10/06 12:40:06 mike Exp $package org.faceless.pdf;import java.util.*;import java.io.*;import java.awt.Color;/** * <p> * The Form class represents the interactive Form that may be included as part * of a PDF document. This form can be filled out by users and then eventually * submitted for processing in the same way as an HTML form. Unlike HTML, a * PDF document may only have one form. * </p><p> * A form contains zero or more {@link FormElement} objects, each one of which * usually has one or more visual representation on the page, in the form of * a {@link PDFAnnotation} (the exception is the {@link FormSignature} * class, which currently cannot have a visual appearance). Each element in the * form is stored under a name, which is used to reference the element and must * be unique. * </p><p> * The name may be a simple string, like <code>"Element1"</code>, or it may be * a <i>compound</i> name, with fields separated with dots, for example * <code>"Employee.Address.City"</code>. Simple and Compound names must not * collide - for example, it would be illegal to have elements called * "Country.Capital" and "Country" in the same document. * </p> * <p> * <b>Note that using interactive forms requires the "Extended Edition" * of the library</b> - although the classes are supplied with the package an * "Extended Edition" license must be purchased to activate this functionality. * </p> * * @see PDF#getForm * @see FormElement * @since 1.1.13 */public final class Form extends PeeredObject{ private final org.faceless.pdf2.Form form; private PDFStyle backstyle; private int radio=FormElement.STYLE_CIRCLE, check=FormElement.STYLE_CHECK; Form(org.faceless.pdf2.Form form) { this.form=form; } Object getPeer() { return form; } /** * <p> * Add an element to the form. Although a form can contain as many elements * as you like, currently only a single signature with a state of * {@link FormSignature#STATE_PENDING} can be added to each document. * </p> * @param name the name of the form element * @param element the element to add to the form * @throws IllegalStateException if the element already exists in the form */ public void addElement(String name, FormElement element) { form.addElement(name, element.element); } /** * Return the specified element from the form. * @param name the name of the form element * @return the specified element, or <code>null</code> if it doesn't exist */ public FormElement getElement(String name) { return (FormElement)PeeredObject.getPeer(form.getElement(name)); } /** * Remove the specified element from the form, if it exists. * @param name the name of the form element */ public void removeElement(String name) { form.removeElement(name); } /** * Rename an element in the form. If the specified element * name does not exist, an <code>IllegalArgumentException</code> * is thrown * @param fromname the original name of the form element * @param toname the new name of the form element * @since 1.1.23 */ public void renameElement(String fromname, String toname) { form.renameElement(fromname,toname); } /** * Remove all the elements from the form * @since 1.2.1 */ public void clear() { form.clear(); } /** * Return a map of all the elements in the form. Each key * in the Map is a {@link java.lang.String} representing the name of the * element, and the corresponding value is the {@link FormElement}. The * returned map is unmodifiable - changes are not allowed. * @return an unmodifiable <code>Map</code> containing all the form elements */ public Map getElements() { Map out = new org.faceless.util.OrderedMap(); Map in = form.getElements(); for (Iterator i = in.entrySet().iterator();i.hasNext();) { Map.Entry e = (Map.Entry)i.next(); out.put(e.getKey(), PeeredObject.getPeer(e.getValue())); } return Collections.unmodifiableMap(out); } /** * Given a <code>FormElement</code>, return the name by which * this element is stored in the form, or <code>null</code> if * it doesn't exist. * @return the name of this element or <code>null</code> if it's not in the Form */ public String getName(FormElement element) { return form.getName(element.element); } /** * Set the default background style for all new elements * added to the form. This can be overridden by the <code>setStyle</code> * method of each <code>FormElement</code>. The default is a white * background with a plain black border * @since 1.1.23 * @param style the default background style for new form elements * @see PDFStyle#setFormStyle */ public void setBackgroundStyle(PDFStyle style) { org.faceless.pdf2.PDFStyle newstyle = style.style; char newcheck, newradio; if (check==FormElement.STYLE_CIRCLE) newcheck=newstyle.FORMRADIOBUTTONSTYLE_CIRCLE; else if (check==FormElement.STYLE_CROSS) newcheck=newstyle.FORMRADIOBUTTONSTYLE_CROSS; else if (check==FormElement.STYLE_DIAMOND) newcheck=newstyle.FORMRADIOBUTTONSTYLE_DIAMOND; else if (check==FormElement.STYLE_SQUARE) newcheck=newstyle.FORMRADIOBUTTONSTYLE_SQUARE; else if (check==FormElement.STYLE_STAR) newcheck=newstyle.FORMRADIOBUTTONSTYLE_STAR; else newcheck=newstyle.FORMRADIOBUTTONSTYLE_CHECK; if (radio==FormElement.STYLE_CIRCLE) newradio=newstyle.FORMRADIOBUTTONSTYLE_CIRCLE; else if (radio==FormElement.STYLE_CROSS) newradio=newstyle.FORMRADIOBUTTONSTYLE_CROSS; else if (radio==FormElement.STYLE_DIAMOND) newradio=newstyle.FORMRADIOBUTTONSTYLE_DIAMOND; else if (radio==FormElement.STYLE_SQUARE) newradio=newstyle.FORMRADIOBUTTONSTYLE_SQUARE; else if (radio==FormElement.STYLE_STAR) newradio=newstyle.FORMRADIOBUTTONSTYLE_STAR; else newradio=newstyle.FORMRADIOBUTTONSTYLE_CHECK; newstyle.setFormRadioButtonStyle(newradio); newstyle.setFormCheckboxStyle(newcheck); form.setBackgroundStyle(newstyle); this.backstyle=style; } /** * Set the default text style for all new elements added * to the form that contain text (the {@link FormText}, {@link FormChoice} * and {@link FormButton} classes). The style must include a font, which * is an instance of {@link StandardFont} or {@link TrueTypeFont}, and fill * color to draw the text in. Note that some TrueType fonts caused problems * when used in text boxes in Acrobat 4.0, so for maximum compatibility we * recommend using a Standard font. The default is Black 11pt Helvetica. * @param style the default text style for new form elements * @since 1.1.23 */ public void setTextStyle(PDFStyle style) { form.setTextStyle(style.style); } /** * Set the default style for any {@link FormRadioButton} and * {@link FormCheckbox} elements added to the form. The values * are one of {@link FormElement#STYLE_CIRCLE}, {@link FormElement#STYLE_CHECK} * {@link FormElement#STYLE_SQUARE}, {@link FormElement#STYLE_CROSS}, * {@link FormElement#STYLE_DIAMOND} or {@link FormElement#STYLE_STAR}. * The defaults are <code>STYLE_CHECK</code> for checkboxes and * <code>STYLE_CIRCLE</code> for radiobuttons. * @param checkbox the default style for new checkboxes * @param radiobutton the default style for new radiobuttons * @since 1.1.23 */ public void setButtonStyle(int checkbox, int radiobutton) { this.check=checkbox; this.radio=radiobutton; setBackgroundStyle(backstyle); } public String toString() { Map m = getElements(); String s = "<form>\n"; for (Iterator i = m.keySet().iterator();i.hasNext();) { String name = (String)i.next(); FormElement val = (FormElement)m.get(name); String z = val.toString(); int j = Math.min(z.indexOf(' '), z.indexOf('>')); z = z.substring(0,j)+" name=\""+name+"\""+z.substring(j); s+=" "+z+"\n"; } return s+"</form>"; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -