001: /*
002: * $Id: PdfAcroForm.java 2702 2007-04-20 16:18:04Z psoares33 $
003: * $Name$
004: *
005: * Copyright 2002 Bruno Lowagie
006: *
007: * The contents of this file are subject to the Mozilla Public License Version 1.1
008: * (the "License"); you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
010: *
011: * Software distributed under the License is distributed on an "AS IS" basis,
012: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
013: * for the specific language governing rights and limitations under the License.
014: *
015: * The Original Code is 'iText, a free JAVA-PDF library'.
016: *
017: * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
018: * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
019: * All Rights Reserved.
020: * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
021: * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
022: *
023: * Contributor(s): all the names of the contributors are added in the source code
024: * where applicable.
025: *
026: * Alternatively, the contents of this file may be used under the terms of the
027: * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
028: * provisions of LGPL are applicable instead of those above. If you wish to
029: * allow use of your version of this file only under the terms of the LGPL
030: * License and not to allow others to use your version of this file under
031: * the MPL, indicate your decision by deleting the provisions above and
032: * replace them with the notice and other provisions required by the LGPL.
033: * If you do not delete the provisions above, a recipient may use your version
034: * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
035: *
036: * This library is free software; you can redistribute it and/or modify it
037: * under the terms of the MPL as stated above or under the terms of the GNU
038: * Library General Public License as published by the Free Software Foundation;
039: * either version 2 of the License, or any later version.
040: *
041: * This library is distributed in the hope that it will be useful, but WITHOUT
042: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
043: * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
044: * details.
045: *
046: * If you didn't download this code from the following link, you should check if
047: * you aren't using an obsolete version:
048: * http://www.lowagie.com/iText/
049: */
050:
051: package com.lowagie.text.pdf;
052:
053: import java.util.HashMap;
054: import java.util.Iterator;
055:
056: import com.lowagie.text.ExceptionConverter;
057: import com.lowagie.text.Rectangle;
058:
059: /**
060: * Each PDF document can contain maximum 1 AcroForm.
061: */
062:
063: public class PdfAcroForm extends PdfDictionary {
064:
065: private PdfWriter writer;
066:
067: /** This is a map containing FieldTemplates. */
068: private HashMap fieldTemplates = new HashMap();
069:
070: /** This is an array containing DocumentFields. */
071: private PdfArray documentFields = new PdfArray();
072:
073: /** This is an array containing the calculationorder of the fields. */
074: private PdfArray calculationOrder = new PdfArray();
075:
076: /** Contains the signature flags. */
077: private int sigFlags = 0;
078:
079: /** Creates new PdfAcroForm
080: * @param writer*/
081: public PdfAcroForm(PdfWriter writer) {
082: super ();
083: this .writer = writer;
084: }
085:
086: public void setNeedAppearances(boolean value) {
087: put(PdfName.NEEDAPPEARANCES, new PdfBoolean(value));
088: }
089:
090: /**
091: * Adds fieldTemplates.
092: * @param ft
093: */
094:
095: public void addFieldTemplates(HashMap ft) {
096: fieldTemplates.putAll(ft);
097: }
098:
099: /**
100: * Adds documentFields.
101: * @param ref
102: */
103:
104: public void addDocumentField(PdfIndirectReference ref) {
105: documentFields.add(ref);
106: }
107:
108: /**
109: * Checks if the Acroform is valid
110: * @return true if the Acroform is valid
111: */
112:
113: public boolean isValid() {
114: if (documentFields.size() == 0)
115: return false;
116: put(PdfName.FIELDS, documentFields);
117: if (sigFlags != 0)
118: put(PdfName.SIGFLAGS, new PdfNumber(sigFlags));
119: if (calculationOrder.size() > 0)
120: put(PdfName.CO, calculationOrder);
121: if (fieldTemplates.isEmpty())
122: return true;
123: PdfDictionary dic = new PdfDictionary();
124: for (Iterator it = fieldTemplates.keySet().iterator(); it
125: .hasNext();) {
126: PdfTemplate template = (PdfTemplate) it.next();
127: PdfFormField.mergeResources(dic, (PdfDictionary) template
128: .getResources());
129: }
130: put(PdfName.DR, dic);
131: put(PdfName.DA, new PdfString("/Helv 0 Tf 0 g "));
132: PdfDictionary fonts = (PdfDictionary) dic.get(PdfName.FONT);
133: if (fonts != null) {
134: writer.eliminateFontSubset(fonts);
135: }
136: return true;
137: }
138:
139: /**
140: * Adds an object to the calculationOrder.
141: * @param formField
142: */
143:
144: public void addCalculationOrder(PdfFormField formField) {
145: calculationOrder.add(formField.getIndirectReference());
146: }
147:
148: /**
149: * Sets the signature flags.
150: * @param f
151: */
152:
153: public void setSigFlags(int f) {
154: sigFlags |= f;
155: }
156:
157: /**
158: * Adds a formfield to the AcroForm.
159: * @param formField
160: */
161:
162: public void addFormField(PdfFormField formField) {
163: writer.addAnnotation(formField);
164: }
165:
166: /**
167: * @param name
168: * @param caption
169: * @param value
170: * @param url
171: * @param font
172: * @param fontSize
173: * @param llx
174: * @param lly
175: * @param urx
176: * @param ury
177: * @return a PdfFormField
178: */
179: public PdfFormField addHtmlPostButton(String name, String caption,
180: String value, String url, BaseFont font, float fontSize,
181: float llx, float lly, float urx, float ury) {
182: PdfAction action = PdfAction.createSubmitForm(url, null,
183: PdfAction.SUBMIT_HTML_FORMAT);
184: PdfFormField button = new PdfFormField(writer, llx, lly, urx,
185: ury, action);
186: setButtonParams(button, PdfFormField.FF_PUSHBUTTON, name, value);
187: drawButton(button, caption, font, fontSize, llx, lly, urx, ury);
188: addFormField(button);
189: return button;
190: }
191:
192: /**
193: * @param name
194: * @param caption
195: * @param value
196: * @param font
197: * @param fontSize
198: * @param llx
199: * @param lly
200: * @param urx
201: * @param ury
202: * @return a PdfFormField
203: */
204: public PdfFormField addResetButton(String name, String caption,
205: String value, BaseFont font, float fontSize, float llx,
206: float lly, float urx, float ury) {
207: PdfAction action = PdfAction.createResetForm(null, 0);
208: PdfFormField button = new PdfFormField(writer, llx, lly, urx,
209: ury, action);
210: setButtonParams(button, PdfFormField.FF_PUSHBUTTON, name, value);
211: drawButton(button, caption, font, fontSize, llx, lly, urx, ury);
212: addFormField(button);
213: return button;
214: }
215:
216: /**
217: * @param name
218: * @param value
219: * @param url
220: * @param appearance
221: * @param llx
222: * @param lly
223: * @param urx
224: * @param ury
225: * @return a PdfFormField
226: */
227: public PdfFormField addMap(String name, String value, String url,
228: PdfContentByte appearance, float llx, float lly, float urx,
229: float ury) {
230: PdfAction action = PdfAction.createSubmitForm(url, null,
231: PdfAction.SUBMIT_HTML_FORMAT
232: | PdfAction.SUBMIT_COORDINATES);
233: PdfFormField button = new PdfFormField(writer, llx, lly, urx,
234: ury, action);
235: setButtonParams(button, PdfFormField.FF_PUSHBUTTON, name, null);
236: PdfAppearance pa = PdfAppearance.createAppearance(writer, urx
237: - llx, ury - lly);
238: pa.add(appearance);
239: button.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, pa);
240: addFormField(button);
241: return button;
242: }
243:
244: /**
245: * @param button
246: * @param characteristics
247: * @param name
248: * @param value
249: */
250: public void setButtonParams(PdfFormField button,
251: int characteristics, String name, String value) {
252: button.setButton(characteristics);
253: button.setFlags(PdfAnnotation.FLAGS_PRINT);
254: button.setPage();
255: button.setFieldName(name);
256: if (value != null)
257: button.setValueAsString(value);
258: }
259:
260: /**
261: * @param button
262: * @param caption
263: * @param font
264: * @param fontSize
265: * @param llx
266: * @param lly
267: * @param urx
268: * @param ury
269: */
270: public void drawButton(PdfFormField button, String caption,
271: BaseFont font, float fontSize, float llx, float lly,
272: float urx, float ury) {
273: PdfAppearance pa = PdfAppearance.createAppearance(writer, urx
274: - llx, ury - lly);
275: pa.drawButton(0f, 0f, urx - llx, ury - lly, caption, font,
276: fontSize);
277: button.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, pa);
278: }
279:
280: /**
281: * @param name
282: * @param value
283: * @return a PdfFormField
284: */
285: public PdfFormField addHiddenField(String name, String value) {
286: PdfFormField hidden = PdfFormField.createEmpty(writer);
287: hidden.setFieldName(name);
288: hidden.setValueAsName(value);
289: addFormField(hidden);
290: return hidden;
291: }
292:
293: /**
294: * @param name
295: * @param text
296: * @param font
297: * @param fontSize
298: * @param llx
299: * @param lly
300: * @param urx
301: * @param ury
302: * @return a PdfFormField
303: */
304: public PdfFormField addSingleLineTextField(String name,
305: String text, BaseFont font, float fontSize, float llx,
306: float lly, float urx, float ury) {
307: PdfFormField field = PdfFormField.createTextField(writer,
308: PdfFormField.SINGLELINE, PdfFormField.PLAINTEXT, 0);
309: setTextFieldParams(field, text, name, llx, lly, urx, ury);
310: drawSingleLineOfText(field, text, font, fontSize, llx, lly,
311: urx, ury);
312: addFormField(field);
313: return field;
314: }
315:
316: /**
317: * @param name
318: * @param text
319: * @param font
320: * @param fontSize
321: * @param llx
322: * @param lly
323: * @param urx
324: * @param ury
325: * @return a PdfFormField
326: */
327: public PdfFormField addMultiLineTextField(String name, String text,
328: BaseFont font, float fontSize, float llx, float lly,
329: float urx, float ury) {
330: PdfFormField field = PdfFormField.createTextField(writer,
331: PdfFormField.MULTILINE, PdfFormField.PLAINTEXT, 0);
332: setTextFieldParams(field, text, name, llx, lly, urx, ury);
333: drawMultiLineOfText(field, text, font, fontSize, llx, lly, urx,
334: ury);
335: addFormField(field);
336: return field;
337: }
338:
339: /**
340: * @param name
341: * @param text
342: * @param font
343: * @param fontSize
344: * @param llx
345: * @param lly
346: * @param urx
347: * @param ury
348: * @return PdfFormField
349: */
350: public PdfFormField addSingleLinePasswordField(String name,
351: String text, BaseFont font, float fontSize, float llx,
352: float lly, float urx, float ury) {
353: PdfFormField field = PdfFormField.createTextField(writer,
354: PdfFormField.SINGLELINE, PdfFormField.PASSWORD, 0);
355: setTextFieldParams(field, text, name, llx, lly, urx, ury);
356: drawSingleLineOfText(field, text, font, fontSize, llx, lly,
357: urx, ury);
358: addFormField(field);
359: return field;
360: }
361:
362: /**
363: * @param field
364: * @param text
365: * @param name
366: * @param llx
367: * @param lly
368: * @param urx
369: * @param ury
370: */
371: public void setTextFieldParams(PdfFormField field, String text,
372: String name, float llx, float lly, float urx, float ury) {
373: field.setWidget(new Rectangle(llx, lly, urx, ury),
374: PdfAnnotation.HIGHLIGHT_INVERT);
375: field.setValueAsString(text);
376: field.setDefaultValueAsString(text);
377: field.setFieldName(name);
378: field.setFlags(PdfAnnotation.FLAGS_PRINT);
379: field.setPage();
380: }
381:
382: /**
383: * @param field
384: * @param text
385: * @param font
386: * @param fontSize
387: * @param llx
388: * @param lly
389: * @param urx
390: * @param ury
391: */
392: public void drawSingleLineOfText(PdfFormField field, String text,
393: BaseFont font, float fontSize, float llx, float lly,
394: float urx, float ury) {
395: PdfAppearance tp = PdfAppearance.createAppearance(writer, urx
396: - llx, ury - lly);
397: PdfAppearance tp2 = (PdfAppearance) tp.getDuplicate();
398: tp2.setFontAndSize(font, fontSize);
399: tp2.resetRGBColorFill();
400: field.setDefaultAppearanceString(tp2);
401: tp.drawTextField(0f, 0f, urx - llx, ury - lly);
402: tp.beginVariableText();
403: tp.saveState();
404: tp.rectangle(3f, 3f, urx - llx - 6f, ury - lly - 6f);
405: tp.clip();
406: tp.newPath();
407: tp.beginText();
408: tp.setFontAndSize(font, fontSize);
409: tp.resetRGBColorFill();
410: tp.setTextMatrix(4, (ury - lly) / 2 - (fontSize * 0.3f));
411: tp.showText(text);
412: tp.endText();
413: tp.restoreState();
414: tp.endVariableText();
415: field.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, tp);
416: }
417:
418: /**
419: * @param field
420: * @param text
421: * @param font
422: * @param fontSize
423: * @param llx
424: * @param lly
425: * @param urx
426: * @param ury
427: */
428: public void drawMultiLineOfText(PdfFormField field, String text,
429: BaseFont font, float fontSize, float llx, float lly,
430: float urx, float ury) {
431: PdfAppearance tp = PdfAppearance.createAppearance(writer, urx
432: - llx, ury - lly);
433: PdfAppearance tp2 = (PdfAppearance) tp.getDuplicate();
434: tp2.setFontAndSize(font, fontSize);
435: tp2.resetRGBColorFill();
436: field.setDefaultAppearanceString(tp2);
437: tp.drawTextField(0f, 0f, urx - llx, ury - lly);
438: tp.beginVariableText();
439: tp.saveState();
440: tp.rectangle(3f, 3f, urx - llx - 6f, ury - lly - 6f);
441: tp.clip();
442: tp.newPath();
443: tp.beginText();
444: tp.setFontAndSize(font, fontSize);
445: tp.resetRGBColorFill();
446: tp.setTextMatrix(4, 5);
447: java.util.StringTokenizer tokenizer = new java.util.StringTokenizer(
448: text, "\n");
449: float yPos = ury - lly;
450: while (tokenizer.hasMoreTokens()) {
451: yPos -= fontSize * 1.2f;
452: tp.showTextAligned(PdfContentByte.ALIGN_LEFT, tokenizer
453: .nextToken(), 3, yPos, 0);
454: }
455: tp.endText();
456: tp.restoreState();
457: tp.endVariableText();
458: field.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, tp);
459: }
460:
461: /**
462: * @param name
463: * @param value
464: * @param status
465: * @param llx
466: * @param lly
467: * @param urx
468: * @param ury
469: * @return a PdfFormField
470: */
471: public PdfFormField addCheckBox(String name, String value,
472: boolean status, float llx, float lly, float urx, float ury) {
473: PdfFormField field = PdfFormField.createCheckBox(writer);
474: setCheckBoxParams(field, name, value, status, llx, lly, urx,
475: ury);
476: drawCheckBoxAppearences(field, value, llx, lly, urx, ury);
477: addFormField(field);
478: return field;
479: }
480:
481: /**
482: * @param field
483: * @param name
484: * @param value
485: * @param status
486: * @param llx
487: * @param lly
488: * @param urx
489: * @param ury
490: */
491: public void setCheckBoxParams(PdfFormField field, String name,
492: String value, boolean status, float llx, float lly,
493: float urx, float ury) {
494: field.setWidget(new Rectangle(llx, lly, urx, ury),
495: PdfAnnotation.HIGHLIGHT_TOGGLE);
496: field.setFieldName(name);
497: if (status) {
498: field.setValueAsName(value);
499: field.setAppearanceState(value);
500: } else {
501: field.setValueAsName("Off");
502: field.setAppearanceState("Off");
503: }
504: field.setFlags(PdfAnnotation.FLAGS_PRINT);
505: field.setPage();
506: field.setBorderStyle(new PdfBorderDictionary(1,
507: PdfBorderDictionary.STYLE_SOLID));
508: }
509:
510: /**
511: * @param field
512: * @param value
513: * @param llx
514: * @param lly
515: * @param urx
516: * @param ury
517: */
518: public void drawCheckBoxAppearences(PdfFormField field,
519: String value, float llx, float lly, float urx, float ury) {
520: BaseFont font = null;
521: try {
522: font = BaseFont.createFont(BaseFont.ZAPFDINGBATS,
523: BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
524: } catch (Exception e) {
525: throw new ExceptionConverter(e);
526: }
527: float size = (ury - lly);
528: PdfAppearance tpOn = PdfAppearance.createAppearance(writer, urx
529: - llx, ury - lly);
530: PdfAppearance tp2 = (PdfAppearance) tpOn.getDuplicate();
531: tp2.setFontAndSize(font, size);
532: tp2.resetRGBColorFill();
533: field.setDefaultAppearanceString(tp2);
534: tpOn.drawTextField(0f, 0f, urx - llx, ury - lly);
535: tpOn.saveState();
536: tpOn.resetRGBColorFill();
537: tpOn.beginText();
538: tpOn.setFontAndSize(font, size);
539: tpOn.showTextAligned(PdfContentByte.ALIGN_CENTER, "4",
540: (urx - llx) / 2, (ury - lly) / 2 - (size * 0.3f), 0);
541: tpOn.endText();
542: tpOn.restoreState();
543: field.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, value,
544: tpOn);
545: PdfAppearance tpOff = PdfAppearance.createAppearance(writer,
546: urx - llx, ury - lly);
547: tpOff.drawTextField(0f, 0f, urx - llx, ury - lly);
548: field.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Off",
549: tpOff);
550: }
551:
552: /**
553: * @param name
554: * @param defaultValue
555: * @param noToggleToOff
556: * @return a PdfFormField
557: */
558: public PdfFormField getRadioGroup(String name, String defaultValue,
559: boolean noToggleToOff) {
560: PdfFormField radio = PdfFormField.createRadioButton(writer,
561: noToggleToOff);
562: radio.setFieldName(name);
563: radio.setValueAsName(defaultValue);
564: return radio;
565: }
566:
567: /**
568: * @param radiogroup
569: */
570: public void addRadioGroup(PdfFormField radiogroup) {
571: addFormField(radiogroup);
572: }
573:
574: /**
575: * @param radiogroup
576: * @param value
577: * @param llx
578: * @param lly
579: * @param urx
580: * @param ury
581: * @return a PdfFormField
582: */
583: public PdfFormField addRadioButton(PdfFormField radiogroup,
584: String value, float llx, float lly, float urx, float ury) {
585: PdfFormField radio = PdfFormField.createEmpty(writer);
586: radio.setWidget(new Rectangle(llx, lly, urx, ury),
587: PdfAnnotation.HIGHLIGHT_TOGGLE);
588: String name = ((PdfName) radiogroup.get(PdfName.V)).toString()
589: .substring(1);
590: if (name.equals(value)) {
591: radio.setAppearanceState(value);
592: } else {
593: radio.setAppearanceState("Off");
594: }
595: drawRadioAppearences(radio, value, llx, lly, urx, ury);
596: radiogroup.addKid(radio);
597: return radio;
598: }
599:
600: /**
601: * @param field
602: * @param value
603: * @param llx
604: * @param lly
605: * @param urx
606: * @param ury
607: */
608: public void drawRadioAppearences(PdfFormField field, String value,
609: float llx, float lly, float urx, float ury) {
610: PdfAppearance tpOn = PdfAppearance.createAppearance(writer, urx
611: - llx, ury - lly);
612: tpOn.drawRadioField(0f, 0f, urx - llx, ury - lly, true);
613: field.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, value,
614: tpOn);
615: PdfAppearance tpOff = PdfAppearance.createAppearance(writer,
616: urx - llx, ury - lly);
617: tpOff.drawRadioField(0f, 0f, urx - llx, ury - lly, false);
618: field.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Off",
619: tpOff);
620: }
621:
622: /**
623: * @param name
624: * @param options
625: * @param defaultValue
626: * @param font
627: * @param fontSize
628: * @param llx
629: * @param lly
630: * @param urx
631: * @param ury
632: * @return a PdfFormField
633: */
634: public PdfFormField addSelectList(String name, String[] options,
635: String defaultValue, BaseFont font, float fontSize,
636: float llx, float lly, float urx, float ury) {
637: PdfFormField choice = PdfFormField.createList(writer, options,
638: 0);
639: setChoiceParams(choice, name, defaultValue, llx, lly, urx, ury);
640: StringBuffer text = new StringBuffer();
641: for (int i = 0; i < options.length; i++) {
642: text.append(options[i]).append('\n');
643: }
644: drawMultiLineOfText(choice, text.toString(), font, fontSize,
645: llx, lly, urx, ury);
646: addFormField(choice);
647: return choice;
648: }
649:
650: /**
651: * @param name
652: * @param options
653: * @param defaultValue
654: * @param font
655: * @param fontSize
656: * @param llx
657: * @param lly
658: * @param urx
659: * @param ury
660: * @return a PdfFormField
661: */
662: public PdfFormField addSelectList(String name, String[][] options,
663: String defaultValue, BaseFont font, float fontSize,
664: float llx, float lly, float urx, float ury) {
665: PdfFormField choice = PdfFormField.createList(writer, options,
666: 0);
667: setChoiceParams(choice, name, defaultValue, llx, lly, urx, ury);
668: StringBuffer text = new StringBuffer();
669: for (int i = 0; i < options.length; i++) {
670: text.append(options[i][1]).append('\n');
671: }
672: drawMultiLineOfText(choice, text.toString(), font, fontSize,
673: llx, lly, urx, ury);
674: addFormField(choice);
675: return choice;
676: }
677:
678: /**
679: * @param name
680: * @param options
681: * @param defaultValue
682: * @param editable
683: * @param font
684: * @param fontSize
685: * @param llx
686: * @param lly
687: * @param urx
688: * @param ury
689: * @return a PdfFormField
690: */
691: public PdfFormField addComboBox(String name, String[] options,
692: String defaultValue, boolean editable, BaseFont font,
693: float fontSize, float llx, float lly, float urx, float ury) {
694: PdfFormField choice = PdfFormField.createCombo(writer,
695: editable, options, 0);
696: setChoiceParams(choice, name, defaultValue, llx, lly, urx, ury);
697: if (defaultValue == null) {
698: defaultValue = options[0];
699: }
700: drawSingleLineOfText(choice, defaultValue, font, fontSize, llx,
701: lly, urx, ury);
702: addFormField(choice);
703: return choice;
704: }
705:
706: /**
707: * @param name
708: * @param options
709: * @param defaultValue
710: * @param editable
711: * @param font
712: * @param fontSize
713: * @param llx
714: * @param lly
715: * @param urx
716: * @param ury
717: * @return a PdfFormField
718: */
719: public PdfFormField addComboBox(String name, String[][] options,
720: String defaultValue, boolean editable, BaseFont font,
721: float fontSize, float llx, float lly, float urx, float ury) {
722: PdfFormField choice = PdfFormField.createCombo(writer,
723: editable, options, 0);
724: setChoiceParams(choice, name, defaultValue, llx, lly, urx, ury);
725: String value = null;
726: for (int i = 0; i < options.length; i++) {
727: if (options[i][0].equals(defaultValue)) {
728: value = options[i][1];
729: break;
730: }
731: }
732: if (value == null) {
733: value = options[0][1];
734: }
735: drawSingleLineOfText(choice, value, font, fontSize, llx, lly,
736: urx, ury);
737: addFormField(choice);
738: return choice;
739: }
740:
741: /**
742: * @param field
743: * @param name
744: * @param defaultValue
745: * @param llx
746: * @param lly
747: * @param urx
748: * @param ury
749: */
750: public void setChoiceParams(PdfFormField field, String name,
751: String defaultValue, float llx, float lly, float urx,
752: float ury) {
753: field.setWidget(new Rectangle(llx, lly, urx, ury),
754: PdfAnnotation.HIGHLIGHT_INVERT);
755: if (defaultValue != null) {
756: field.setValueAsString(defaultValue);
757: field.setDefaultValueAsString(defaultValue);
758: }
759: field.setFieldName(name);
760: field.setFlags(PdfAnnotation.FLAGS_PRINT);
761: field.setPage();
762: field.setBorderStyle(new PdfBorderDictionary(2,
763: PdfBorderDictionary.STYLE_SOLID));
764: }
765:
766: /**
767: * @param name
768: * @param llx
769: * @param lly
770: * @param urx
771: * @param ury
772: * @return a PdfFormField
773: */
774: public PdfFormField addSignature(String name, float llx, float lly,
775: float urx, float ury) {
776: PdfFormField signature = PdfFormField.createSignature(writer);
777: setSignatureParams(signature, name, llx, lly, urx, ury);
778: drawSignatureAppearences(signature, llx, lly, urx, ury);
779: addFormField(signature);
780: return signature;
781: }
782:
783: /**
784: * @param field
785: * @param name
786: * @param llx
787: * @param lly
788: * @param urx
789: * @param ury
790: */
791: public void setSignatureParams(PdfFormField field, String name,
792: float llx, float lly, float urx, float ury) {
793: field.setWidget(new Rectangle(llx, lly, urx, ury),
794: PdfAnnotation.HIGHLIGHT_INVERT);
795: field.setFieldName(name);
796: field.setFlags(PdfAnnotation.FLAGS_PRINT);
797: field.setPage();
798: field.setMKBorderColor(java.awt.Color.black);
799: field.setMKBackgroundColor(java.awt.Color.white);
800: }
801:
802: /**
803: * @param field
804: * @param llx
805: * @param lly
806: * @param urx
807: * @param ury
808: */
809: public void drawSignatureAppearences(PdfFormField field, float llx,
810: float lly, float urx, float ury) {
811: PdfAppearance tp = PdfAppearance.createAppearance(writer, urx
812: - llx, ury - lly);
813: tp.setGrayFill(1.0f);
814: tp.rectangle(0, 0, urx - llx, ury - lly);
815: tp.fill();
816: tp.setGrayStroke(0);
817: tp.setLineWidth(1);
818: tp.rectangle(0.5f, 0.5f, urx - llx - 0.5f, ury - lly - 0.5f);
819: tp.closePathStroke();
820: tp.saveState();
821: tp.rectangle(1, 1, urx - llx - 2, ury - lly - 2);
822: tp.clip();
823: tp.newPath();
824: tp.restoreState();
825: field.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, tp);
826: }
827: }
|