~ubuntu-branches/ubuntu/raring/libitext-java/raring-proposed

« back to all changes in this revision

Viewing changes to com/lowagie/text/pdf/PdfAnnotation.java

  • Committer: Bazaar Package Importer
  • Author(s): Adriaan Peeters
  • Date: 2008-11-23 12:26:51 UTC
  • mfrom: (5.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20081123122651-ab7juwjz41q1123k
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * $Id: PdfAnnotation.java,v 1.67 2006/09/10 15:35:56 psoares33 Exp $
3
 
 * $Name:  $
4
 
 *
5
 
 * Copyright 1999, 2000, 2001, 2002 Bruno Lowagie
6
 
 *
7
 
 * The contents of this file are subject to the Mozilla Public License Version 1.1
8
 
 * (the "License"); you may not use this file except in compliance with the License.
9
 
 * You may obtain a copy of the License at http://www.mozilla.org/MPL/
10
 
 *
11
 
 * Software distributed under the License is distributed on an "AS IS" basis,
12
 
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13
 
 * for the specific language governing rights and limitations under the License.
14
 
 *
15
 
 * The Original Code is 'iText, a free JAVA-PDF library'.
16
 
 *
17
 
 * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
18
 
 * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
19
 
 * All Rights Reserved.
20
 
 * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
21
 
 * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
22
 
 *
23
 
 * Contributor(s): all the names of the contributors are added in the source code
24
 
 * where applicable.
25
 
 *
26
 
 * Alternatively, the contents of this file may be used under the terms of the
27
 
 * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
28
 
 * provisions of LGPL are applicable instead of those above.  If you wish to
29
 
 * allow use of your version of this file only under the terms of the LGPL
30
 
 * License and not to allow others to use your version of this file under
31
 
 * the MPL, indicate your decision by deleting the provisions above and
32
 
 * replace them with the notice and other provisions required by the LGPL.
33
 
 * If you do not delete the provisions above, a recipient may use your version
34
 
 * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
35
 
 *
36
 
 * This library is free software; you can redistribute it and/or modify it
37
 
 * under the terms of the MPL as stated above or under the terms of the GNU
38
 
 * Library General Public License as published by the Free Software Foundation;
39
 
 * either version 2 of the License, or any later version.
40
 
 *
41
 
 * This library is distributed in the hope that it will be useful, but WITHOUT
42
 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
43
 
 * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
44
 
 * details.
45
 
 *
46
 
 * If you didn't download this code from the following link, you should check if
47
 
 * you aren't using an obsolete version:
48
 
 * http://www.lowagie.com/iText/
49
 
 */
50
 
 
51
 
package com.lowagie.text.pdf;
52
 
 
53
 
import com.lowagie.text.Rectangle;
54
 
import java.util.HashMap;
55
 
import java.awt.Color;
56
 
import java.io.*;
57
 
/**
58
 
 * A <CODE>PdfAnnotation</CODE> is a note that is associated with a page.
59
 
 *
60
 
 * @see         PdfDictionary
61
 
 */
62
 
 
63
 
public class PdfAnnotation extends PdfDictionary {
64
 
    /** highlight attributename */
65
 
    public static final PdfName HIGHLIGHT_NONE = PdfName.N;
66
 
    /** highlight attributename */
67
 
    public static final PdfName HIGHLIGHT_INVERT = PdfName.I;
68
 
    /** highlight attributename */
69
 
    public static final PdfName HIGHLIGHT_OUTLINE = PdfName.O;
70
 
    /** highlight attributename */
71
 
    public static final PdfName HIGHLIGHT_PUSH = PdfName.P;
72
 
    /** highlight attributename */
73
 
    public static final PdfName HIGHLIGHT_TOGGLE = PdfName.T;
74
 
    /** flagvalue */
75
 
    public static final int FLAGS_INVISIBLE = 1;
76
 
    /** flagvalue */
77
 
    public static final int FLAGS_HIDDEN = 2;
78
 
    /** flagvalue */
79
 
    public static final int FLAGS_PRINT = 4;
80
 
    /** flagvalue */
81
 
    public static final int FLAGS_NOZOOM = 8;
82
 
    /** flagvalue */
83
 
    public static final int FLAGS_NOROTATE = 16;
84
 
    /** flagvalue */
85
 
    public static final int FLAGS_NOVIEW = 32;
86
 
    /** flagvalue */
87
 
    public static final int FLAGS_READONLY = 64;
88
 
    /** flagvalue */
89
 
    public static final int FLAGS_LOCKED = 128;
90
 
    /** flagvalue */
91
 
    public static final int FLAGS_TOGGLENOVIEW = 256;
92
 
    /** appearance attributename */
93
 
    public static final PdfName APPEARANCE_NORMAL = PdfName.N;
94
 
    /** appearance attributename */
95
 
    public static final PdfName APPEARANCE_ROLLOVER = PdfName.R;
96
 
    /** appearance attributename */
97
 
    public static final PdfName APPEARANCE_DOWN = PdfName.D;
98
 
    /** attributevalue */
99
 
    public static final PdfName AA_ENTER = PdfName.E;
100
 
    /** attributevalue */
101
 
    public static final PdfName AA_EXIT = PdfName.X;
102
 
    /** attributevalue */
103
 
    public static final PdfName AA_DOWN = PdfName.D;
104
 
    /** attributevalue */
105
 
    public static final PdfName AA_UP = PdfName.U;
106
 
    /** attributevalue */
107
 
    public static final PdfName AA_FOCUS = PdfName.FO;
108
 
    /** attributevalue */
109
 
    public static final PdfName AA_BLUR = PdfName.BL;
110
 
    /** attributevalue */
111
 
    public static final PdfName AA_JS_KEY = PdfName.K;
112
 
    /** attributevalue */
113
 
    public static final PdfName AA_JS_FORMAT = PdfName.F;
114
 
    /** attributevalue */
115
 
    public static final PdfName AA_JS_CHANGE = PdfName.V;
116
 
    /** attributevalue */
117
 
    public static final PdfName AA_JS_OTHER_CHANGE = PdfName.C;
118
 
    /** attributevalue */
119
 
    public static final int MARKUP_HIGHLIGHT = 0;
120
 
    /** attributevalue */
121
 
    public static final int MARKUP_UNDERLINE = 1;
122
 
    /** attributevalue */
123
 
    public static final int MARKUP_STRIKEOUT = 2;
124
 
    
125
 
    protected PdfWriter writer;
126
 
    protected PdfIndirectReference reference;
127
 
    protected HashMap templates;
128
 
    protected boolean form = false;
129
 
    protected boolean annotation = true;
130
 
    
131
 
    /** Holds value of property used. */
132
 
    protected boolean used = false;
133
 
    
134
 
    /** Holds value of property placeInPage. */
135
 
    private int placeInPage = -1;
136
 
    
137
 
    // constructors
138
 
    public PdfAnnotation(PdfWriter writer, Rectangle rect) {
139
 
        this.writer = writer;
140
 
        if (rect != null)
141
 
            put(PdfName.RECT, new PdfRectangle(rect));
142
 
    }
143
 
    
144
 
/**
145
 
 * Constructs a new <CODE>PdfAnnotation</CODE> of subtype text.
146
 
 * @param writer
147
 
 * @param llx
148
 
 * @param lly
149
 
 * @param urx
150
 
 * @param ury
151
 
 * @param title
152
 
 * @param content
153
 
 */
154
 
    
155
 
    PdfAnnotation(PdfWriter writer, float llx, float lly, float urx, float ury, PdfString title, PdfString content) {
156
 
        this.writer = writer;
157
 
        put(PdfName.SUBTYPE, PdfName.TEXT);
158
 
        put(PdfName.T, title);
159
 
        put(PdfName.RECT, new PdfRectangle(llx, lly, urx, ury));
160
 
        put(PdfName.CONTENTS, content);
161
 
    }
162
 
    
163
 
/**
164
 
 * Constructs a new <CODE>PdfAnnotation</CODE> of subtype link (Action).
165
 
 * @param writer
166
 
 * @param llx
167
 
 * @param lly
168
 
 * @param urx
169
 
 * @param ury
170
 
 * @param action
171
 
 */
172
 
    
173
 
    public PdfAnnotation(PdfWriter writer, float llx, float lly, float urx, float ury, PdfAction action) {
174
 
        this.writer = writer;
175
 
        put(PdfName.SUBTYPE, PdfName.LINK);
176
 
        put(PdfName.RECT, new PdfRectangle(llx, lly, urx, ury));
177
 
        put(PdfName.A, action);
178
 
        put(PdfName.BORDER, new PdfBorderArray(0, 0, 0));
179
 
        put(PdfName.C, new PdfColor(0x00, 0x00, 0xFF));
180
 
    }
181
 
 
182
 
    /**
183
 
     * Creates a screen PdfAnnotation
184
 
     * @param writer
185
 
     * @param rect
186
 
     * @param clipTitle
187
 
     * @param fs
188
 
     * @param mimeType
189
 
     * @param playOnDisplay
190
 
     * @return a screen PdfAnnotation
191
 
     * @throws IOException
192
 
     */
193
 
    public static PdfAnnotation createScreen(PdfWriter writer, Rectangle rect, String clipTitle, PdfFileSpecification fs,
194
 
                                             String mimeType, boolean playOnDisplay) throws IOException {
195
 
        PdfAnnotation ann = new PdfAnnotation(writer, rect);
196
 
        ann.put(PdfName.SUBTYPE, PdfName.SCREEN);
197
 
        ann.put (PdfName.F, new PdfNumber(FLAGS_PRINT));
198
 
        ann.put(PdfName.TYPE, PdfName.ANNOT);
199
 
        ann.setPage();
200
 
        PdfIndirectReference ref = ann.getIndirectReference();
201
 
        PdfAction action = PdfAction.rendition(clipTitle,fs,mimeType, ref);
202
 
        PdfIndirectReference actionRef = writer.addToBody(action).getIndirectReference();
203
 
        // for play on display add trigger event
204
 
        if (playOnDisplay)
205
 
        {
206
 
            PdfDictionary aa = new PdfDictionary();
207
 
            aa.put(new PdfName("PV"), actionRef);
208
 
            ann.put(PdfName.AA, aa);
209
 
        }
210
 
        ann.put(PdfName.A, actionRef);
211
 
        return ann;
212
 
    }
213
 
 
214
 
    public PdfIndirectReference getIndirectReference() {
215
 
        if (reference == null) {
216
 
            reference = writer.getPdfIndirectReference();
217
 
        }
218
 
        return reference;
219
 
    }
220
 
    
221
 
    /**
222
 
     * @param writer
223
 
     * @param rect
224
 
     * @param title
225
 
     * @param contents
226
 
     * @param open
227
 
     * @param icon
228
 
     * @return a PdfAnnotation
229
 
     */
230
 
    public static PdfAnnotation createText(PdfWriter writer, Rectangle rect, String title, String contents, boolean open, String icon) {
231
 
        PdfAnnotation annot = new PdfAnnotation(writer, rect);
232
 
        annot.put(PdfName.SUBTYPE, PdfName.TEXT);
233
 
        if (title != null)
234
 
            annot.put(PdfName.T, new PdfString(title, PdfObject.TEXT_UNICODE));
235
 
        if (contents != null)
236
 
            annot.put(PdfName.CONTENTS, new PdfString(contents, PdfObject.TEXT_UNICODE));
237
 
        if (open)
238
 
            annot.put(PdfName.OPEN, PdfBoolean.PDFTRUE);
239
 
        if (icon != null) {
240
 
            annot.put(PdfName.NAME, new PdfName(icon));
241
 
        }
242
 
        return annot;
243
 
    }
244
 
    
245
 
    /**
246
 
     * Creates a link.
247
 
     * @param writer
248
 
     * @param rect
249
 
     * @param highlight
250
 
     * @return A PdfAnnotation
251
 
     */
252
 
    protected static PdfAnnotation createLink(PdfWriter writer, Rectangle rect, PdfName highlight) {
253
 
        PdfAnnotation annot = new PdfAnnotation(writer, rect);
254
 
        annot.put(PdfName.SUBTYPE, PdfName.LINK);
255
 
        if (!highlight.equals(HIGHLIGHT_INVERT))
256
 
            annot.put(PdfName.H, highlight);
257
 
        return annot;
258
 
    }
259
 
    
260
 
    /**
261
 
     * Creates an Annotation with an Action.
262
 
     * @param writer
263
 
     * @param rect
264
 
     * @param highlight
265
 
     * @param action
266
 
     * @return A PdfAnnotation
267
 
     */
268
 
    public static PdfAnnotation createLink(PdfWriter writer, Rectangle rect, PdfName highlight, PdfAction action) {
269
 
        PdfAnnotation annot = createLink(writer, rect, highlight);
270
 
        annot.putEx(PdfName.A, action);
271
 
        return annot;
272
 
    }
273
 
 
274
 
    /**
275
 
     * Creates an Annotation with an local destination.
276
 
     * @param writer
277
 
     * @param rect
278
 
     * @param highlight
279
 
     * @param namedDestination
280
 
     * @return A PdfAnnotation
281
 
     */
282
 
    public static PdfAnnotation createLink(PdfWriter writer, Rectangle rect, PdfName highlight, String namedDestination) {
283
 
        PdfAnnotation annot = createLink(writer, rect, highlight);
284
 
        annot.put(PdfName.DEST, new PdfString(namedDestination));
285
 
        return annot;
286
 
    }
287
 
 
288
 
    /**
289
 
     * Creates an Annotation with a PdfDestination.
290
 
     * @param writer
291
 
     * @param rect
292
 
     * @param highlight
293
 
     * @param page
294
 
     * @param dest
295
 
     * @return A PdfAnnotation
296
 
     */
297
 
    public static PdfAnnotation createLink(PdfWriter writer, Rectangle rect, PdfName highlight, int page, PdfDestination dest) {
298
 
        PdfAnnotation annot = createLink(writer, rect, highlight);
299
 
        PdfIndirectReference ref = writer.getPageReference(page);
300
 
        dest.addPage(ref);
301
 
        annot.put(PdfName.DEST, dest);
302
 
        return annot;
303
 
    }
304
 
    
305
 
    /**
306
 
     * Add some free text to the document.
307
 
     * @param writer
308
 
     * @param rect
309
 
     * @param contents
310
 
     * @param defaultAppearance
311
 
     * @return A PdfAnnotation
312
 
     */
313
 
    public static PdfAnnotation createFreeText(PdfWriter writer, Rectangle rect, String contents, PdfContentByte defaultAppearance) {
314
 
        PdfAnnotation annot = new PdfAnnotation(writer, rect);
315
 
        annot.put(PdfName.SUBTYPE, PdfName.FREETEXT);
316
 
        annot.put(PdfName.CONTENTS, new PdfString(contents, PdfObject.TEXT_UNICODE));
317
 
        annot.setDefaultAppearanceString(defaultAppearance);
318
 
        return annot;
319
 
    }
320
 
 
321
 
    /**
322
 
     * Adds a line to the document. Move over the line and a tooltip is shown.
323
 
     * @param writer
324
 
     * @param rect
325
 
     * @param contents
326
 
     * @param x1
327
 
     * @param y1
328
 
     * @param x2
329
 
     * @param y2
330
 
     * @return A PdfAnnotation
331
 
     */
332
 
    public static PdfAnnotation createLine(PdfWriter writer, Rectangle rect, String contents, float x1, float y1, float x2, float y2) {
333
 
        PdfAnnotation annot = new PdfAnnotation(writer, rect);
334
 
        annot.put(PdfName.SUBTYPE, PdfName.LINE);
335
 
        annot.put(PdfName.CONTENTS, new PdfString(contents, PdfObject.TEXT_UNICODE));
336
 
        PdfArray array = new PdfArray(new PdfNumber(x1));
337
 
        array.add(new PdfNumber(y1));
338
 
        array.add(new PdfNumber(x2));
339
 
        array.add(new PdfNumber(y2));
340
 
        annot.put(PdfName.L, array);
341
 
        return annot;
342
 
    }
343
 
 
344
 
    /**
345
 
     * Adds a circle or a square that shows a tooltip when you pass over it.
346
 
     * @param writer
347
 
     * @param rect
348
 
     * @param contents The tooltip
349
 
     * @param square true if you want a square, false if you want a circle
350
 
     * @return A PdfAnnotation
351
 
     */
352
 
    public static PdfAnnotation createSquareCircle(PdfWriter writer, Rectangle rect, String contents, boolean square) {
353
 
        PdfAnnotation annot = new PdfAnnotation(writer, rect);
354
 
        if (square)
355
 
            annot.put(PdfName.SUBTYPE, PdfName.SQUARE);
356
 
        else
357
 
            annot.put(PdfName.SUBTYPE, PdfName.CIRCLE);
358
 
        annot.put(PdfName.CONTENTS, new PdfString(contents, PdfObject.TEXT_UNICODE));
359
 
        return annot;
360
 
    }
361
 
 
362
 
    public static PdfAnnotation createMarkup(PdfWriter writer, Rectangle rect, String contents, int type, float quadPoints[]) {
363
 
        PdfAnnotation annot = new PdfAnnotation(writer, rect);
364
 
        PdfName name = PdfName.HIGHLIGHT;
365
 
        switch (type) {
366
 
            case MARKUP_UNDERLINE:
367
 
                name = PdfName.UNDERLINE;
368
 
                break;
369
 
            case MARKUP_STRIKEOUT:
370
 
                name = PdfName.STRIKEOUT;
371
 
                break;
372
 
        }
373
 
        annot.put(PdfName.SUBTYPE, name);
374
 
        annot.put(PdfName.CONTENTS, new PdfString(contents, PdfObject.TEXT_UNICODE));
375
 
        PdfArray array = new PdfArray();
376
 
        for (int k = 0; k < quadPoints.length; ++k)
377
 
            array.add(new PdfNumber(quadPoints[k]));
378
 
        annot.put(PdfName.QUADPOINTS, array);
379
 
        return annot;
380
 
    }
381
 
 
382
 
    /**
383
 
     * Adds a Stamp to your document. Move over the stamp and a tooltip is shown
384
 
     * @param writer
385
 
     * @param rect
386
 
     * @param contents
387
 
     * @param name
388
 
     * @return A PdfAnnotation
389
 
     */
390
 
    public static PdfAnnotation createStamp(PdfWriter writer, Rectangle rect, String contents, String name) {
391
 
        PdfAnnotation annot = new PdfAnnotation(writer, rect);
392
 
        annot.put(PdfName.SUBTYPE, PdfName.STAMP);
393
 
        annot.put(PdfName.CONTENTS, new PdfString(contents, PdfObject.TEXT_UNICODE));
394
 
        annot.put(PdfName.NAME, new PdfName(name));
395
 
        return annot;
396
 
    }
397
 
 
398
 
    public static PdfAnnotation createInk(PdfWriter writer, Rectangle rect, String contents, float inkList[][]) {
399
 
        PdfAnnotation annot = new PdfAnnotation(writer, rect);
400
 
        annot.put(PdfName.SUBTYPE, PdfName.INK);
401
 
        annot.put(PdfName.CONTENTS, new PdfString(contents, PdfObject.TEXT_UNICODE));
402
 
        PdfArray outer = new PdfArray();
403
 
        for (int k = 0; k < inkList.length; ++k) {
404
 
            PdfArray inner = new PdfArray();
405
 
            float deep[] = inkList[k];
406
 
            for (int j = 0; j < deep.length; ++j)
407
 
                inner.add(new PdfNumber(deep[j]));
408
 
            outer.add(inner);
409
 
        }
410
 
        annot.put(PdfName.INKLIST, outer);
411
 
        return annot;
412
 
    }
413
 
 
414
 
    /** Creates a file attachment annotation.
415
 
     * @param writer the <CODE>PdfWriter</CODE>
416
 
     * @param rect the dimensions in the page of the annotation
417
 
     * @param contents the file description
418
 
     * @param fileStore an array with the file. If it's <CODE>null</CODE>
419
 
     * the file will be read from the disk
420
 
     * @param file the path to the file. It will only be used if
421
 
     * <CODE>fileStore</CODE> is not <CODE>null</CODE>
422
 
     * @param fileDisplay the actual file name stored in the pdf
423
 
     * @throws IOException on error
424
 
     * @return the annotation
425
 
     */    
426
 
    public static PdfAnnotation createFileAttachment(PdfWriter writer, Rectangle rect, String contents, byte fileStore[], String file, String fileDisplay) throws IOException {
427
 
        return createFileAttachment(writer, rect, contents, PdfFileSpecification.fileEmbedded(writer, file, fileDisplay, fileStore));
428
 
    }
429
 
 
430
 
    /** Creates a file attachment annotation
431
 
     * @param writer
432
 
     * @param rect
433
 
     * @param contents
434
 
     * @param fs
435
 
     * @return the annotation
436
 
     * @throws IOException
437
 
     */
438
 
    public static PdfAnnotation createFileAttachment(PdfWriter writer, Rectangle rect, String contents, PdfFileSpecification fs) throws IOException {
439
 
        PdfAnnotation annot = new PdfAnnotation(writer, rect);
440
 
        annot.put(PdfName.SUBTYPE, PdfName.FILEATTACHMENT);
441
 
        if (contents != null)
442
 
            annot.put(PdfName.CONTENTS, new PdfString(contents, PdfObject.TEXT_UNICODE));
443
 
        annot.put(PdfName.FS, fs.getReference());
444
 
        return annot;
445
 
    }
446
 
    
447
 
    /**
448
 
     * Adds a popup to your document.
449
 
     * @param writer
450
 
     * @param rect
451
 
     * @param contents
452
 
     * @param open
453
 
     * @return A PdfAnnotation
454
 
     */
455
 
    public static PdfAnnotation createPopup(PdfWriter writer, Rectangle rect, String contents, boolean open) {
456
 
        PdfAnnotation annot = new PdfAnnotation(writer, rect);
457
 
        annot.put(PdfName.SUBTYPE, PdfName.POPUP);
458
 
        if (contents != null)
459
 
            annot.put(PdfName.CONTENTS, new PdfString(contents, PdfObject.TEXT_UNICODE));
460
 
        if (open)
461
 
            annot.put(PdfName.OPEN, PdfBoolean.PDFTRUE);
462
 
        return annot;
463
 
    }
464
 
 
465
 
    public void setDefaultAppearanceString(PdfContentByte cb) {
466
 
        byte b[] = cb.getInternalBuffer().toByteArray();
467
 
        int len = b.length;
468
 
        for (int k = 0; k < len; ++k) {
469
 
            if (b[k] == '\n')
470
 
                b[k] = 32;
471
 
        }
472
 
        put(PdfName.DA, new PdfString(b));
473
 
    }
474
 
    
475
 
    public void setFlags(int flags) {
476
 
        if (flags == 0)
477
 
            remove(PdfName.F);
478
 
        else
479
 
            put(PdfName.F, new PdfNumber(flags));
480
 
    }
481
 
    
482
 
    public void setBorder(PdfBorderArray border) {
483
 
        put(PdfName.BORDER, border);
484
 
    }
485
 
 
486
 
    public void setBorderStyle(PdfBorderDictionary border) {
487
 
        put(PdfName.BS, border);
488
 
    }
489
 
    
490
 
    /**
491
 
     * Sets the annotation's highlighting mode. The values can be
492
 
     * <CODE>HIGHLIGHT_NONE</CODE>, <CODE>HIGHLIGHT_INVERT</CODE>,
493
 
     * <CODE>HIGHLIGHT_OUTLINE</CODE> and <CODE>HIGHLIGHT_PUSH</CODE>;
494
 
     * @param highlight the annotation's highlighting mode
495
 
     */    
496
 
    public void setHighlighting(PdfName highlight) {
497
 
        if (highlight.equals(HIGHLIGHT_INVERT))
498
 
            remove(PdfName.H);
499
 
        else
500
 
            put(PdfName.H, highlight);
501
 
    }
502
 
    
503
 
    public void setAppearance(PdfName ap, PdfTemplate template) {
504
 
        PdfDictionary dic = (PdfDictionary)get(PdfName.AP);
505
 
        if (dic == null)
506
 
            dic = new PdfDictionary();
507
 
        dic.put(ap, template.getIndirectReference());
508
 
        put(PdfName.AP, dic);
509
 
        if (!form)
510
 
            return;
511
 
        if (templates == null)
512
 
            templates = new HashMap();
513
 
        templates.put(template, null);
514
 
    }
515
 
 
516
 
    public void setAppearance(PdfName ap, String state, PdfTemplate template) {
517
 
        PdfDictionary dicAp = (PdfDictionary)get(PdfName.AP);
518
 
        if (dicAp == null)
519
 
            dicAp = new PdfDictionary();
520
 
 
521
 
        PdfDictionary dic;
522
 
        PdfObject obj = dicAp.get(ap);
523
 
        if (obj != null && obj.isDictionary())
524
 
            dic = (PdfDictionary)obj;
525
 
        else
526
 
            dic = new PdfDictionary();
527
 
        dic.put(new PdfName(state), template.getIndirectReference());
528
 
        dicAp.put(ap, dic);
529
 
        put(PdfName.AP, dicAp);
530
 
        if (!form)
531
 
            return;
532
 
        if (templates == null)
533
 
            templates = new HashMap();
534
 
        templates.put(template, null);
535
 
    }
536
 
    
537
 
    public void setAppearanceState(String state) {
538
 
        if (state == null) {
539
 
            remove(PdfName.AS);
540
 
            return;
541
 
        }
542
 
        put(PdfName.AS, new PdfName(state));
543
 
    }
544
 
    
545
 
    public void setColor(Color color) {
546
 
        put(PdfName.C, new PdfColor(color));
547
 
    }
548
 
    
549
 
    public void setTitle(String title) {
550
 
        if (title == null) {
551
 
            remove(PdfName.T);
552
 
            return;
553
 
        }
554
 
        put(PdfName.T, new PdfString(title, PdfObject.TEXT_UNICODE));
555
 
    }
556
 
    
557
 
    public void setPopup(PdfAnnotation popup) {
558
 
        put(PdfName.POPUP, popup.getIndirectReference());
559
 
        popup.put(PdfName.PARENT, getIndirectReference());
560
 
    }
561
 
    
562
 
    public void setAction(PdfAction action) {
563
 
        put(PdfName.A, action);
564
 
    }
565
 
    
566
 
    public void setAdditionalActions(PdfName key, PdfAction action) {
567
 
        PdfDictionary dic;
568
 
        PdfObject obj = get(PdfName.AA);
569
 
        if (obj != null && obj.isDictionary())
570
 
            dic = (PdfDictionary)obj;
571
 
        else
572
 
            dic = new PdfDictionary();
573
 
        dic.put(key, action);
574
 
        put(PdfName.AA, dic);
575
 
    }
576
 
    
577
 
    /** Getter for property used.
578
 
     * @return Value of property used.
579
 
     */
580
 
    public boolean isUsed() {
581
 
        return used;
582
 
    }
583
 
    
584
 
    /** Setter for property used.
585
 
     */
586
 
    void setUsed() {
587
 
        used = true;
588
 
    }
589
 
    
590
 
    HashMap getTemplates() {
591
 
        return templates;
592
 
    }
593
 
    
594
 
    /** Getter for property form.
595
 
     * @return Value of property form.
596
 
     */
597
 
    public boolean isForm() {
598
 
        return form;
599
 
    }
600
 
    
601
 
    /** Getter for property annotation.
602
 
     * @return Value of property annotation.
603
 
     */
604
 
    public boolean isAnnotation() {
605
 
        return annotation;
606
 
    }
607
 
    
608
 
    public void setPage(int page) {
609
 
        put(PdfName.P, writer.getPageReference(page));
610
 
    }
611
 
    
612
 
    public void setPage() {
613
 
        put(PdfName.P, writer.getCurrentPage());
614
 
    }
615
 
    
616
 
    /** Getter for property placeInPage.
617
 
     * @return Value of property placeInPage.
618
 
     */
619
 
    public int getPlaceInPage() {
620
 
        return placeInPage;
621
 
    }
622
 
    
623
 
    /** Places the annotation in a specified page that must be greater
624
 
     * or equal to the current one. With <code>PdfStamper</code> the page
625
 
     * can be any. The first page is 1.
626
 
     * @param placeInPage New value of property placeInPage.
627
 
     */
628
 
    public void setPlaceInPage(int placeInPage) {
629
 
        this.placeInPage = placeInPage;
630
 
    }
631
 
    
632
 
    public void setRotate(int v) {
633
 
        put(PdfName.ROTATE, new PdfNumber(v));
634
 
    }
635
 
    
636
 
    PdfDictionary getMK() {
637
 
        PdfDictionary mk = (PdfDictionary)get(PdfName.MK);
638
 
        if (mk == null) {
639
 
            mk = new PdfDictionary();
640
 
            put(PdfName.MK, mk);
641
 
        }
642
 
        return mk;
643
 
    }
644
 
    
645
 
    public void setMKRotation(int rotation) {
646
 
        getMK().put(PdfName.R, new PdfNumber(rotation));
647
 
    }
648
 
    
649
 
    public static PdfArray getMKColor(Color color) {
650
 
        PdfArray array = new PdfArray();
651
 
        int type = ExtendedColor.getType(color);
652
 
        switch (type) {
653
 
            case ExtendedColor.TYPE_GRAY: {
654
 
                array.add(new PdfNumber(((GrayColor)color).getGray()));
655
 
                break;
656
 
            }
657
 
            case ExtendedColor.TYPE_CMYK: {
658
 
                CMYKColor cmyk = (CMYKColor)color;
659
 
                array.add(new PdfNumber(cmyk.getCyan()));
660
 
                array.add(new PdfNumber(cmyk.getMagenta()));
661
 
                array.add(new PdfNumber(cmyk.getYellow()));
662
 
                array.add(new PdfNumber(cmyk.getBlack()));
663
 
                break;
664
 
            }
665
 
            case ExtendedColor.TYPE_SEPARATION:
666
 
            case ExtendedColor.TYPE_PATTERN:
667
 
            case ExtendedColor.TYPE_SHADING:
668
 
                throw new RuntimeException("Separations, patterns and shadings are not allowed in MK dictionary.");
669
 
            default:
670
 
                array.add(new PdfNumber(color.getRed() / 255f));
671
 
                array.add(new PdfNumber(color.getGreen() / 255f));
672
 
                array.add(new PdfNumber(color.getBlue() / 255f));
673
 
        }
674
 
        return array;
675
 
    }
676
 
    
677
 
    public void setMKBorderColor(Color color) {
678
 
        if (color == null)
679
 
            getMK().remove(PdfName.BC);
680
 
        else
681
 
            getMK().put(PdfName.BC, getMKColor(color));
682
 
    }
683
 
    
684
 
    public void setMKBackgroundColor(Color color) {
685
 
        if (color == null)
686
 
            getMK().remove(PdfName.BG);
687
 
        else
688
 
            getMK().put(PdfName.BG, getMKColor(color));
689
 
    }
690
 
    
691
 
    public void setMKNormalCaption(String caption) {
692
 
        getMK().put(PdfName.CA, new PdfString(caption, PdfObject.TEXT_UNICODE));
693
 
    }
694
 
    
695
 
    public void setMKRolloverCaption(String caption) {
696
 
        getMK().put(PdfName.RC, new PdfString(caption, PdfObject.TEXT_UNICODE));
697
 
    }
698
 
    
699
 
    public void setMKAlternateCaption(String caption) {
700
 
        getMK().put(PdfName.AC, new PdfString(caption, PdfObject.TEXT_UNICODE));
701
 
    }
702
 
    
703
 
    public void setMKNormalIcon(PdfTemplate template) {
704
 
        getMK().put(PdfName.I, template.getIndirectReference());
705
 
    }
706
 
    
707
 
    public void setMKRolloverIcon(PdfTemplate template) {
708
 
        getMK().put(PdfName.RI, template.getIndirectReference());
709
 
    }
710
 
    
711
 
    public void setMKAlternateIcon(PdfTemplate template) {
712
 
        getMK().put(PdfName.IX, template.getIndirectReference());
713
 
    }
714
 
    
715
 
    public void setMKIconFit(PdfName scale, PdfName scalingType, float leftoverLeft, float leftoverBottom, boolean fitInBounds) {
716
 
        PdfDictionary dic = new PdfDictionary();
717
 
        if (!scale.equals(PdfName.A))
718
 
            dic.put(PdfName.SW, scale);
719
 
        if (!scalingType.equals(PdfName.P))
720
 
            dic.put(PdfName.S, scalingType);
721
 
        if (leftoverLeft != 0.5f || leftoverBottom != 0.5f) {
722
 
            PdfArray array = new PdfArray(new PdfNumber(leftoverLeft));
723
 
            array.add(new PdfNumber(leftoverBottom));
724
 
            dic.put(PdfName.A, array);
725
 
        }
726
 
        if (fitInBounds)
727
 
            dic.put(PdfName.FB, PdfBoolean.PDFTRUE);
728
 
        getMK().put(PdfName.IF, dic);
729
 
    }
730
 
    
731
 
    public void setMKTextPosition(int tp) {
732
 
        getMK().put(PdfName.TP, new PdfNumber(tp));
733
 
    }
734
 
    
735
 
    /**
736
 
     * Sets the layer this annotation belongs to.
737
 
     * @param layer the layer this annotation belongs to
738
 
     */    
739
 
    public void setLayer(PdfOCG layer) {
740
 
        put(PdfName.OC, layer.getRef());
741
 
    }
742
 
}
 
 
b'\\ No newline at end of file'