Source Code Cross Referenced for PdfAnnotation.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:         * $Id: PdfAnnotation.java 2911 2007-09-06 06:39:00Z xlv $
003:         * $Name$
004:         *
005:         * Copyright 1999, 2000, 2001, 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.awt.Color;
054:        import java.io.IOException;
055:        import java.util.HashMap;
056:
057:        import com.lowagie.text.Rectangle;
058:
059:        /**
060:         * A <CODE>PdfAnnotation</CODE> is a note that is associated with a page.
061:         *
062:         * @see		PdfDictionary
063:         */
064:
065:        public class PdfAnnotation extends PdfDictionary {
066:            /** highlight attributename */
067:            public static final PdfName HIGHLIGHT_NONE = PdfName.N;
068:            /** highlight attributename */
069:            public static final PdfName HIGHLIGHT_INVERT = PdfName.I;
070:            /** highlight attributename */
071:            public static final PdfName HIGHLIGHT_OUTLINE = PdfName.O;
072:            /** highlight attributename */
073:            public static final PdfName HIGHLIGHT_PUSH = PdfName.P;
074:            /** highlight attributename */
075:            public static final PdfName HIGHLIGHT_TOGGLE = PdfName.T;
076:            /** flagvalue */
077:            public static final int FLAGS_INVISIBLE = 1;
078:            /** flagvalue */
079:            public static final int FLAGS_HIDDEN = 2;
080:            /** flagvalue */
081:            public static final int FLAGS_PRINT = 4;
082:            /** flagvalue */
083:            public static final int FLAGS_NOZOOM = 8;
084:            /** flagvalue */
085:            public static final int FLAGS_NOROTATE = 16;
086:            /** flagvalue */
087:            public static final int FLAGS_NOVIEW = 32;
088:            /** flagvalue */
089:            public static final int FLAGS_READONLY = 64;
090:            /** flagvalue */
091:            public static final int FLAGS_LOCKED = 128;
092:            /** flagvalue */
093:            public static final int FLAGS_TOGGLENOVIEW = 256;
094:            /** appearance attributename */
095:            public static final PdfName APPEARANCE_NORMAL = PdfName.N;
096:            /** appearance attributename */
097:            public static final PdfName APPEARANCE_ROLLOVER = PdfName.R;
098:            /** appearance attributename */
099:            public static final PdfName APPEARANCE_DOWN = PdfName.D;
100:            /** attributevalue */
101:            public static final PdfName AA_ENTER = PdfName.E;
102:            /** attributevalue */
103:            public static final PdfName AA_EXIT = PdfName.X;
104:            /** attributevalue */
105:            public static final PdfName AA_DOWN = PdfName.D;
106:            /** attributevalue */
107:            public static final PdfName AA_UP = PdfName.U;
108:            /** attributevalue */
109:            public static final PdfName AA_FOCUS = PdfName.FO;
110:            /** attributevalue */
111:            public static final PdfName AA_BLUR = PdfName.BL;
112:            /** attributevalue */
113:            public static final PdfName AA_JS_KEY = PdfName.K;
114:            /** attributevalue */
115:            public static final PdfName AA_JS_FORMAT = PdfName.F;
116:            /** attributevalue */
117:            public static final PdfName AA_JS_CHANGE = PdfName.V;
118:            /** attributevalue */
119:            public static final PdfName AA_JS_OTHER_CHANGE = PdfName.C;
120:            /** attributevalue */
121:            public static final int MARKUP_HIGHLIGHT = 0;
122:            /** attributevalue */
123:            public static final int MARKUP_UNDERLINE = 1;
124:            /** attributevalue */
125:            public static final int MARKUP_STRIKEOUT = 2;
126:
127:            protected PdfWriter writer;
128:            protected PdfIndirectReference reference;
129:            protected HashMap templates;
130:            protected boolean form = false;
131:            protected boolean annotation = true;
132:
133:            /** Holds value of property used. */
134:            protected boolean used = false;
135:
136:            /** Holds value of property placeInPage. */
137:            private int placeInPage = -1;
138:
139:            // constructors
140:            public PdfAnnotation(PdfWriter writer, Rectangle rect) {
141:                this .writer = writer;
142:                if (rect != null)
143:                    put(PdfName.RECT, new PdfRectangle(rect));
144:            }
145:
146:            /**
147:             * Constructs a new <CODE>PdfAnnotation</CODE> of subtype text.
148:             * @param writer
149:             * @param llx
150:             * @param lly
151:             * @param urx
152:             * @param ury
153:             * @param title
154:             * @param content
155:             */
156:
157:            public PdfAnnotation(PdfWriter writer, float llx, float lly,
158:                    float urx, float ury, PdfString title, PdfString content) {
159:                this .writer = writer;
160:                put(PdfName.SUBTYPE, PdfName.TEXT);
161:                put(PdfName.T, title);
162:                put(PdfName.RECT, new PdfRectangle(llx, lly, urx, ury));
163:                put(PdfName.CONTENTS, content);
164:            }
165:
166:            /**
167:             * Constructs a new <CODE>PdfAnnotation</CODE> of subtype link (Action).
168:             * @param writer
169:             * @param llx
170:             * @param lly
171:             * @param urx
172:             * @param ury
173:             * @param action
174:             */
175:
176:            public PdfAnnotation(PdfWriter writer, float llx, float lly,
177:                    float urx, float ury, PdfAction action) {
178:                this .writer = writer;
179:                put(PdfName.SUBTYPE, PdfName.LINK);
180:                put(PdfName.RECT, new PdfRectangle(llx, lly, urx, ury));
181:                put(PdfName.A, action);
182:                put(PdfName.BORDER, new PdfBorderArray(0, 0, 0));
183:                put(PdfName.C, new PdfColor(0x00, 0x00, 0xFF));
184:            }
185:
186:            /**
187:             * Creates a screen PdfAnnotation
188:             * @param writer
189:             * @param rect
190:             * @param clipTitle
191:             * @param fs
192:             * @param mimeType
193:             * @param playOnDisplay
194:             * @return a screen PdfAnnotation
195:             * @throws IOException
196:             */
197:            public static PdfAnnotation createScreen(PdfWriter writer,
198:                    Rectangle rect, String clipTitle, PdfFileSpecification fs,
199:                    String mimeType, boolean playOnDisplay) throws IOException {
200:                PdfAnnotation ann = new PdfAnnotation(writer, rect);
201:                ann.put(PdfName.SUBTYPE, PdfName.SCREEN);
202:                ann.put(PdfName.F, new PdfNumber(FLAGS_PRINT));
203:                ann.put(PdfName.TYPE, PdfName.ANNOT);
204:                ann.setPage();
205:                PdfIndirectReference ref = ann.getIndirectReference();
206:                PdfAction action = PdfAction.rendition(clipTitle, fs, mimeType,
207:                        ref);
208:                PdfIndirectReference actionRef = writer.addToBody(action)
209:                        .getIndirectReference();
210:                // for play on display add trigger event
211:                if (playOnDisplay) {
212:                    PdfDictionary aa = new PdfDictionary();
213:                    aa.put(new PdfName("PV"), actionRef);
214:                    ann.put(PdfName.AA, aa);
215:                }
216:                ann.put(PdfName.A, actionRef);
217:                return ann;
218:            }
219:
220:            public PdfIndirectReference getIndirectReference() {
221:                if (reference == null) {
222:                    reference = writer.getPdfIndirectReference();
223:                }
224:                return reference;
225:            }
226:
227:            /**
228:             * @param writer
229:             * @param rect
230:             * @param title
231:             * @param contents
232:             * @param open
233:             * @param icon
234:             * @return a PdfAnnotation
235:             */
236:            public static PdfAnnotation createText(PdfWriter writer,
237:                    Rectangle rect, String title, String contents,
238:                    boolean open, String icon) {
239:                PdfAnnotation annot = new PdfAnnotation(writer, rect);
240:                annot.put(PdfName.SUBTYPE, PdfName.TEXT);
241:                if (title != null)
242:                    annot.put(PdfName.T, new PdfString(title,
243:                            PdfObject.TEXT_UNICODE));
244:                if (contents != null)
245:                    annot.put(PdfName.CONTENTS, new PdfString(contents,
246:                            PdfObject.TEXT_UNICODE));
247:                if (open)
248:                    annot.put(PdfName.OPEN, PdfBoolean.PDFTRUE);
249:                if (icon != null) {
250:                    annot.put(PdfName.NAME, new PdfName(icon));
251:                }
252:                return annot;
253:            }
254:
255:            /**
256:             * Creates a link.
257:             * @param writer
258:             * @param rect
259:             * @param highlight
260:             * @return A PdfAnnotation
261:             */
262:            protected static PdfAnnotation createLink(PdfWriter writer,
263:                    Rectangle rect, PdfName highlight) {
264:                PdfAnnotation annot = new PdfAnnotation(writer, rect);
265:                annot.put(PdfName.SUBTYPE, PdfName.LINK);
266:                if (!highlight.equals(HIGHLIGHT_INVERT))
267:                    annot.put(PdfName.H, highlight);
268:                return annot;
269:            }
270:
271:            /**
272:             * Creates an Annotation with an Action.
273:             * @param writer
274:             * @param rect
275:             * @param highlight
276:             * @param action
277:             * @return A PdfAnnotation
278:             */
279:            public static PdfAnnotation createLink(PdfWriter writer,
280:                    Rectangle rect, PdfName highlight, PdfAction action) {
281:                PdfAnnotation annot = createLink(writer, rect, highlight);
282:                annot.putEx(PdfName.A, action);
283:                return annot;
284:            }
285:
286:            /**
287:             * Creates an Annotation with an local destination.
288:             * @param writer
289:             * @param rect
290:             * @param highlight
291:             * @param namedDestination
292:             * @return A PdfAnnotation
293:             */
294:            public static PdfAnnotation createLink(PdfWriter writer,
295:                    Rectangle rect, PdfName highlight, String namedDestination) {
296:                PdfAnnotation annot = createLink(writer, rect, highlight);
297:                annot.put(PdfName.DEST, new PdfString(namedDestination));
298:                return annot;
299:            }
300:
301:            /**
302:             * Creates an Annotation with a PdfDestination.
303:             * @param writer
304:             * @param rect
305:             * @param highlight
306:             * @param page
307:             * @param dest
308:             * @return A PdfAnnotation
309:             */
310:            public static PdfAnnotation createLink(PdfWriter writer,
311:                    Rectangle rect, PdfName highlight, int page,
312:                    PdfDestination dest) {
313:                PdfAnnotation annot = createLink(writer, rect, highlight);
314:                PdfIndirectReference ref = writer.getPageReference(page);
315:                dest.addPage(ref);
316:                annot.put(PdfName.DEST, dest);
317:                return annot;
318:            }
319:
320:            /**
321:             * Add some free text to the document.
322:             * @param writer
323:             * @param rect
324:             * @param contents
325:             * @param defaultAppearance
326:             * @return A PdfAnnotation
327:             */
328:            public static PdfAnnotation createFreeText(PdfWriter writer,
329:                    Rectangle rect, String contents,
330:                    PdfContentByte defaultAppearance) {
331:                PdfAnnotation annot = new PdfAnnotation(writer, rect);
332:                annot.put(PdfName.SUBTYPE, PdfName.FREETEXT);
333:                annot.put(PdfName.CONTENTS, new PdfString(contents,
334:                        PdfObject.TEXT_UNICODE));
335:                annot.setDefaultAppearanceString(defaultAppearance);
336:                return annot;
337:            }
338:
339:            /**
340:             * Adds a line to the document. Move over the line and a tooltip is shown.
341:             * @param writer
342:             * @param rect
343:             * @param contents
344:             * @param x1
345:             * @param y1
346:             * @param x2
347:             * @param y2
348:             * @return A PdfAnnotation
349:             */
350:            public static PdfAnnotation createLine(PdfWriter writer,
351:                    Rectangle rect, String contents, float x1, float y1,
352:                    float x2, float y2) {
353:                PdfAnnotation annot = new PdfAnnotation(writer, rect);
354:                annot.put(PdfName.SUBTYPE, PdfName.LINE);
355:                annot.put(PdfName.CONTENTS, new PdfString(contents,
356:                        PdfObject.TEXT_UNICODE));
357:                PdfArray array = new PdfArray(new PdfNumber(x1));
358:                array.add(new PdfNumber(y1));
359:                array.add(new PdfNumber(x2));
360:                array.add(new PdfNumber(y2));
361:                annot.put(PdfName.L, array);
362:                return annot;
363:            }
364:
365:            /**
366:             * Adds a circle or a square that shows a tooltip when you pass over it.
367:             * @param writer
368:             * @param rect
369:             * @param contents The tooltip
370:             * @param square true if you want a square, false if you want a circle
371:             * @return A PdfAnnotation
372:             */
373:            public static PdfAnnotation createSquareCircle(PdfWriter writer,
374:                    Rectangle rect, String contents, boolean square) {
375:                PdfAnnotation annot = new PdfAnnotation(writer, rect);
376:                if (square)
377:                    annot.put(PdfName.SUBTYPE, PdfName.SQUARE);
378:                else
379:                    annot.put(PdfName.SUBTYPE, PdfName.CIRCLE);
380:                annot.put(PdfName.CONTENTS, new PdfString(contents,
381:                        PdfObject.TEXT_UNICODE));
382:                return annot;
383:            }
384:
385:            public static PdfAnnotation createMarkup(PdfWriter writer,
386:                    Rectangle rect, String contents, int type,
387:                    float quadPoints[]) {
388:                PdfAnnotation annot = new PdfAnnotation(writer, rect);
389:                PdfName name = PdfName.HIGHLIGHT;
390:                switch (type) {
391:                case MARKUP_UNDERLINE:
392:                    name = PdfName.UNDERLINE;
393:                    break;
394:                case MARKUP_STRIKEOUT:
395:                    name = PdfName.STRIKEOUT;
396:                    break;
397:                }
398:                annot.put(PdfName.SUBTYPE, name);
399:                annot.put(PdfName.CONTENTS, new PdfString(contents,
400:                        PdfObject.TEXT_UNICODE));
401:                PdfArray array = new PdfArray();
402:                for (int k = 0; k < quadPoints.length; ++k)
403:                    array.add(new PdfNumber(quadPoints[k]));
404:                annot.put(PdfName.QUADPOINTS, array);
405:                return annot;
406:            }
407:
408:            /**
409:             * Adds a Stamp to your document. Move over the stamp and a tooltip is shown
410:             * @param writer
411:             * @param rect
412:             * @param contents
413:             * @param name
414:             * @return A PdfAnnotation
415:             */
416:            public static PdfAnnotation createStamp(PdfWriter writer,
417:                    Rectangle rect, String contents, String name) {
418:                PdfAnnotation annot = new PdfAnnotation(writer, rect);
419:                annot.put(PdfName.SUBTYPE, PdfName.STAMP);
420:                annot.put(PdfName.CONTENTS, new PdfString(contents,
421:                        PdfObject.TEXT_UNICODE));
422:                annot.put(PdfName.NAME, new PdfName(name));
423:                return annot;
424:            }
425:
426:            public static PdfAnnotation createInk(PdfWriter writer,
427:                    Rectangle rect, String contents, float inkList[][]) {
428:                PdfAnnotation annot = new PdfAnnotation(writer, rect);
429:                annot.put(PdfName.SUBTYPE, PdfName.INK);
430:                annot.put(PdfName.CONTENTS, new PdfString(contents,
431:                        PdfObject.TEXT_UNICODE));
432:                PdfArray outer = new PdfArray();
433:                for (int k = 0; k < inkList.length; ++k) {
434:                    PdfArray inner = new PdfArray();
435:                    float deep[] = inkList[k];
436:                    for (int j = 0; j < deep.length; ++j)
437:                        inner.add(new PdfNumber(deep[j]));
438:                    outer.add(inner);
439:                }
440:                annot.put(PdfName.INKLIST, outer);
441:                return annot;
442:            }
443:
444:            /** Creates a file attachment annotation.
445:             * @param writer the <CODE>PdfWriter</CODE>
446:             * @param rect the dimensions in the page of the annotation
447:             * @param contents the file description
448:             * @param fileStore an array with the file. If it's <CODE>null</CODE>
449:             * the file will be read from the disk
450:             * @param file the path to the file. It will only be used if
451:             * <CODE>fileStore</CODE> is not <CODE>null</CODE>
452:             * @param fileDisplay the actual file name stored in the pdf
453:             * @throws IOException on error
454:             * @return the annotation
455:             */
456:            public static PdfAnnotation createFileAttachment(PdfWriter writer,
457:                    Rectangle rect, String contents, byte fileStore[],
458:                    String file, String fileDisplay) throws IOException {
459:                return createFileAttachment(writer, rect, contents,
460:                        PdfFileSpecification.fileEmbedded(writer, file,
461:                                fileDisplay, fileStore));
462:            }
463:
464:            /** Creates a file attachment annotation
465:             * @param writer
466:             * @param rect
467:             * @param contents
468:             * @param fs
469:             * @return the annotation
470:             * @throws IOException
471:             */
472:            public static PdfAnnotation createFileAttachment(PdfWriter writer,
473:                    Rectangle rect, String contents, PdfFileSpecification fs)
474:                    throws IOException {
475:                PdfAnnotation annot = new PdfAnnotation(writer, rect);
476:                annot.put(PdfName.SUBTYPE, PdfName.FILEATTACHMENT);
477:                if (contents != null)
478:                    annot.put(PdfName.CONTENTS, new PdfString(contents,
479:                            PdfObject.TEXT_UNICODE));
480:                annot.put(PdfName.FS, fs.getReference());
481:                return annot;
482:            }
483:
484:            /**
485:             * Adds a popup to your document.
486:             * @param writer
487:             * @param rect
488:             * @param contents
489:             * @param open
490:             * @return A PdfAnnotation
491:             */
492:            public static PdfAnnotation createPopup(PdfWriter writer,
493:                    Rectangle rect, String contents, boolean open) {
494:                PdfAnnotation annot = new PdfAnnotation(writer, rect);
495:                annot.put(PdfName.SUBTYPE, PdfName.POPUP);
496:                if (contents != null)
497:                    annot.put(PdfName.CONTENTS, new PdfString(contents,
498:                            PdfObject.TEXT_UNICODE));
499:                if (open)
500:                    annot.put(PdfName.OPEN, PdfBoolean.PDFTRUE);
501:                return annot;
502:            }
503:
504:            public void setDefaultAppearanceString(PdfContentByte cb) {
505:                byte b[] = cb.getInternalBuffer().toByteArray();
506:                int len = b.length;
507:                for (int k = 0; k < len; ++k) {
508:                    if (b[k] == '\n')
509:                        b[k] = 32;
510:                }
511:                put(PdfName.DA, new PdfString(b));
512:            }
513:
514:            public void setFlags(int flags) {
515:                if (flags == 0)
516:                    remove(PdfName.F);
517:                else
518:                    put(PdfName.F, new PdfNumber(flags));
519:            }
520:
521:            public void setBorder(PdfBorderArray border) {
522:                put(PdfName.BORDER, border);
523:            }
524:
525:            public void setBorderStyle(PdfBorderDictionary border) {
526:                put(PdfName.BS, border);
527:            }
528:
529:            /**
530:             * Sets the annotation's highlighting mode. The values can be
531:             * <CODE>HIGHLIGHT_NONE</CODE>, <CODE>HIGHLIGHT_INVERT</CODE>,
532:             * <CODE>HIGHLIGHT_OUTLINE</CODE> and <CODE>HIGHLIGHT_PUSH</CODE>;
533:             * @param highlight the annotation's highlighting mode
534:             */
535:            public void setHighlighting(PdfName highlight) {
536:                if (highlight.equals(HIGHLIGHT_INVERT))
537:                    remove(PdfName.H);
538:                else
539:                    put(PdfName.H, highlight);
540:            }
541:
542:            public void setAppearance(PdfName ap, PdfTemplate template) {
543:                PdfDictionary dic = (PdfDictionary) get(PdfName.AP);
544:                if (dic == null)
545:                    dic = new PdfDictionary();
546:                dic.put(ap, template.getIndirectReference());
547:                put(PdfName.AP, dic);
548:                if (!form)
549:                    return;
550:                if (templates == null)
551:                    templates = new HashMap();
552:                templates.put(template, null);
553:            }
554:
555:            public void setAppearance(PdfName ap, String state,
556:                    PdfTemplate template) {
557:                PdfDictionary dicAp = (PdfDictionary) get(PdfName.AP);
558:                if (dicAp == null)
559:                    dicAp = new PdfDictionary();
560:
561:                PdfDictionary dic;
562:                PdfObject obj = dicAp.get(ap);
563:                if (obj != null && obj.isDictionary())
564:                    dic = (PdfDictionary) obj;
565:                else
566:                    dic = new PdfDictionary();
567:                dic.put(new PdfName(state), template.getIndirectReference());
568:                dicAp.put(ap, dic);
569:                put(PdfName.AP, dicAp);
570:                if (!form)
571:                    return;
572:                if (templates == null)
573:                    templates = new HashMap();
574:                templates.put(template, null);
575:            }
576:
577:            public void setAppearanceState(String state) {
578:                if (state == null) {
579:                    remove(PdfName.AS);
580:                    return;
581:                }
582:                put(PdfName.AS, new PdfName(state));
583:            }
584:
585:            public void setColor(Color color) {
586:                put(PdfName.C, new PdfColor(color));
587:            }
588:
589:            public void setTitle(String title) {
590:                if (title == null) {
591:                    remove(PdfName.T);
592:                    return;
593:                }
594:                put(PdfName.T, new PdfString(title, PdfObject.TEXT_UNICODE));
595:            }
596:
597:            public void setPopup(PdfAnnotation popup) {
598:                put(PdfName.POPUP, popup.getIndirectReference());
599:                popup.put(PdfName.PARENT, getIndirectReference());
600:            }
601:
602:            public void setAction(PdfAction action) {
603:                put(PdfName.A, action);
604:            }
605:
606:            public void setAdditionalActions(PdfName key, PdfAction action) {
607:                PdfDictionary dic;
608:                PdfObject obj = get(PdfName.AA);
609:                if (obj != null && obj.isDictionary())
610:                    dic = (PdfDictionary) obj;
611:                else
612:                    dic = new PdfDictionary();
613:                dic.put(key, action);
614:                put(PdfName.AA, dic);
615:            }
616:
617:            /** Getter for property used.
618:             * @return Value of property used.
619:             */
620:            public boolean isUsed() {
621:                return used;
622:            }
623:
624:            /** Setter for property used.
625:             */
626:            public void setUsed() {
627:                used = true;
628:            }
629:
630:            public HashMap getTemplates() {
631:                return templates;
632:            }
633:
634:            /** Getter for property form.
635:             * @return Value of property form.
636:             */
637:            public boolean isForm() {
638:                return form;
639:            }
640:
641:            /** Getter for property annotation.
642:             * @return Value of property annotation.
643:             */
644:            public boolean isAnnotation() {
645:                return annotation;
646:            }
647:
648:            public void setPage(int page) {
649:                put(PdfName.P, writer.getPageReference(page));
650:            }
651:
652:            public void setPage() {
653:                put(PdfName.P, writer.getCurrentPage());
654:            }
655:
656:            /** Getter for property placeInPage.
657:             * @return Value of property placeInPage.
658:             */
659:            public int getPlaceInPage() {
660:                return placeInPage;
661:            }
662:
663:            /** Places the annotation in a specified page that must be greater
664:             * or equal to the current one. With <code>PdfStamper</code> the page
665:             * can be any. The first page is 1.
666:             * @param placeInPage New value of property placeInPage.
667:             */
668:            public void setPlaceInPage(int placeInPage) {
669:                this .placeInPage = placeInPage;
670:            }
671:
672:            public void setRotate(int v) {
673:                put(PdfName.ROTATE, new PdfNumber(v));
674:            }
675:
676:            PdfDictionary getMK() {
677:                PdfDictionary mk = (PdfDictionary) get(PdfName.MK);
678:                if (mk == null) {
679:                    mk = new PdfDictionary();
680:                    put(PdfName.MK, mk);
681:                }
682:                return mk;
683:            }
684:
685:            public void setMKRotation(int rotation) {
686:                getMK().put(PdfName.R, new PdfNumber(rotation));
687:            }
688:
689:            public static PdfArray getMKColor(Color color) {
690:                PdfArray array = new PdfArray();
691:                int type = ExtendedColor.getType(color);
692:                switch (type) {
693:                case ExtendedColor.TYPE_GRAY: {
694:                    array.add(new PdfNumber(((GrayColor) color).getGray()));
695:                    break;
696:                }
697:                case ExtendedColor.TYPE_CMYK: {
698:                    CMYKColor cmyk = (CMYKColor) color;
699:                    array.add(new PdfNumber(cmyk.getCyan()));
700:                    array.add(new PdfNumber(cmyk.getMagenta()));
701:                    array.add(new PdfNumber(cmyk.getYellow()));
702:                    array.add(new PdfNumber(cmyk.getBlack()));
703:                    break;
704:                }
705:                case ExtendedColor.TYPE_SEPARATION:
706:                case ExtendedColor.TYPE_PATTERN:
707:                case ExtendedColor.TYPE_SHADING:
708:                    throw new RuntimeException(
709:                            "Separations, patterns and shadings are not allowed in MK dictionary.");
710:                default:
711:                    array.add(new PdfNumber(color.getRed() / 255f));
712:                    array.add(new PdfNumber(color.getGreen() / 255f));
713:                    array.add(new PdfNumber(color.getBlue() / 255f));
714:                }
715:                return array;
716:            }
717:
718:            public void setMKBorderColor(Color color) {
719:                if (color == null)
720:                    getMK().remove(PdfName.BC);
721:                else
722:                    getMK().put(PdfName.BC, getMKColor(color));
723:            }
724:
725:            public void setMKBackgroundColor(Color color) {
726:                if (color == null)
727:                    getMK().remove(PdfName.BG);
728:                else
729:                    getMK().put(PdfName.BG, getMKColor(color));
730:            }
731:
732:            public void setMKNormalCaption(String caption) {
733:                getMK().put(PdfName.CA,
734:                        new PdfString(caption, PdfObject.TEXT_UNICODE));
735:            }
736:
737:            public void setMKRolloverCaption(String caption) {
738:                getMK().put(PdfName.RC,
739:                        new PdfString(caption, PdfObject.TEXT_UNICODE));
740:            }
741:
742:            public void setMKAlternateCaption(String caption) {
743:                getMK().put(PdfName.AC,
744:                        new PdfString(caption, PdfObject.TEXT_UNICODE));
745:            }
746:
747:            public void setMKNormalIcon(PdfTemplate template) {
748:                getMK().put(PdfName.I, template.getIndirectReference());
749:            }
750:
751:            public void setMKRolloverIcon(PdfTemplate template) {
752:                getMK().put(PdfName.RI, template.getIndirectReference());
753:            }
754:
755:            public void setMKAlternateIcon(PdfTemplate template) {
756:                getMK().put(PdfName.IX, template.getIndirectReference());
757:            }
758:
759:            public void setMKIconFit(PdfName scale, PdfName scalingType,
760:                    float leftoverLeft, float leftoverBottom,
761:                    boolean fitInBounds) {
762:                PdfDictionary dic = new PdfDictionary();
763:                if (!scale.equals(PdfName.A))
764:                    dic.put(PdfName.SW, scale);
765:                if (!scalingType.equals(PdfName.P))
766:                    dic.put(PdfName.S, scalingType);
767:                if (leftoverLeft != 0.5f || leftoverBottom != 0.5f) {
768:                    PdfArray array = new PdfArray(new PdfNumber(leftoverLeft));
769:                    array.add(new PdfNumber(leftoverBottom));
770:                    dic.put(PdfName.A, array);
771:                }
772:                if (fitInBounds)
773:                    dic.put(PdfName.FB, PdfBoolean.PDFTRUE);
774:                getMK().put(PdfName.IF, dic);
775:            }
776:
777:            public void setMKTextPosition(int tp) {
778:                getMK().put(PdfName.TP, new PdfNumber(tp));
779:            }
780:
781:            /**
782:             * Sets the layer this annotation belongs to.
783:             * @param layer the layer this annotation belongs to
784:             */
785:            public void setLayer(PdfOCG layer) {
786:                put(PdfName.OC, layer.getRef());
787:            }
788:
789:            /**
790:             * Sets the name of the annotation.
791:             * With this name the annotation can be identified among
792:             * all the annotations on a page (it has to be unique).
793:             */
794:            public void setName(String name) {
795:                put(PdfName.NM, new PdfString(name));
796:            }
797:
798:            /**
799:             * This class processes links from imported pages so that they may be active. The following example code reads a group
800:             * of files and places them all on the output PDF, four pages in a single page, keeping the links active.
801:             * <pre>
802:             * String[] files = new String[] {&quot;input1.pdf&quot;, &quot;input2.pdf&quot;};
803:             * String outputFile = &quot;output.pdf&quot;;
804:             * int firstPage=1;
805:             * Document document = new Document();
806:             * PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(outputFile));
807:             * document.setPageSize(PageSize.A4);
808:             * float W = PageSize.A4.getWidth() / 2;
809:             * float H = PageSize.A4.getHeight() / 2;
810:             * document.open();
811:             * PdfContentByte cb = writer.getDirectContent();
812:             * for (int i = 0; i &lt; files.length; i++) {
813:             *    PdfReader currentReader = new PdfReader(files[i]);
814:             *    currentReader.consolidateNamedDestinations();
815:             *    for (int page = 1; page &lt;= currentReader.getNumberOfPages(); page++) {
816:             *        PdfImportedPage importedPage = writer.getImportedPage(currentReader, page);
817:             *        float a = 0.5f;
818:             *        float e = (page % 2 == 0) ? W : 0;
819:             *        float f = (page % 4 == 1 || page % 4 == 2) ? H : 0;
820:             *        ArrayList links = currentReader.getLinks(page);
821:             *        cb.addTemplate(importedPage, a, 0, 0, a, e, f);
822:             *        for (int j = 0; j &lt; links.size(); j++) {
823:             *            PdfAnnotation.PdfImportedLink link = (PdfAnnotation.PdfImportedLink)links.get(j);
824:             *            if (link.isInternal()) {
825:             *                int dPage = link.getDestinationPage();
826:             *                int newDestPage = (dPage-1)/4 + firstPage;
827:             *                float ee = (dPage % 2 == 0) ? W : 0;
828:             *                float ff = (dPage % 4 == 1 || dPage % 4 == 2) ? H : 0;
829:             *                link.setDestinationPage(newDestPage);
830:             *                link.transformDestination(a, 0, 0, a, ee, ff);
831:             *            }
832:             *            link.transformRect(a, 0, 0, a, e, f);
833:             *            writer.addAnnotation(link.createAnnotation(writer));
834:             *        }
835:             *        if (page % 4 == 0)
836:             *        document.newPage();
837:             *    }
838:             *    if (i &lt; files.length - 1)
839:             *    document.newPage();
840:             *    firstPage += (currentReader.getNumberOfPages()+3)/4;
841:             * }
842:             * document.close();
843:             * </pre>
844:             */
845:            public static class PdfImportedLink {
846:                float llx, lly, urx, ury;
847:                HashMap parameters = new HashMap();
848:                PdfArray destination = null;
849:                int newPage = 0;
850:
851:                PdfImportedLink(PdfDictionary annotation) {
852:                    parameters.putAll(annotation.hashMap);
853:                    try {
854:                        destination = (PdfArray) parameters
855:                                .remove(PdfName.DEST);
856:                    } catch (ClassCastException ex) {
857:                        throw new IllegalArgumentException(
858:                                "You have to consolidate the named destinations of your reader.");
859:                    }
860:                    if (destination != null) {
861:                        destination = new PdfArray(destination);
862:                    }
863:                    PdfArray rc = (PdfArray) parameters.remove(PdfName.RECT);
864:                    llx = rc.getAsNumber(0).floatValue();
865:                    lly = rc.getAsNumber(1).floatValue();
866:                    urx = rc.getAsNumber(2).floatValue();
867:                    ury = rc.getAsNumber(3).floatValue();
868:                }
869:
870:                public boolean isInternal() {
871:                    return destination != null;
872:                }
873:
874:                public int getDestinationPage() {
875:                    if (!isInternal())
876:                        return 0;
877:
878:                    // here destination is something like
879:                    // [132 0 R, /XYZ, 29.3898, 731.864502, null]
880:                    PdfIndirectReference ref = destination
881:                            .getAsIndirectObject(0);
882:
883:                    PRIndirectReference pr = (PRIndirectReference) ref;
884:                    PdfReader r = pr.getReader();
885:                    for (int i = 1; i <= r.getNumberOfPages(); i++) {
886:                        PRIndirectReference pp = r.getPageOrigRef(i);
887:                        if (pp.getGeneration() == pr.getGeneration()
888:                                && pp.getNumber() == pr.getNumber())
889:                            return i;
890:                    }
891:                    throw new IllegalArgumentException("Page not found.");
892:                }
893:
894:                public void setDestinationPage(int newPage) {
895:                    if (!isInternal())
896:                        throw new IllegalArgumentException(
897:                                "Cannot change destination of external link");
898:                    this .newPage = newPage;
899:                }
900:
901:                public void transformDestination(float a, float b, float c,
902:                        float d, float e, float f) {
903:                    if (!isInternal())
904:                        throw new IllegalArgumentException(
905:                                "Cannot change destination of external link");
906:                    if (destination.getAsName(1).equals(PdfName.XYZ)) {
907:                        float x = destination.getAsNumber(2).floatValue();
908:                        float y = destination.getAsNumber(3).floatValue();
909:                        float xx = x * a + y * c + e;
910:                        float yy = x * b + y * d + f;
911:                        destination.getArrayList().set(2, new PdfNumber(xx));
912:                        destination.getArrayList().set(3, new PdfNumber(yy));
913:                    }
914:                }
915:
916:                public void transformRect(float a, float b, float c, float d,
917:                        float e, float f) {
918:                    float x = llx * a + lly * c + e;
919:                    float y = llx * b + lly * d + f;
920:                    llx = x;
921:                    lly = y;
922:                    x = urx * a + ury * c + e;
923:                    y = urx * b + ury * d + f;
924:                    urx = x;
925:                    ury = y;
926:                }
927:
928:                public PdfAnnotation createAnnotation(PdfWriter writer) {
929:                    PdfAnnotation annotation = new PdfAnnotation(writer,
930:                            new Rectangle(llx, lly, urx, ury));
931:                    if (newPage != 0) {
932:                        PdfIndirectReference ref = writer
933:                                .getPageReference(newPage);
934:                        destination.getArrayList().set(0, ref);
935:                    }
936:                    if (destination != null)
937:                        annotation.put(PdfName.DEST, destination);
938:                    annotation.hashMap.putAll(parameters);
939:                    return annotation;
940:                }
941:            }
942:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.