Source Code Cross Referenced for PushbuttonField.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 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.Image;
053:        import com.lowagie.text.Rectangle;
054:
055:        /**
056:         * Creates a pushbutton field. It supports all the text and icon alignments.
057:         * The icon may be an image or a template.
058:         * <p>
059:         * Example usage:
060:         * <p>
061:         * <PRE>
062:         * Document document = new Document(PageSize.A4, 50, 50, 50, 50);
063:         * PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("output.pdf"));
064:         * document.open();
065:         * PdfContentByte cb = writer.getDirectContent();
066:         * Image img = Image.getInstance("image.png");
067:         * PushbuttonField bt = new PushbuttonField(writer, new Rectangle(100, 100, 200, 200), "Button1");
068:         * bt.setText("My Caption");
069:         * bt.setFontSize(0);
070:         * bt.setImage(img);
071:         * bt.setLayout(PushbuttonField.LAYOUT_ICON_TOP_LABEL_BOTTOM);
072:         * bt.setBackgroundColor(Color.cyan);
073:         * bt.setBorderStyle(PdfBorderDictionary.STYLE_SOLID);
074:         * bt.setBorderColor(Color.red);
075:         * bt.setBorderWidth(3);
076:         * PdfFormField ff = bt.getField();
077:         * PdfAction ac = PdfAction.createSubmitForm("http://www.submit-site.com", null, 0);
078:         * ff.setAction(ac);
079:         * writer.addAnnotation(ff);
080:         * document.close();
081:         * </PRE>
082:         * @author Paulo Soares (psoares@consiste.pt)
083:         */
084:        public class PushbuttonField extends BaseField {
085:
086:            /** A layout option */
087:            public static final int LAYOUT_LABEL_ONLY = 1;
088:            /** A layout option */
089:            public static final int LAYOUT_ICON_ONLY = 2;
090:            /** A layout option */
091:            public static final int LAYOUT_ICON_TOP_LABEL_BOTTOM = 3;
092:            /** A layout option */
093:            public static final int LAYOUT_LABEL_TOP_ICON_BOTTOM = 4;
094:            /** A layout option */
095:            public static final int LAYOUT_ICON_LEFT_LABEL_RIGHT = 5;
096:            /** A layout option */
097:            public static final int LAYOUT_LABEL_LEFT_ICON_RIGHT = 6;
098:            /** A layout option */
099:            public static final int LAYOUT_LABEL_OVER_ICON = 7;
100:            /** An icon scaling option */
101:            public static final int SCALE_ICON_ALWAYS = 1;
102:            /** An icon scaling option */
103:            public static final int SCALE_ICON_NEVER = 2;
104:            /** An icon scaling option */
105:            public static final int SCALE_ICON_IS_TOO_BIG = 3;
106:            /** An icon scaling option */
107:            public static final int SCALE_ICON_IS_TOO_SMALL = 4;
108:
109:            /**
110:             * Holds value of property layout.
111:             */
112:            private int layout = LAYOUT_LABEL_ONLY;
113:
114:            /**
115:             * Holds value of property image.
116:             */
117:            private Image image;
118:
119:            /**
120:             * Holds value of property template.
121:             */
122:            private PdfTemplate template;
123:
124:            /**
125:             * Holds value of property scaleIcon.
126:             */
127:            private int scaleIcon = SCALE_ICON_ALWAYS;
128:
129:            /**
130:             * Holds value of property proportionalIcon.
131:             */
132:            private boolean proportionalIcon = true;
133:
134:            /**
135:             * Holds value of property iconVerticalAdjustment.
136:             */
137:            private float iconVerticalAdjustment = 0.5f;
138:
139:            /**
140:             * Holds value of property iconHorizontalAdjustment.
141:             */
142:            private float iconHorizontalAdjustment = 0.5f;
143:
144:            /**
145:             * Holds value of property iconFitToBounds.
146:             */
147:            private boolean iconFitToBounds;
148:
149:            private PdfTemplate tp;
150:
151:            /**
152:             * Creates a new instance of PushbuttonField
153:             * @param writer the document <CODE>PdfWriter</CODE>
154:             * @param box the field location and dimensions
155:             * @param fieldName the field name. If <CODE>null</CODE> only the widget keys
156:             * will be included in the field allowing it to be used as a kid field.
157:             */
158:            public PushbuttonField(PdfWriter writer, Rectangle box,
159:                    String fieldName) {
160:                super (writer, box, fieldName);
161:            }
162:
163:            /**
164:             * Getter for property layout.
165:             * @return Value of property layout.
166:             */
167:            public int getLayout() {
168:                return this .layout;
169:            }
170:
171:            /**
172:             * Sets the icon and label layout. Possible values are <CODE>LAYOUT_LABEL_ONLY</CODE>,
173:             * <CODE>LAYOUT_ICON_ONLY</CODE>, <CODE>LAYOUT_ICON_TOP_LABEL_BOTTOM</CODE>,
174:             * <CODE>LAYOUT_LABEL_TOP_ICON_BOTTOM</CODE>, <CODE>LAYOUT_ICON_LEFT_LABEL_RIGHT</CODE>,
175:             * <CODE>LAYOUT_LABEL_LEFT_ICON_RIGHT</CODE> and <CODE>LAYOUT_LABEL_OVER_ICON</CODE>.
176:             * The default is <CODE>LAYOUT_LABEL_ONLY</CODE>.
177:             * @param layout New value of property layout.
178:             */
179:            public void setLayout(int layout) {
180:                if (layout < LAYOUT_LABEL_ONLY
181:                        || layout > LAYOUT_LABEL_OVER_ICON)
182:                    throw new IllegalArgumentException("Layout out of bounds.");
183:                this .layout = layout;
184:            }
185:
186:            /**
187:             * Getter for property image.
188:             * @return Value of property image.
189:             */
190:            public Image getImage() {
191:                return this .image;
192:            }
193:
194:            /**
195:             * Sets the icon as an image.
196:             * @param image the image
197:             */
198:            public void setImage(Image image) {
199:                this .image = image;
200:                template = null;
201:            }
202:
203:            /**
204:             * Getter for property template.
205:             * @return Value of property template.
206:             */
207:            public PdfTemplate getTemplate() {
208:                return this .template;
209:            }
210:
211:            /**
212:             * Sets the icon as a template.
213:             * @param template the template
214:             */
215:            public void setTemplate(PdfTemplate template) {
216:                this .template = template;
217:                image = null;
218:            }
219:
220:            /**
221:             * Getter for property scaleIcon.
222:             * @return Value of property scaleIcon.
223:             */
224:            public int getScaleIcon() {
225:                return this .scaleIcon;
226:            }
227:
228:            /**
229:             * Sets the way the icon will be scaled. Possible values are
230:             * <CODE>SCALE_ICON_ALWAYS</CODE>, <CODE>SCALE_ICON_NEVER</CODE>,
231:             * <CODE>SCALE_ICON_IS_TOO_BIG</CODE> and <CODE>SCALE_ICON_IS_TOO_SMALL</CODE>.
232:             * The default is <CODE>SCALE_ICON_ALWAYS</CODE>.
233:             * @param scaleIcon the way the icon will be scaled
234:             */
235:            public void setScaleIcon(int scaleIcon) {
236:                if (scaleIcon < SCALE_ICON_ALWAYS
237:                        || scaleIcon > SCALE_ICON_IS_TOO_SMALL)
238:                    scaleIcon = SCALE_ICON_ALWAYS;
239:                this .scaleIcon = scaleIcon;
240:            }
241:
242:            /**
243:             * Getter for property proportionalIcon.
244:             * @return Value of property proportionalIcon.
245:             */
246:            public boolean isProportionalIcon() {
247:                return this .proportionalIcon;
248:            }
249:
250:            /**
251:             * Sets the way the icon is scaled. If <CODE>true</CODE> the icon is scaled proportionally,
252:             * if <CODE>false</CODE> the scaling is done anamorphicaly.
253:             * @param proportionalIcon the way the icon is scaled
254:             */
255:            public void setProportionalIcon(boolean proportionalIcon) {
256:                this .proportionalIcon = proportionalIcon;
257:            }
258:
259:            /**
260:             * Getter for property iconVerticalAdjustment.
261:             * @return Value of property iconVerticalAdjustment.
262:             */
263:            public float getIconVerticalAdjustment() {
264:                return this .iconVerticalAdjustment;
265:            }
266:
267:            /**
268:             * A number between 0 and 1 indicating the fraction of leftover space to allocate at the bottom of the icon.
269:             * A value of 0 positions the icon at the bottom of the annotation rectangle.
270:             * A value of 0.5 centers it within the rectangle. The default is 0.5.
271:             * @param iconVerticalAdjustment a number between 0 and 1 indicating the fraction of leftover space to allocate at the bottom of the icon
272:             */
273:            public void setIconVerticalAdjustment(float iconVerticalAdjustment) {
274:                if (iconVerticalAdjustment < 0)
275:                    iconVerticalAdjustment = 0;
276:                else if (iconVerticalAdjustment > 1)
277:                    iconVerticalAdjustment = 1;
278:                this .iconVerticalAdjustment = iconVerticalAdjustment;
279:            }
280:
281:            /**
282:             * Getter for property iconHorizontalAdjustment.
283:             * @return Value of property iconHorizontalAdjustment.
284:             */
285:            public float getIconHorizontalAdjustment() {
286:                return this .iconHorizontalAdjustment;
287:            }
288:
289:            /**
290:             * A number between 0 and 1 indicating the fraction of leftover space to allocate at the left of the icon.
291:             * A value of 0 positions the icon at the left of the annotation rectangle.
292:             * A value of 0.5 centers it within the rectangle. The default is 0.5.
293:             * @param iconHorizontalAdjustment a number between 0 and 1 indicating the fraction of leftover space to allocate at the left of the icon
294:             */
295:            public void setIconHorizontalAdjustment(
296:                    float iconHorizontalAdjustment) {
297:                if (iconHorizontalAdjustment < 0)
298:                    iconHorizontalAdjustment = 0;
299:                else if (iconHorizontalAdjustment > 1)
300:                    iconHorizontalAdjustment = 1;
301:                this .iconHorizontalAdjustment = iconHorizontalAdjustment;
302:            }
303:
304:            private float calculateFontSize(float w, float h)
305:                    throws IOException, DocumentException {
306:                BaseFont ufont = getRealFont();
307:                float fsize = fontSize;
308:                if (fsize == 0) {
309:                    float bw = ufont.getWidthPoint(text, 1);
310:                    if (bw == 0)
311:                        fsize = 12;
312:                    else
313:                        fsize = w / bw;
314:                    float nfsize = h
315:                            / (1 - ufont.getFontDescriptor(BaseFont.DESCENT, 1));
316:                    fsize = Math.min(fsize, nfsize);
317:                    if (fsize < 4)
318:                        fsize = 4;
319:                }
320:                return fsize;
321:            }
322:
323:            /**
324:             * Gets the button appearance.
325:             * @throws IOException on error
326:             * @throws DocumentException on error
327:             * @return the button appearance
328:             */
329:            public PdfAppearance getAppearance() throws IOException,
330:                    DocumentException {
331:                PdfAppearance app = getBorderAppearance();
332:                Rectangle box = new Rectangle(app.getBoundingBox());
333:                if ((text == null || text.length() == 0)
334:                        && (layout == LAYOUT_LABEL_ONLY || (image == null
335:                                && template == null && iconReference == null))) {
336:                    return app;
337:                }
338:                if (layout == LAYOUT_ICON_ONLY && image == null
339:                        && template == null && iconReference == null)
340:                    return app;
341:                BaseFont ufont = getRealFont();
342:                boolean borderExtra = borderStyle == PdfBorderDictionary.STYLE_BEVELED
343:                        || borderStyle == PdfBorderDictionary.STYLE_INSET;
344:                float h = box.getHeight() - borderWidth * 2;
345:                float bw2 = borderWidth;
346:                if (borderExtra) {
347:                    h -= borderWidth * 2;
348:                    bw2 *= 2;
349:                }
350:                float offsetX = (borderExtra ? 2 * borderWidth : borderWidth);
351:                offsetX = Math.max(offsetX, 1);
352:                float offX = Math.min(bw2, offsetX);
353:                tp = null;
354:                float textX = Float.NaN;
355:                float textY = 0;
356:                float fsize = fontSize;
357:                float wt = box.getWidth() - 2 * offX - 2;
358:                float ht = box.getHeight() - 2 * offX;
359:                float adj = (iconFitToBounds ? 0 : offX + 1);
360:                int nlayout = layout;
361:                if (image == null && template == null && iconReference == null)
362:                    nlayout = LAYOUT_LABEL_ONLY;
363:                Rectangle iconBox = null;
364:                while (true) {
365:                    switch (nlayout) {
366:                    case LAYOUT_LABEL_ONLY:
367:                    case LAYOUT_LABEL_OVER_ICON:
368:                        if (text != null && text.length() > 0 && wt > 0
369:                                && ht > 0) {
370:                            fsize = calculateFontSize(wt, ht);
371:                            textX = (box.getWidth() - ufont.getWidthPoint(text,
372:                                    fsize)) / 2;
373:                            textY = (box.getHeight() - ufont.getFontDescriptor(
374:                                    BaseFont.ASCENT, fsize)) / 2;
375:                        }
376:                    case LAYOUT_ICON_ONLY:
377:                        if (nlayout == LAYOUT_LABEL_OVER_ICON
378:                                || nlayout == LAYOUT_ICON_ONLY)
379:                            iconBox = new Rectangle(box.getLeft() + adj, box
380:                                    .getBottom()
381:                                    + adj, box.getRight() - adj, box.getTop()
382:                                    - adj);
383:                        break;
384:                    case LAYOUT_ICON_TOP_LABEL_BOTTOM:
385:                        if (text == null || text.length() == 0 || wt <= 0
386:                                || ht <= 0) {
387:                            nlayout = LAYOUT_ICON_ONLY;
388:                            continue;
389:                        }
390:                        float nht = box.getHeight() * 0.35f - offX;
391:                        if (nht > 0)
392:                            fsize = calculateFontSize(wt, nht);
393:                        else
394:                            fsize = 4;
395:                        textX = (box.getWidth() - ufont.getWidthPoint(text,
396:                                fsize)) / 2;
397:                        textY = offX
398:                                - ufont.getFontDescriptor(BaseFont.DESCENT,
399:                                        fsize);
400:                        iconBox = new Rectangle(box.getLeft() + adj, textY
401:                                + fsize, box.getRight() - adj, box.getTop()
402:                                - adj);
403:                        break;
404:                    case LAYOUT_LABEL_TOP_ICON_BOTTOM:
405:                        if (text == null || text.length() == 0 || wt <= 0
406:                                || ht <= 0) {
407:                            nlayout = LAYOUT_ICON_ONLY;
408:                            continue;
409:                        }
410:                        nht = box.getHeight() * 0.35f - offX;
411:                        if (nht > 0)
412:                            fsize = calculateFontSize(wt, nht);
413:                        else
414:                            fsize = 4;
415:                        textX = (box.getWidth() - ufont.getWidthPoint(text,
416:                                fsize)) / 2;
417:                        textY = box.getHeight() - offX - fsize;
418:                        if (textY < offX)
419:                            textY = offX;
420:                        iconBox = new Rectangle(box.getLeft() + adj, box
421:                                .getBottom()
422:                                + adj, box.getRight() - adj, textY
423:                                + ufont.getFontDescriptor(BaseFont.DESCENT,
424:                                        fsize));
425:                        break;
426:                    case LAYOUT_LABEL_LEFT_ICON_RIGHT:
427:                        if (text == null || text.length() == 0 || wt <= 0
428:                                || ht <= 0) {
429:                            nlayout = LAYOUT_ICON_ONLY;
430:                            continue;
431:                        }
432:                        float nw = box.getWidth() * 0.35f - offX;
433:                        if (nw > 0)
434:                            fsize = calculateFontSize(wt, nw);
435:                        else
436:                            fsize = 4;
437:                        if (ufont.getWidthPoint(text, fsize) >= wt) {
438:                            nlayout = LAYOUT_LABEL_ONLY;
439:                            fsize = fontSize;
440:                            continue;
441:                        }
442:                        textX = offX + 1;
443:                        textY = (box.getHeight() - ufont.getFontDescriptor(
444:                                BaseFont.ASCENT, fsize)) / 2;
445:                        iconBox = new Rectangle(textX
446:                                + ufont.getWidthPoint(text, fsize), box
447:                                .getBottom()
448:                                + adj, box.getRight() - adj, box.getTop() - adj);
449:                        break;
450:                    case LAYOUT_ICON_LEFT_LABEL_RIGHT:
451:                        if (text == null || text.length() == 0 || wt <= 0
452:                                || ht <= 0) {
453:                            nlayout = LAYOUT_ICON_ONLY;
454:                            continue;
455:                        }
456:                        nw = box.getWidth() * 0.35f - offX;
457:                        if (nw > 0)
458:                            fsize = calculateFontSize(wt, nw);
459:                        else
460:                            fsize = 4;
461:                        if (ufont.getWidthPoint(text, fsize) >= wt) {
462:                            nlayout = LAYOUT_LABEL_ONLY;
463:                            fsize = fontSize;
464:                            continue;
465:                        }
466:                        textX = box.getWidth()
467:                                - ufont.getWidthPoint(text, fsize) - offX - 1;
468:                        textY = (box.getHeight() - ufont.getFontDescriptor(
469:                                BaseFont.ASCENT, fsize)) / 2;
470:                        iconBox = new Rectangle(box.getLeft() + adj, box
471:                                .getBottom()
472:                                + adj, textX - 1, box.getTop() - adj);
473:                        break;
474:                    }
475:                    break;
476:                }
477:                if (textY < box.getBottom() + offX)
478:                    textY = box.getBottom() + offX;
479:                if (iconBox != null
480:                        && (iconBox.getWidth() <= 0 || iconBox.getHeight() <= 0))
481:                    iconBox = null;
482:                boolean haveIcon = false;
483:                float boundingBoxWidth = 0;
484:                float boundingBoxHeight = 0;
485:                PdfArray matrix = null;
486:                if (iconBox != null) {
487:                    if (image != null) {
488:                        tp = new PdfTemplate(writer);
489:                        tp.setBoundingBox(new Rectangle(image));
490:                        writer.addDirectTemplateSimple(tp, PdfName.FRM);
491:                        tp.addImage(image, image.getWidth(), 0, 0, image
492:                                .getHeight(), 0, 0);
493:                        haveIcon = true;
494:                        boundingBoxWidth = tp.getBoundingBox().getWidth();
495:                        boundingBoxHeight = tp.getBoundingBox().getHeight();
496:                    } else if (template != null) {
497:                        tp = new PdfTemplate(writer);
498:                        tp.setBoundingBox(new Rectangle(template.getWidth(),
499:                                template.getHeight()));
500:                        writer.addDirectTemplateSimple(tp, PdfName.FRM);
501:                        tp.addTemplate(template, template.getBoundingBox()
502:                                .getLeft(), template.getBoundingBox()
503:                                .getBottom());
504:                        haveIcon = true;
505:                        boundingBoxWidth = tp.getBoundingBox().getWidth();
506:                        boundingBoxHeight = tp.getBoundingBox().getHeight();
507:                    } else if (iconReference != null) {
508:                        PdfDictionary dic = (PdfDictionary) PdfReader
509:                                .getPdfObject(iconReference);
510:                        if (dic != null) {
511:                            Rectangle r2 = PdfReader
512:                                    .getNormalizedRectangle((PdfArray) PdfReader
513:                                            .getPdfObject(dic.get(PdfName.BBOX)));
514:                            matrix = (PdfArray) PdfReader.getPdfObject(dic
515:                                    .get(PdfName.MATRIX));
516:                            haveIcon = true;
517:                            boundingBoxWidth = r2.getWidth();
518:                            boundingBoxHeight = r2.getHeight();
519:                        }
520:                    }
521:                }
522:                if (haveIcon) {
523:                    float icx = iconBox.getWidth() / boundingBoxWidth;
524:                    float icy = iconBox.getHeight() / boundingBoxHeight;
525:                    if (proportionalIcon) {
526:                        switch (scaleIcon) {
527:                        case SCALE_ICON_IS_TOO_BIG:
528:                            icx = Math.min(icx, icy);
529:                            icx = Math.min(icx, 1);
530:                            break;
531:                        case SCALE_ICON_IS_TOO_SMALL:
532:                            icx = Math.min(icx, icy);
533:                            icx = Math.max(icx, 1);
534:                            break;
535:                        case SCALE_ICON_NEVER:
536:                            icx = 1;
537:                            break;
538:                        default:
539:                            icx = Math.min(icx, icy);
540:                            break;
541:                        }
542:                        icy = icx;
543:                    } else {
544:                        switch (scaleIcon) {
545:                        case SCALE_ICON_IS_TOO_BIG:
546:                            icx = Math.min(icx, 1);
547:                            icy = Math.min(icy, 1);
548:                            break;
549:                        case SCALE_ICON_IS_TOO_SMALL:
550:                            icx = Math.max(icx, 1);
551:                            icy = Math.max(icy, 1);
552:                            break;
553:                        case SCALE_ICON_NEVER:
554:                            icx = icy = 1;
555:                            break;
556:                        default:
557:                            break;
558:                        }
559:                    }
560:                    float xpos = iconBox.getLeft()
561:                            + (iconBox.getWidth() - (boundingBoxWidth * icx))
562:                            * iconHorizontalAdjustment;
563:                    float ypos = iconBox.getBottom()
564:                            + (iconBox.getHeight() - (boundingBoxHeight * icy))
565:                            * iconVerticalAdjustment;
566:                    app.saveState();
567:                    app.rectangle(iconBox.getLeft(), iconBox.getBottom(),
568:                            iconBox.getWidth(), iconBox.getHeight());
569:                    app.clip();
570:                    app.newPath();
571:                    if (tp != null)
572:                        app.addTemplate(tp, icx, 0, 0, icy, xpos, ypos);
573:                    else {
574:                        float cox = 0;
575:                        float coy = 0;
576:                        if (matrix != null && matrix.size() == 6) {
577:                            PdfNumber nm = (PdfNumber) PdfReader
578:                                    .getPdfObject((PdfObject) matrix
579:                                            .getArrayList().get(4));
580:                            if (nm != null)
581:                                cox = nm.floatValue();
582:                            nm = (PdfNumber) PdfReader
583:                                    .getPdfObject((PdfObject) matrix
584:                                            .getArrayList().get(5));
585:                            if (nm != null)
586:                                coy = nm.floatValue();
587:                        }
588:                        app.addTemplateReference(iconReference, PdfName.FRM,
589:                                icx, 0, 0, icy, xpos - cox * icx, ypos - coy
590:                                        * icy);
591:                    }
592:                    app.restoreState();
593:                }
594:                if (!Float.isNaN(textX)) {
595:                    app.saveState();
596:                    app.rectangle(offX, offX, box.getWidth() - 2 * offX, box
597:                            .getHeight()
598:                            - 2 * offX);
599:                    app.clip();
600:                    app.newPath();
601:                    if (textColor == null)
602:                        app.resetGrayFill();
603:                    else
604:                        app.setColorFill(textColor);
605:                    app.beginText();
606:                    app.setFontAndSize(ufont, fsize);
607:                    app.setTextMatrix(textX, textY);
608:                    app.showText(text);
609:                    app.endText();
610:                    app.restoreState();
611:                }
612:                return app;
613:            }
614:
615:            /**
616:             * Gets the pushbutton field.
617:             * @throws IOException on error
618:             * @throws DocumentException on error
619:             * @return the pushbutton field
620:             */
621:            public PdfFormField getField() throws IOException,
622:                    DocumentException {
623:                PdfFormField field = PdfFormField.createPushButton(writer);
624:                field.setWidget(box, PdfAnnotation.HIGHLIGHT_INVERT);
625:                if (fieldName != null) {
626:                    field.setFieldName(fieldName);
627:                    if ((options & READ_ONLY) != 0)
628:                        field.setFieldFlags(PdfFormField.FF_READ_ONLY);
629:                    if ((options & REQUIRED) != 0)
630:                        field.setFieldFlags(PdfFormField.FF_REQUIRED);
631:                }
632:                if (text != null)
633:                    field.setMKNormalCaption(text);
634:                if (rotation != 0)
635:                    field.setMKRotation(rotation);
636:                field.setBorderStyle(new PdfBorderDictionary(borderWidth,
637:                        borderStyle, new PdfDashPattern(3)));
638:                PdfAppearance tpa = getAppearance();
639:                field.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, tpa);
640:                PdfAppearance da = (PdfAppearance) tpa.getDuplicate();
641:                da.setFontAndSize(getRealFont(), fontSize);
642:                if (textColor == null)
643:                    da.setGrayFill(0);
644:                else
645:                    da.setColorFill(textColor);
646:                field.setDefaultAppearanceString(da);
647:                if (borderColor != null)
648:                    field.setMKBorderColor(borderColor);
649:                if (backgroundColor != null)
650:                    field.setMKBackgroundColor(backgroundColor);
651:                switch (visibility) {
652:                case HIDDEN:
653:                    field.setFlags(PdfAnnotation.FLAGS_PRINT
654:                            | PdfAnnotation.FLAGS_HIDDEN);
655:                    break;
656:                case VISIBLE_BUT_DOES_NOT_PRINT:
657:                    break;
658:                case HIDDEN_BUT_PRINTABLE:
659:                    field.setFlags(PdfAnnotation.FLAGS_PRINT
660:                            | PdfAnnotation.FLAGS_NOVIEW);
661:                    break;
662:                default:
663:                    field.setFlags(PdfAnnotation.FLAGS_PRINT);
664:                    break;
665:                }
666:                if (tp != null)
667:                    field.setMKNormalIcon(tp);
668:                field.setMKTextPosition(layout - 1);
669:                PdfName scale = PdfName.A;
670:                if (scaleIcon == SCALE_ICON_IS_TOO_BIG)
671:                    scale = PdfName.B;
672:                else if (scaleIcon == SCALE_ICON_IS_TOO_SMALL)
673:                    scale = PdfName.S;
674:                else if (scaleIcon == SCALE_ICON_NEVER)
675:                    scale = PdfName.N;
676:                field.setMKIconFit(scale, proportionalIcon ? PdfName.P
677:                        : PdfName.A, iconHorizontalAdjustment,
678:                        iconVerticalAdjustment, iconFitToBounds);
679:                return field;
680:            }
681:
682:            /**
683:             * Getter for property iconFitToBounds.
684:             * @return Value of property iconFitToBounds.
685:             */
686:            public boolean isIconFitToBounds() {
687:                return this .iconFitToBounds;
688:            }
689:
690:            /**
691:             * If <CODE>true</CODE> the icon will be scaled to fit fully within the bounds of the annotation,
692:             * if <CODE>false</CODE> the border width will be taken into account. The default
693:             * is <CODE>false</CODE>.
694:             * @param iconFitToBounds if <CODE>true</CODE> the icon will be scaled to fit fully within the bounds of the annotation,
695:             * if <CODE>false</CODE> the border width will be taken into account
696:             */
697:            public void setIconFitToBounds(boolean iconFitToBounds) {
698:                this .iconFitToBounds = iconFitToBounds;
699:            }
700:
701:            /**
702:             * Holds value of property iconReference.
703:             */
704:            private PRIndirectReference iconReference;
705:
706:            /**
707:             * Gets the reference to an existing icon.
708:             * @return the reference to an existing icon.
709:             */
710:            public PRIndirectReference getIconReference() {
711:                return this .iconReference;
712:            }
713:
714:            /**
715:             * Sets the reference to an existing icon.
716:             * @param iconReference the reference to an existing icon
717:             */
718:            public void setIconReference(PRIndirectReference iconReference) {
719:                this.iconReference = iconReference;
720:            }
721:
722:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.