Source Code Cross Referenced for TextField.java in  » PDF » pdf-itext » com » lowagie » text » pdf » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » PDF » pdf itext » com.lowagie.text.pdf 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2003-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:
048:        package com.lowagie.text.pdf;
049:
050:        import java.awt.Color;
051:        import java.io.IOException;
052:        import java.util.ArrayList;
053:
054:        import com.lowagie.text.Chunk;
055:        import com.lowagie.text.DocumentException;
056:        import com.lowagie.text.Element;
057:        import com.lowagie.text.Font;
058:        import com.lowagie.text.Phrase;
059:        import com.lowagie.text.Rectangle;
060:
061:        /** Supports text, combo and list fields generating the correct appearances.
062:         * All the option in the Acrobat GUI are supported in an easy to use API.
063:         * @author Paulo Soares (psoares@consiste.pt)
064:         */
065:        public class TextField extends BaseField {
066:
067:            /** Holds value of property defaultText. */
068:            private String defaultText;
069:
070:            /** Holds value of property choices. */
071:            private String[] choices;
072:
073:            /** Holds value of property choiceExports. */
074:            private String[] choiceExports;
075:
076:            /** Holds value of property choiceSelection. */
077:            private int choiceSelection;
078:
079:            private int topFirst;
080:
081:            private float extraMarginLeft;
082:            private float extraMarginTop;
083:
084:            /** Creates a new <CODE>TextField</CODE>.
085:             * @param writer the document <CODE>PdfWriter</CODE>
086:             * @param box the field location and dimensions
087:             * @param fieldName the field name. If <CODE>null</CODE> only the widget keys
088:             * will be included in the field allowing it to be used as a kid field.
089:             */
090:            public TextField(PdfWriter writer, Rectangle box, String fieldName) {
091:                super (writer, box, fieldName);
092:            }
093:
094:            private static boolean checkRTL(String text) {
095:                if (text == null || text.length() == 0)
096:                    return false;
097:                char[] cc = text.toCharArray();
098:                for (int k = 0; k < cc.length; ++k) {
099:                    int c = (int) cc[k];
100:                    if (c >= 0x590 && c < 0x0780)
101:                        return true;
102:                }
103:                return false;
104:            }
105:
106:            private static void changeFontSize(Phrase p, float size) {
107:                for (int k = 0; k < p.size(); ++k) {
108:                    ((Chunk) p.get(k)).getFont().setSize(size);
109:                }
110:            }
111:
112:            private Phrase composePhrase(String text, BaseFont ufont,
113:                    Color color, float fontSize) {
114:                Phrase phrase = null;
115:                if (extensionFont == null
116:                        && (substitutionFonts == null || substitutionFonts
117:                                .isEmpty()))
118:                    phrase = new Phrase(new Chunk(text, new Font(ufont,
119:                            fontSize, 0, color)));
120:                else {
121:                    FontSelector fs = new FontSelector();
122:                    fs.addFont(new Font(ufont, fontSize, 0, color));
123:                    if (extensionFont != null)
124:                        fs.addFont(new Font(extensionFont, fontSize, 0, color));
125:                    if (substitutionFonts != null) {
126:                        for (int k = 0; k < substitutionFonts.size(); ++k) {
127:                            fs.addFont(new Font((BaseFont) substitutionFonts
128:                                    .get(k), fontSize, 0, color));
129:                        }
130:                    }
131:                    phrase = fs.process(text);
132:                }
133:                return phrase;
134:            }
135:
136:            private static String removeCRLF(String text) {
137:                if (text.indexOf('\n') >= 0 || text.indexOf('\r') >= 0) {
138:                    char[] p = text.toCharArray();
139:                    StringBuffer sb = new StringBuffer(p.length);
140:                    for (int k = 0; k < p.length; ++k) {
141:                        char c = p[k];
142:                        if (c == '\n')
143:                            sb.append(' ');
144:                        else if (c == '\r') {
145:                            sb.append(' ');
146:                            if (k < p.length - 1 && p[k + 1] == '\n')
147:                                ++k;
148:                        } else
149:                            sb.append(c);
150:                    }
151:                    return sb.toString();
152:                }
153:                return text;
154:            }
155:
156:            public PdfAppearance getAppearance() throws IOException,
157:                    DocumentException {
158:                PdfAppearance app = getBorderAppearance();
159:                app.beginVariableText();
160:                if (text == null || text.length() == 0) {
161:                    app.endVariableText();
162:                    return app;
163:                }
164:                BaseFont ufont = getRealFont();
165:                boolean borderExtra = borderStyle == PdfBorderDictionary.STYLE_BEVELED
166:                        || borderStyle == PdfBorderDictionary.STYLE_INSET;
167:                float h = box.getHeight() - borderWidth * 2;
168:                float bw2 = borderWidth;
169:                if (borderExtra) {
170:                    h -= borderWidth * 2;
171:                    bw2 *= 2;
172:                }
173:                h -= extraMarginTop;
174:                float offsetX = (borderExtra ? 2 * borderWidth : borderWidth);
175:                offsetX = Math.max(offsetX, 1);
176:                float offX = Math.min(bw2, offsetX);
177:                app.saveState();
178:                app.rectangle(offX, offX, box.getWidth() - 2 * offX, box
179:                        .getHeight()
180:                        - 2 * offX);
181:                app.clip();
182:                app.newPath();
183:                Color fcolor = (textColor == null) ? GrayColor.GRAYBLACK
184:                        : textColor;
185:                String ptext = text; //fixed by Kazuya Ujihara (ujihara.jp)
186:                if ((options & PASSWORD) != 0) {
187:                    char[] pchar = new char[text.length()];
188:                    for (int i = 0; i < text.length(); i++)
189:                        pchar[i] = '*';
190:                    ptext = new String(pchar);
191:                }
192:                int rtl = checkRTL(ptext) ? PdfWriter.RUN_DIRECTION_LTR
193:                        : PdfWriter.RUN_DIRECTION_NO_BIDI;
194:                if ((options & MULTILINE) == 0) {
195:                    ptext = removeCRLF(text);
196:                }
197:                Phrase phrase = composePhrase(ptext, ufont, fcolor, fontSize);
198:                if ((options & MULTILINE) != 0) {
199:                    float usize = fontSize;
200:                    float width = box.getWidth() - 4 * offsetX
201:                            - extraMarginLeft;
202:                    float factor = ufont.getFontDescriptor(BaseFont.BBOXURY, 1)
203:                            - ufont.getFontDescriptor(BaseFont.BBOXLLY, 1);
204:                    ColumnText ct = new ColumnText(null);
205:                    if (usize == 0) {
206:                        usize = h / factor;
207:                        if (usize > 4) {
208:                            if (usize > 12)
209:                                usize = 12;
210:                            float step = Math.max((usize - 4) / 10, 0.2f);
211:                            ct.setSimpleColumn(0, -h, width, 0);
212:                            ct.setAlignment(alignment);
213:                            ct.setRunDirection(rtl);
214:                            for (; usize > 4; usize -= step) {
215:                                ct.setYLine(0);
216:                                changeFontSize(phrase, usize);
217:                                ct.setText(phrase);
218:                                ct.setLeading(factor * usize);
219:                                int status = ct.go(true);
220:                                if ((status & ColumnText.NO_MORE_COLUMN) == 0)
221:                                    break;
222:                            }
223:                        }
224:                        if (usize < 4) {
225:                            usize = 4;
226:                        }
227:                    }
228:                    changeFontSize(phrase, usize);
229:                    ct.setCanvas(app);
230:                    float leading = usize * factor;
231:                    float offsetY = offsetX + h
232:                            - ufont.getFontDescriptor(BaseFont.BBOXURY, usize);
233:                    ct.setSimpleColumn(extraMarginLeft + 2 * offsetX, -20000,
234:                            box.getWidth() - 2 * offsetX, offsetY + leading);
235:                    ct.setLeading(leading);
236:                    ct.setAlignment(alignment);
237:                    ct.setRunDirection(rtl);
238:                    ct.setText(phrase);
239:                    ct.go();
240:                } else {
241:                    float usize = fontSize;
242:                    if (usize == 0) {
243:                        float maxCalculatedSize = h
244:                                / (ufont.getFontDescriptor(BaseFont.BBOXURX, 1) - ufont
245:                                        .getFontDescriptor(BaseFont.BBOXLLY, 1));
246:                        changeFontSize(phrase, 1);
247:                        float wd = ColumnText.getWidth(phrase, rtl, 0);
248:                        if (wd == 0)
249:                            usize = maxCalculatedSize;
250:                        else
251:                            usize = (box.getWidth() - extraMarginLeft - 4 * offsetX)
252:                                    / wd;
253:                        if (usize > maxCalculatedSize)
254:                            usize = maxCalculatedSize;
255:                        if (usize < 4)
256:                            usize = 4;
257:                    }
258:                    changeFontSize(phrase, usize);
259:                    float offsetY = offX
260:                            + ((box.getHeight() - 2 * offX) - ufont
261:                                    .getFontDescriptor(BaseFont.ASCENT, usize))
262:                            / 2;
263:                    if (offsetY < offX)
264:                        offsetY = offX;
265:                    if (offsetY - offX < -ufont.getFontDescriptor(
266:                            BaseFont.DESCENT, usize)) {
267:                        float ny = -ufont.getFontDescriptor(BaseFont.DESCENT,
268:                                usize)
269:                                + offX;
270:                        float dy = box.getHeight()
271:                                - offX
272:                                - ufont.getFontDescriptor(BaseFont.ASCENT,
273:                                        usize);
274:                        offsetY = Math.min(ny, Math.max(offsetY, dy));
275:                    }
276:                    if ((options & COMB) != 0 && maxCharacterLength > 0) {
277:                        int textLen = Math.min(maxCharacterLength, ptext
278:                                .length());
279:                        int position = 0;
280:                        if (alignment == Element.ALIGN_RIGHT) {
281:                            position = maxCharacterLength - textLen;
282:                        } else if (alignment == Element.ALIGN_CENTER) {
283:                            position = (maxCharacterLength - textLen) / 2;
284:                        }
285:                        float step = (box.getWidth() - extraMarginLeft)
286:                                / maxCharacterLength;
287:                        float start = step / 2 + position * step;
288:                        if (textColor == null)
289:                            app.setGrayFill(0);
290:                        else
291:                            app.setColorFill(textColor);
292:                        app.beginText();
293:                        for (int k = 0; k < phrase.size(); ++k) {
294:                            Chunk ck = (Chunk) phrase.get(k);
295:                            BaseFont bf = ck.getFont().getBaseFont();
296:                            app.setFontAndSize(bf, usize);
297:                            StringBuffer sb = ck.append("");
298:                            for (int j = 0; j < sb.length(); ++j) {
299:                                String c = sb.substring(j, j + 1);
300:                                float wd = bf.getWidthPoint(c, usize);
301:                                app.setTextMatrix(extraMarginLeft + start - wd
302:                                        / 2, offsetY - extraMarginTop);
303:                                app.showText(c);
304:                                start += step;
305:                            }
306:                        }
307:                        app.endText();
308:                    } else {
309:                        if (alignment == Element.ALIGN_RIGHT) {
310:                            ColumnText.showTextAligned(app,
311:                                    Element.ALIGN_RIGHT, phrase,
312:                                    extraMarginLeft + box.getWidth() - 2
313:                                            * offsetX,
314:                                    offsetY - extraMarginTop, 0, rtl, 0);
315:                        } else if (alignment == Element.ALIGN_CENTER) {
316:                            ColumnText.showTextAligned(app,
317:                                    Element.ALIGN_CENTER, phrase,
318:                                    extraMarginLeft + box.getWidth() / 2,
319:                                    offsetY - extraMarginTop, 0, rtl, 0);
320:                        } else
321:                            ColumnText.showTextAligned(app, Element.ALIGN_LEFT,
322:                                    phrase, extraMarginLeft + 2 * offsetX,
323:                                    offsetY - extraMarginTop, 0, rtl, 0);
324:                    }
325:                }
326:                app.restoreState();
327:                app.endVariableText();
328:                return app;
329:            }
330:
331:            PdfAppearance getListAppearance() throws IOException,
332:                    DocumentException {
333:                PdfAppearance app = getBorderAppearance();
334:                app.beginVariableText();
335:                if (choices == null || choices.length == 0) {
336:                    app.endVariableText();
337:                    return app;
338:                }
339:                int topChoice = choiceSelection;
340:                if (topChoice >= choices.length) {
341:                    topChoice = choices.length - 1;
342:                }
343:                if (topChoice < 0)
344:                    topChoice = 0;
345:                BaseFont ufont = getRealFont();
346:                float usize = fontSize;
347:                if (usize == 0)
348:                    usize = 12;
349:                boolean borderExtra = borderStyle == PdfBorderDictionary.STYLE_BEVELED
350:                        || borderStyle == PdfBorderDictionary.STYLE_INSET;
351:                float h = box.getHeight() - borderWidth * 2;
352:                if (borderExtra)
353:                    h -= borderWidth * 2;
354:                float offsetX = (borderExtra ? 2 * borderWidth : borderWidth);
355:                float leading = ufont
356:                        .getFontDescriptor(BaseFont.BBOXURY, usize)
357:                        - ufont.getFontDescriptor(BaseFont.BBOXLLY, usize);
358:                int maxFit = (int) (h / leading) + 1;
359:                int first = 0;
360:                int last = 0;
361:                last = topChoice + maxFit / 2 + 1;
362:                first = last - maxFit;
363:                if (first < 0) {
364:                    last += first;
365:                    first = 0;
366:                }
367:                //        first = topChoice;
368:                last = first + maxFit;
369:                if (last > choices.length)
370:                    last = choices.length;
371:                topFirst = first;
372:                app.saveState();
373:                app.rectangle(offsetX, offsetX, box.getWidth() - 2 * offsetX,
374:                        box.getHeight() - 2 * offsetX);
375:                app.clip();
376:                app.newPath();
377:                Color fcolor = (textColor == null) ? GrayColor.GRAYBLACK
378:                        : textColor;
379:                app.setColorFill(new Color(10, 36, 106));
380:                app.rectangle(offsetX, offsetX + h - (topChoice - first + 1)
381:                        * leading, box.getWidth() - 2 * offsetX, leading);
382:                app.fill();
383:                float xp = offsetX * 2;
384:                float yp = offsetX + h
385:                        - ufont.getFontDescriptor(BaseFont.BBOXURY, usize);
386:                for (int idx = first; idx < last; ++idx, yp -= leading) {
387:                    String ptext = choices[idx];
388:                    int rtl = checkRTL(ptext) ? PdfWriter.RUN_DIRECTION_LTR
389:                            : PdfWriter.RUN_DIRECTION_NO_BIDI;
390:                    ptext = removeCRLF(ptext);
391:                    Phrase phrase = composePhrase(ptext, ufont,
392:                            (idx == topChoice) ? GrayColor.GRAYWHITE : fcolor,
393:                            usize);
394:                    ColumnText.showTextAligned(app, Element.ALIGN_LEFT, phrase,
395:                            xp, yp, 0, rtl, 0);
396:                }
397:                app.restoreState();
398:                app.endVariableText();
399:                return app;
400:            }
401:
402:            /** Gets a new text field.
403:             * @throws IOException on error
404:             * @throws DocumentException on error
405:             * @return a new text field
406:             */
407:            public PdfFormField getTextField() throws IOException,
408:                    DocumentException {
409:                if (maxCharacterLength <= 0)
410:                    options &= ~COMB;
411:                if ((options & COMB) != 0)
412:                    options &= ~MULTILINE;
413:                PdfFormField field = PdfFormField.createTextField(writer,
414:                        false, false, maxCharacterLength);
415:                field.setWidget(box, PdfAnnotation.HIGHLIGHT_INVERT);
416:                switch (alignment) {
417:                case Element.ALIGN_CENTER:
418:                    field.setQuadding(PdfFormField.Q_CENTER);
419:                    break;
420:                case Element.ALIGN_RIGHT:
421:                    field.setQuadding(PdfFormField.Q_RIGHT);
422:                    break;
423:                }
424:                if (rotation != 0)
425:                    field.setMKRotation(rotation);
426:                if (fieldName != null) {
427:                    field.setFieldName(fieldName);
428:                    if ((options & REQUIRED) == 0 && !"".equals(text))
429:                        field.setValueAsString(text);
430:                    if (defaultText != null)
431:                        field.setDefaultValueAsString(defaultText);
432:                    if ((options & READ_ONLY) != 0)
433:                        field.setFieldFlags(PdfFormField.FF_READ_ONLY);
434:                    if ((options & REQUIRED) != 0)
435:                        field.setFieldFlags(PdfFormField.FF_REQUIRED);
436:                    if ((options & MULTILINE) != 0)
437:                        field.setFieldFlags(PdfFormField.FF_MULTILINE);
438:                    if ((options & DO_NOT_SCROLL) != 0)
439:                        field.setFieldFlags(PdfFormField.FF_DONOTSCROLL);
440:                    if ((options & PASSWORD) != 0)
441:                        field.setFieldFlags(PdfFormField.FF_PASSWORD);
442:                    if ((options & FILE_SELECTION) != 0)
443:                        field.setFieldFlags(PdfFormField.FF_FILESELECT);
444:                    if ((options & DO_NOT_SPELL_CHECK) != 0)
445:                        field.setFieldFlags(PdfFormField.FF_DONOTSPELLCHECK);
446:                    if ((options & COMB) != 0)
447:                        field.setFieldFlags(PdfFormField.FF_COMB);
448:                }
449:                field.setBorderStyle(new PdfBorderDictionary(borderWidth,
450:                        borderStyle, new PdfDashPattern(3)));
451:                PdfAppearance tp = getAppearance();
452:                field.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, tp);
453:                PdfAppearance da = (PdfAppearance) tp.getDuplicate();
454:                da.setFontAndSize(getRealFont(), fontSize);
455:                if (textColor == null)
456:                    da.setGrayFill(0);
457:                else
458:                    da.setColorFill(textColor);
459:                field.setDefaultAppearanceString(da);
460:                if (borderColor != null)
461:                    field.setMKBorderColor(borderColor);
462:                if (backgroundColor != null)
463:                    field.setMKBackgroundColor(backgroundColor);
464:                switch (visibility) {
465:                case HIDDEN:
466:                    field.setFlags(PdfAnnotation.FLAGS_PRINT
467:                            | PdfAnnotation.FLAGS_HIDDEN);
468:                    break;
469:                case VISIBLE_BUT_DOES_NOT_PRINT:
470:                    break;
471:                case HIDDEN_BUT_PRINTABLE:
472:                    field.setFlags(PdfAnnotation.FLAGS_PRINT
473:                            | PdfAnnotation.FLAGS_NOVIEW);
474:                    break;
475:                default:
476:                    field.setFlags(PdfAnnotation.FLAGS_PRINT);
477:                    break;
478:                }
479:                return field;
480:            }
481:
482:            /** Gets a new combo field.
483:             * @throws IOException on error
484:             * @throws DocumentException on error
485:             * @return a new combo field
486:             */
487:            public PdfFormField getComboField() throws IOException,
488:                    DocumentException {
489:                return getChoiceField(false);
490:            }
491:
492:            /** Gets a new list field.
493:             * @throws IOException on error
494:             * @throws DocumentException on error
495:             * @return a new list field
496:             */
497:            public PdfFormField getListField() throws IOException,
498:                    DocumentException {
499:                return getChoiceField(true);
500:            }
501:
502:            protected PdfFormField getChoiceField(boolean isList)
503:                    throws IOException, DocumentException {
504:                options &= (~MULTILINE) & (~COMB);
505:                String uchoices[] = choices;
506:                if (uchoices == null)
507:                    uchoices = new String[0];
508:                int topChoice = choiceSelection;
509:                if (topChoice >= uchoices.length)
510:                    topChoice = uchoices.length - 1;
511:                if (text == null)
512:                    text = ""; //fixed by Kazuya Ujihara (ujihara.jp)
513:                if (topChoice >= 0)
514:                    text = uchoices[topChoice];
515:                if (topChoice < 0)
516:                    topChoice = 0;
517:                PdfFormField field = null;
518:                String mix[][] = null;
519:                if (choiceExports == null) {
520:                    if (isList)
521:                        field = PdfFormField.createList(writer, uchoices,
522:                                topChoice);
523:                    else
524:                        field = PdfFormField.createCombo(writer,
525:                                (options & EDIT) != 0, uchoices, topChoice);
526:                } else {
527:                    mix = new String[uchoices.length][2];
528:                    for (int k = 0; k < mix.length; ++k)
529:                        mix[k][0] = mix[k][1] = uchoices[k];
530:                    int top = Math.min(uchoices.length, choiceExports.length);
531:                    for (int k = 0; k < top; ++k) {
532:                        if (choiceExports[k] != null)
533:                            mix[k][0] = choiceExports[k];
534:                    }
535:                    if (isList)
536:                        field = PdfFormField.createList(writer, mix, topChoice);
537:                    else
538:                        field = PdfFormField.createCombo(writer,
539:                                (options & EDIT) != 0, mix, topChoice);
540:                }
541:                field.setWidget(box, PdfAnnotation.HIGHLIGHT_INVERT);
542:                if (rotation != 0)
543:                    field.setMKRotation(rotation);
544:                if (fieldName != null) {
545:                    field.setFieldName(fieldName);
546:                    if (uchoices.length > 0) {
547:                        if (mix != null) {
548:                            field.setValueAsString(mix[topChoice][0]);
549:                            field.setDefaultValueAsString(mix[topChoice][0]);
550:                        } else {
551:                            field.setValueAsString(text);
552:                            field.setDefaultValueAsString(text);
553:                        }
554:                    }
555:                    if ((options & READ_ONLY) != 0)
556:                        field.setFieldFlags(PdfFormField.FF_READ_ONLY);
557:                    if ((options & REQUIRED) != 0)
558:                        field.setFieldFlags(PdfFormField.FF_REQUIRED);
559:                    if ((options & DO_NOT_SPELL_CHECK) != 0)
560:                        field.setFieldFlags(PdfFormField.FF_DONOTSPELLCHECK);
561:                }
562:                field.setBorderStyle(new PdfBorderDictionary(borderWidth,
563:                        borderStyle, new PdfDashPattern(3)));
564:                PdfAppearance tp;
565:                if (isList) {
566:                    tp = getListAppearance();
567:                    if (topFirst > 0)
568:                        field.put(PdfName.TI, new PdfNumber(topFirst));
569:                } else
570:                    tp = getAppearance();
571:                field.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, tp);
572:                PdfAppearance da = (PdfAppearance) tp.getDuplicate();
573:                da.setFontAndSize(getRealFont(), fontSize);
574:                if (textColor == null)
575:                    da.setGrayFill(0);
576:                else
577:                    da.setColorFill(textColor);
578:                field.setDefaultAppearanceString(da);
579:                if (borderColor != null)
580:                    field.setMKBorderColor(borderColor);
581:                if (backgroundColor != null)
582:                    field.setMKBackgroundColor(backgroundColor);
583:                switch (visibility) {
584:                case HIDDEN:
585:                    field.setFlags(PdfAnnotation.FLAGS_PRINT
586:                            | PdfAnnotation.FLAGS_HIDDEN);
587:                    break;
588:                case VISIBLE_BUT_DOES_NOT_PRINT:
589:                    break;
590:                case HIDDEN_BUT_PRINTABLE:
591:                    field.setFlags(PdfAnnotation.FLAGS_PRINT
592:                            | PdfAnnotation.FLAGS_NOVIEW);
593:                    break;
594:                default:
595:                    field.setFlags(PdfAnnotation.FLAGS_PRINT);
596:                    break;
597:                }
598:                return field;
599:            }
600:
601:            /** Gets the default text.
602:             * @return the default text
603:             */
604:            public String getDefaultText() {
605:                return this .defaultText;
606:            }
607:
608:            /** Sets the default text. It is only meaningful for text fields.
609:             * @param defaultText the default text
610:             */
611:            public void setDefaultText(String defaultText) {
612:                this .defaultText = defaultText;
613:            }
614:
615:            /** Gets the choices to be presented to the user in list/combo
616:             * fields.
617:             * @return the choices to be presented to the user
618:             */
619:            public String[] getChoices() {
620:                return this .choices;
621:            }
622:
623:            /** Sets the choices to be presented to the user in list/combo
624:             * fields.
625:             * @param choices the choices to be presented to the user
626:             */
627:            public void setChoices(String[] choices) {
628:                this .choices = choices;
629:            }
630:
631:            /** Gets the export values in list/combo fields.
632:             * @return the export values in list/combo fields
633:             */
634:            public String[] getChoiceExports() {
635:                return this .choiceExports;
636:            }
637:
638:            /** Sets the export values in list/combo fields. If this array
639:             * is <CODE>null</CODE> then the choice values will also be used
640:             * as the export values.
641:             * @param choiceExports the export values in list/combo fields
642:             */
643:            public void setChoiceExports(String[] choiceExports) {
644:                this .choiceExports = choiceExports;
645:            }
646:
647:            /** Gets the zero based index of the selected item.
648:             * @return the zero based index of the selected item
649:             */
650:            public int getChoiceSelection() {
651:                return this .choiceSelection;
652:            }
653:
654:            /** Sets the zero based index of the selected item.
655:             * @param choiceSelection the zero based index of the selected item
656:             */
657:            public void setChoiceSelection(int choiceSelection) {
658:                this .choiceSelection = choiceSelection;
659:            }
660:
661:            int getTopFirst() {
662:                return topFirst;
663:            }
664:
665:            /**
666:             * Sets extra margins in text fields to better mimic the Acrobat layout.
667:             * @param extraMarginLeft the extra marging left
668:             * @param extraMarginTop the extra margin top
669:             */
670:            public void setExtraMargin(float extraMarginLeft,
671:                    float extraMarginTop) {
672:                this .extraMarginLeft = extraMarginLeft;
673:                this .extraMarginTop = extraMarginTop;
674:            }
675:
676:            /**
677:             * Holds value of property substitutionFonts.
678:             */
679:            private ArrayList substitutionFonts;
680:
681:            /**
682:             * Gets the list of substitution fonts. The list is composed of <CODE>BaseFont</CODE> and can be <CODE>null</CODE>. The fonts in this list will be used if the original
683:             * font doesn't contain the needed glyphs.
684:             * @return the list
685:             */
686:            public ArrayList getSubstitutionFonts() {
687:                return this .substitutionFonts;
688:            }
689:
690:            /**
691:             * Sets a list of substitution fonts. The list is composed of <CODE>BaseFont</CODE> and can also be <CODE>null</CODE>. The fonts in this list will be used if the original
692:             * font doesn't contain the needed glyphs.
693:             * @param substitutionFonts the list
694:             */
695:            public void setSubstitutionFonts(ArrayList substitutionFonts) {
696:                this .substitutionFonts = substitutionFonts;
697:            }
698:
699:            /**
700:             * Holds value of property extensionFont.
701:             */
702:            private BaseFont extensionFont;
703:
704:            /**
705:             * Gets the extensionFont. This font will be searched before the
706:             * substitution fonts. It may be <code>null</code>.
707:             * @return the extensionFont
708:             */
709:            public BaseFont getExtensionFont() {
710:                return this .extensionFont;
711:            }
712:
713:            /**
714:             * Sets the extensionFont. This font will be searched before the
715:             * substitution fonts. It may be <code>null</code>.
716:             * @param extensionFont New value of property extensionFont.
717:             */
718:            public void setExtensionFont(BaseFont extensionFont) {
719:                this.extensionFont = extensionFont;
720:            }
721:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.