📄 radiocheckfield.java
字号:
/*
* Copyright 2005 by Paulo Soares.
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code is 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://www.lowagie.com/iText/
*/
package com.lowagie.text.pdf;
import java.io.IOException;
import com.lowagie.text.DocumentException;
import com.lowagie.text.ExceptionConverter;
import com.lowagie.text.Rectangle;
/**
* Creates a radio or a check field.
* <p>
* Example usage:
* <p>
* <PRE>
* Document document = new Document(PageSize.A4, 50, 50, 50, 50);
* PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("output.pdf"));
* document.open();
* PdfContentByte cb = writer.getDirectContent();
* RadioCheckField bt = new RadioCheckField(writer, new Rectangle(100, 100, 200, 200), "radio", "v1");
* bt.setCheckType(RadioCheckField.TYPE_CIRCLE);
* bt.setBackgroundColor(Color.cyan);
* bt.setBorderStyle(PdfBorderDictionary.STYLE_SOLID);
* bt.setBorderColor(Color.red);
* bt.setTextColor(Color.yellow);
* bt.setBorderWidth(BaseField.BORDER_WIDTH_THICK);
* bt.setChecked(false);
* PdfFormField f1 = bt.getRadioField();
* bt.setOnValue("v2");
* bt.setChecked(true);
* bt.setBox(new Rectangle(100, 300, 200, 400));
* PdfFormField f2 = bt.getRadioField();
* bt.setChecked(false);
* PdfFormField top = bt.getRadioGroup(true, false);
* bt.setOnValue("v3");
* bt.setBox(new Rectangle(100, 500, 200, 600));
* PdfFormField f3 = bt.getRadioField();
* top.addKid(f1);
* top.addKid(f2);
* top.addKid(f3);
* writer.addAnnotation(top);
* bt = new RadioCheckField(writer, new Rectangle(300, 300, 400, 400), "check1", "Yes");
* bt.setCheckType(RadioCheckField.TYPE_CHECK);
* bt.setBorderWidth(BaseField.BORDER_WIDTH_THIN);
* bt.setBorderColor(Color.black);
* bt.setBackgroundColor(Color.white);
* PdfFormField ck = bt.getCheckField();
* writer.addAnnotation(ck);
* document.close();
* </PRE>
* @author Paulo Soares (psoares@consiste.pt)
*/
public class RadioCheckField extends BaseField {
/** A field with the symbol check */
public static final int TYPE_CHECK = 1;
/** A field with the symbol circle */
public static final int TYPE_CIRCLE = 2;
/** A field with the symbol cross */
public static final int TYPE_CROSS = 3;
/** A field with the symbol diamond */
public static final int TYPE_DIAMOND = 4;
/** A field with the symbol square */
public static final int TYPE_SQUARE = 5;
/** A field with the symbol star */
public static final int TYPE_STAR = 6;
private static String typeChars[] = {"4", "l", "8", "u", "n", "H"};
/**
* Holds value of property checkType.
*/
private int checkType;
/**
* Holds value of property onValue.
*/
private String onValue;
/**
* Holds value of property checked.
*/
private boolean checked;
/**
* Creates a new instance of RadioCheckField
* @param writer the document <CODE>PdfWriter</CODE>
* @param box the field location and dimensions
* @param fieldName the field name. It must not be <CODE>null</CODE>
* @param onValue the value when the field is checked
*/
public RadioCheckField(PdfWriter writer, Rectangle box, String fieldName, String onValue) {
super(writer, box, fieldName);
setOnValue(onValue);
setCheckType(TYPE_CIRCLE);
}
/**
* Getter for property checkType.
* @return Value of property checkType.
*/
public int getCheckType() {
return this.checkType;
}
/**
* Sets the checked symbol. It can be
* <CODE>TYPE_CHECK</CODE>,
* <CODE>TYPE_CIRCLE</CODE>,
* <CODE>TYPE_CROSS</CODE>,
* <CODE>TYPE_DIAMOND</CODE>,
* <CODE>TYPE_SQUARE</CODE> and
* <CODE>TYPE_STAR</CODE>.
* @param checkType the checked symbol
*/
public void setCheckType(int checkType) {
if (checkType < TYPE_CHECK || checkType > TYPE_STAR)
checkType = TYPE_CIRCLE;
this.checkType = checkType;
setText(typeChars[checkType - 1]);
try {
setFont(BaseFont.createFont(BaseFont.ZAPFDINGBATS, BaseFont.WINANSI, false));
}
catch (Exception e) {
throw new ExceptionConverter(e);
}
}
/**
* Getter for property onValue.
* @return Value of property onValue.
*/
public String getOnValue() {
return this.onValue;
}
/**
* Sets the value when the field is checked.
* @param onValue the value when the field is checked
*/
public void setOnValue(String onValue) {
this.onValue = onValue;
}
/**
* Getter for property checked.
* @return Value of property checked.
*/
public boolean isChecked() {
return this.checked;
}
/**
* Sets the state of the field to checked or unchecked.
* @param checked the state of the field, <CODE>true</CODE> for checked
* and <CODE>false</CODE> for unchecked
*/
public void setChecked(boolean checked) {
this.checked = checked;
}
/**
* Gets the field appearance.
* @param isRadio <CODE>true</CODE> for a radio field and <CODE>false</CODE>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -