001: /*
002: * Copyright 2005 by Paulo Soares.
003: *
004: * The contents of this file are subject to the Mozilla Public License Version 1.1
005: * (the "License"); you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
007: *
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
010: * for the specific language governing rights and limitations under the License.
011: *
012: * The Original Code is 'iText, a free JAVA-PDF library'.
013: *
014: * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
015: * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
016: * All Rights Reserved.
017: * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
018: * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
019: *
020: * Contributor(s): all the names of the contributors are added in the source code
021: * where applicable.
022: *
023: * Alternatively, the contents of this file may be used under the terms of the
024: * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
025: * provisions of LGPL are applicable instead of those above. If you wish to
026: * allow use of your version of this file only under the terms of the LGPL
027: * License and not to allow others to use your version of this file under
028: * the MPL, indicate your decision by deleting the provisions above and
029: * replace them with the notice and other provisions required by the LGPL.
030: * If you do not delete the provisions above, a recipient may use your version
031: * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
032: *
033: * This library is free software; you can redistribute it and/or modify it
034: * under the terms of the MPL as stated above or under the terms of the GNU
035: * Library General Public License as published by the Free Software Foundation;
036: * either version 2 of the License, or any later version.
037: *
038: * This library is distributed in the hope that it will be useful, but WITHOUT
039: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
040: * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
041: * details.
042: *
043: * If you didn't download this code from the following link, you should check if
044: * you aren't using an obsolete version:
045: * http://www.lowagie.com/iText/
046: */
047: package com.lowagie.text.pdf;
048:
049: import java.io.IOException;
050:
051: import com.lowagie.text.DocumentException;
052: import com.lowagie.text.ExceptionConverter;
053: import com.lowagie.text.Rectangle;
054:
055: /**
056: * Creates a radio or a check field.
057: * <p>
058: * Example usage:
059: * <p>
060: * <PRE>
061: * Document document = new Document(PageSize.A4, 50, 50, 50, 50);
062: * PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("output.pdf"));
063: * document.open();
064: * PdfContentByte cb = writer.getDirectContent();
065: * RadioCheckField bt = new RadioCheckField(writer, new Rectangle(100, 100, 200, 200), "radio", "v1");
066: * bt.setCheckType(RadioCheckField.TYPE_CIRCLE);
067: * bt.setBackgroundColor(Color.cyan);
068: * bt.setBorderStyle(PdfBorderDictionary.STYLE_SOLID);
069: * bt.setBorderColor(Color.red);
070: * bt.setTextColor(Color.yellow);
071: * bt.setBorderWidth(BaseField.BORDER_WIDTH_THICK);
072: * bt.setChecked(false);
073: * PdfFormField f1 = bt.getRadioField();
074: * bt.setOnValue("v2");
075: * bt.setChecked(true);
076: * bt.setBox(new Rectangle(100, 300, 200, 400));
077: * PdfFormField f2 = bt.getRadioField();
078: * bt.setChecked(false);
079: * PdfFormField top = bt.getRadioGroup(true, false);
080: * bt.setOnValue("v3");
081: * bt.setBox(new Rectangle(100, 500, 200, 600));
082: * PdfFormField f3 = bt.getRadioField();
083: * top.addKid(f1);
084: * top.addKid(f2);
085: * top.addKid(f3);
086: * writer.addAnnotation(top);
087: * bt = new RadioCheckField(writer, new Rectangle(300, 300, 400, 400), "check1", "Yes");
088: * bt.setCheckType(RadioCheckField.TYPE_CHECK);
089: * bt.setBorderWidth(BaseField.BORDER_WIDTH_THIN);
090: * bt.setBorderColor(Color.black);
091: * bt.setBackgroundColor(Color.white);
092: * PdfFormField ck = bt.getCheckField();
093: * writer.addAnnotation(ck);
094: * document.close();
095: * </PRE>
096: * @author Paulo Soares (psoares@consiste.pt)
097: */
098: public class RadioCheckField extends BaseField {
099:
100: /** A field with the symbol check */
101: public static final int TYPE_CHECK = 1;
102: /** A field with the symbol circle */
103: public static final int TYPE_CIRCLE = 2;
104: /** A field with the symbol cross */
105: public static final int TYPE_CROSS = 3;
106: /** A field with the symbol diamond */
107: public static final int TYPE_DIAMOND = 4;
108: /** A field with the symbol square */
109: public static final int TYPE_SQUARE = 5;
110: /** A field with the symbol star */
111: public static final int TYPE_STAR = 6;
112:
113: private static String typeChars[] = { "4", "l", "8", "u", "n", "H" };
114:
115: /**
116: * Holds value of property checkType.
117: */
118: private int checkType;
119:
120: /**
121: * Holds value of property onValue.
122: */
123: private String onValue;
124:
125: /**
126: * Holds value of property checked.
127: */
128: private boolean checked;
129:
130: /**
131: * Creates a new instance of RadioCheckField
132: * @param writer the document <CODE>PdfWriter</CODE>
133: * @param box the field location and dimensions
134: * @param fieldName the field name. It must not be <CODE>null</CODE>
135: * @param onValue the value when the field is checked
136: */
137: public RadioCheckField(PdfWriter writer, Rectangle box,
138: String fieldName, String onValue) {
139: super (writer, box, fieldName);
140: setOnValue(onValue);
141: setCheckType(TYPE_CIRCLE);
142: }
143:
144: /**
145: * Getter for property checkType.
146: * @return Value of property checkType.
147: */
148: public int getCheckType() {
149: return this .checkType;
150: }
151:
152: /**
153: * Sets the checked symbol. It can be
154: * <CODE>TYPE_CHECK</CODE>,
155: * <CODE>TYPE_CIRCLE</CODE>,
156: * <CODE>TYPE_CROSS</CODE>,
157: * <CODE>TYPE_DIAMOND</CODE>,
158: * <CODE>TYPE_SQUARE</CODE> and
159: * <CODE>TYPE_STAR</CODE>.
160: * @param checkType the checked symbol
161: */
162: public void setCheckType(int checkType) {
163: if (checkType < TYPE_CHECK || checkType > TYPE_STAR)
164: checkType = TYPE_CIRCLE;
165: this .checkType = checkType;
166: setText(typeChars[checkType - 1]);
167: try {
168: setFont(BaseFont.createFont(BaseFont.ZAPFDINGBATS,
169: BaseFont.WINANSI, false));
170: } catch (Exception e) {
171: throw new ExceptionConverter(e);
172: }
173: }
174:
175: /**
176: * Getter for property onValue.
177: * @return Value of property onValue.
178: */
179: public String getOnValue() {
180: return this .onValue;
181: }
182:
183: /**
184: * Sets the value when the field is checked.
185: * @param onValue the value when the field is checked
186: */
187: public void setOnValue(String onValue) {
188: this .onValue = onValue;
189: }
190:
191: /**
192: * Getter for property checked.
193: * @return Value of property checked.
194: */
195: public boolean isChecked() {
196: return this .checked;
197: }
198:
199: /**
200: * Sets the state of the field to checked or unchecked.
201: * @param checked the state of the field, <CODE>true</CODE> for checked
202: * and <CODE>false</CODE> for unchecked
203: */
204: public void setChecked(boolean checked) {
205: this .checked = checked;
206: }
207:
208: /**
209: * Gets the field appearance.
210: * @param isRadio <CODE>true</CODE> for a radio field and <CODE>false</CODE>
211: * for a check field
212: * @param on <CODE>true</CODE> for the checked state, <CODE>false</CODE>
213: * otherwise
214: * @throws IOException on error
215: * @throws DocumentException on error
216: * @return the appearance
217: */
218: public PdfAppearance getAppearance(boolean isRadio, boolean on)
219: throws IOException, DocumentException {
220: if (isRadio && checkType == TYPE_CIRCLE)
221: return getAppearanceRadioCircle(on);
222: PdfAppearance app = getBorderAppearance();
223: if (!on)
224: return app;
225: BaseFont ufont = getRealFont();
226: boolean borderExtra = borderStyle == PdfBorderDictionary.STYLE_BEVELED
227: || borderStyle == PdfBorderDictionary.STYLE_INSET;
228: float h = box.getHeight() - borderWidth * 2;
229: float bw2 = borderWidth;
230: if (borderExtra) {
231: h -= borderWidth * 2;
232: bw2 *= 2;
233: }
234: float offsetX = (borderExtra ? 2 * borderWidth : borderWidth);
235: offsetX = Math.max(offsetX, 1);
236: float offX = Math.min(bw2, offsetX);
237: float wt = box.getWidth() - 2 * offX;
238: float ht = box.getHeight() - 2 * offX;
239: float fsize = fontSize;
240: if (fsize == 0) {
241: float bw = ufont.getWidthPoint(text, 1);
242: if (bw == 0)
243: fsize = 12;
244: else
245: fsize = wt / bw;
246: float nfsize = h
247: / (ufont.getFontDescriptor(BaseFont.ASCENT, 1));
248: fsize = Math.min(fsize, nfsize);
249: }
250: app.saveState();
251: app.rectangle(offX, offX, wt, ht);
252: app.clip();
253: app.newPath();
254: if (textColor == null)
255: app.resetGrayFill();
256: else
257: app.setColorFill(textColor);
258: app.beginText();
259: app.setFontAndSize(ufont, fsize);
260: app.setTextMatrix((box.getWidth() - ufont.getWidthPoint(text,
261: fsize)) / 2, (box.getHeight() - ufont.getAscentPoint(
262: text, fsize)) / 2);
263: app.showText(text);
264: app.endText();
265: app.restoreState();
266: return app;
267: }
268:
269: /**
270: * Gets the special field appearance for the radio circle.
271: * @param on <CODE>true</CODE> for the checked state, <CODE>false</CODE>
272: * otherwise
273: * @return the appearance
274: */
275: public PdfAppearance getAppearanceRadioCircle(boolean on) {
276: PdfAppearance app = PdfAppearance.createAppearance(writer, box
277: .getWidth(), box.getHeight());
278: switch (rotation) {
279: case 90:
280: app.setMatrix(0, 1, -1, 0, box.getHeight(), 0);
281: break;
282: case 180:
283: app
284: .setMatrix(-1, 0, 0, -1, box.getWidth(), box
285: .getHeight());
286: break;
287: case 270:
288: app.setMatrix(0, -1, 1, 0, 0, box.getWidth());
289: break;
290: }
291: Rectangle box = new Rectangle(app.getBoundingBox());
292: float cx = box.getWidth() / 2;
293: float cy = box.getHeight() / 2;
294: float r = (Math.min(box.getWidth(), box.getHeight()) - borderWidth) / 2;
295: if (r <= 0)
296: return app;
297: if (backgroundColor != null) {
298: app.setColorFill(backgroundColor);
299: app.circle(cx, cy, r + borderWidth / 2);
300: app.fill();
301: }
302: if (borderWidth > 0 && borderColor != null) {
303: app.setLineWidth(borderWidth);
304: app.setColorStroke(borderColor);
305: app.circle(cx, cy, r);
306: app.stroke();
307: }
308: if (on) {
309: if (textColor == null)
310: app.resetGrayFill();
311: else
312: app.setColorFill(textColor);
313: app.circle(cx, cy, r / 2);
314: app.fill();
315: }
316: return app;
317: }
318:
319: /**
320: * Gets a radio group. It's composed of the field specific keys, without the widget
321: * ones. This field is to be used as a field aggregator with {@link PdfFormField#addKid(PdfFormField) addKid()}.
322: * @param noToggleToOff if <CODE>true</CODE>, exactly one radio button must be selected at all
323: * times; clicking the currently selected button has no effect.
324: * If <CODE>false</CODE>, clicking
325: * the selected button deselects it, leaving no button selected.
326: * @param radiosInUnison if <CODE>true</CODE>, a group of radio buttons within a radio button field that
327: * use the same value for the on state will turn on and off in unison; that is if
328: * one is checked, they are all checked. If <CODE>false</CODE>, the buttons are mutually exclusive
329: * (the same behavior as HTML radio buttons)
330: * @return the radio group
331: */
332: public PdfFormField getRadioGroup(boolean noToggleToOff,
333: boolean radiosInUnison) {
334: PdfFormField field = PdfFormField.createRadioButton(writer,
335: noToggleToOff);
336: if (radiosInUnison)
337: field.setFieldFlags(PdfFormField.FF_RADIOSINUNISON);
338: field.setFieldName(fieldName);
339: if ((options & READ_ONLY) != 0)
340: field.setFieldFlags(PdfFormField.FF_READ_ONLY);
341: if ((options & REQUIRED) != 0)
342: field.setFieldFlags(PdfFormField.FF_REQUIRED);
343: field.setValueAsName(checked ? onValue : "Off");
344: return field;
345: }
346:
347: /**
348: * Gets the radio field. It's only composed of the widget keys and must be used
349: * with {@link #getRadioGroup(boolean,boolean)}.
350: * @return the radio field
351: * @throws IOException on error
352: * @throws DocumentException on error
353: */
354: public PdfFormField getRadioField() throws IOException,
355: DocumentException {
356: return getField(true);
357: }
358:
359: /**
360: * Gets the check field.
361: * @return the check field
362: * @throws IOException on error
363: * @throws DocumentException on error
364: */
365: public PdfFormField getCheckField() throws IOException,
366: DocumentException {
367: return getField(false);
368: }
369:
370: /**
371: * Gets a radio or check field.
372: * @param isRadio <CODE>true</CODE> to get a radio field, <CODE>false</CODE> to get
373: * a check field
374: * @throws IOException on error
375: * @throws DocumentException on error
376: * @return the field
377: */
378: protected PdfFormField getField(boolean isRadio)
379: throws IOException, DocumentException {
380: PdfFormField field = null;
381: if (isRadio)
382: field = PdfFormField.createEmpty(writer);
383: else
384: field = PdfFormField.createCheckBox(writer);
385: field.setWidget(box, PdfAnnotation.HIGHLIGHT_INVERT);
386: if (!isRadio) {
387: field.setFieldName(fieldName);
388: if ((options & READ_ONLY) != 0)
389: field.setFieldFlags(PdfFormField.FF_READ_ONLY);
390: if ((options & REQUIRED) != 0)
391: field.setFieldFlags(PdfFormField.FF_REQUIRED);
392: field.setValueAsName(checked ? onValue : "Off");
393: }
394: if (text != null)
395: field.setMKNormalCaption(text);
396: if (rotation != 0)
397: field.setMKRotation(rotation);
398: field.setBorderStyle(new PdfBorderDictionary(borderWidth,
399: borderStyle, new PdfDashPattern(3)));
400: PdfAppearance tpon = getAppearance(isRadio, true);
401: PdfAppearance tpoff = getAppearance(isRadio, false);
402: field.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, onValue,
403: tpon);
404: field.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Off",
405: tpoff);
406: field.setAppearanceState(checked ? onValue : "Off");
407: PdfAppearance da = (PdfAppearance) tpon.getDuplicate();
408: da.setFontAndSize(getRealFont(), fontSize);
409: if (textColor == null)
410: da.setGrayFill(0);
411: else
412: da.setColorFill(textColor);
413: field.setDefaultAppearanceString(da);
414: if (borderColor != null)
415: field.setMKBorderColor(borderColor);
416: if (backgroundColor != null)
417: field.setMKBackgroundColor(backgroundColor);
418: switch (visibility) {
419: case HIDDEN:
420: field.setFlags(PdfAnnotation.FLAGS_PRINT
421: | PdfAnnotation.FLAGS_HIDDEN);
422: break;
423: case VISIBLE_BUT_DOES_NOT_PRINT:
424: break;
425: case HIDDEN_BUT_PRINTABLE:
426: field.setFlags(PdfAnnotation.FLAGS_PRINT
427: | PdfAnnotation.FLAGS_NOVIEW);
428: break;
429: default:
430: field.setFlags(PdfAnnotation.FLAGS_PRINT);
431: break;
432: }
433: return field;
434: }
435: }
|