Skip to content
Navigation Menu
{{ message }}
forked from kovzol/Java-Geometry-Expert
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPDFPage.java
More file actions
648 lines (585 loc) · 20.4 KB
/
Copy pathPDFPage.java
File metadata and controls
648 lines (585 loc) · 20.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
/*
* $Id: PDFPage.java,v 1.5 2007/09/22 12:58:40 gil1 Exp $
*
* $Date: 2007/09/22 12:58:40 $
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package pdf;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.print.PageFormat;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.Vector;
/**
* <p>This class defines a single page within a document. It is linked to a
* single PDFGraphics object</p>
*
* @author Peter T Mount
* @author Eric Z. Beard, ericzbeard@hotmail.com
* @author Gilbert DeLeeuw, gil1@users.sourceforge.net
* @version $Revision: 1.5 $, $Date: 2007/09/22 12:58:40 $
*
*
*/
public class PDFPage extends PDFObject implements Serializable
{
/*
* NOTE: The original class is the work of Peter T. Mount, who released it
* in the uk.org.retep.pdf package. It was modified by Eric Z. Beard as
* follows:
* The package name was changed to gnu.pdf.
* The formatting was changed a little bit.
* It is still licensed under the LGPL.
*/
/**
* Default page format (Letter size with 1 inch margins and
* Portrait orientation)
*/
private static final PageFormat DEF_FORMAT = new PageFormat();
/**
* This is this page format, ie the size of the page, margins, and rotation
*/
protected PageFormat pageFormat;
/**
* This is the pages object id that this page belongs to.
* It is set by the pages object when it is added to it.
*/
protected PDFObject pdfPageList;
/**
* This holds the contents of the page.
*/
protected Vector<PDFObject> contents;
/**
* Object ID that contains a thumbnail sketch of the page.
* -1 indicates no thumbnail.
*/
protected PDFObject thumbnail;
/**
* This holds any Annotations contained within this page.
*/
protected Vector<PDFObject> annotations;
/**
* This holds any resources for this page
*/
protected Vector<String> resources;
// JM
protected Vector<String> imageResources;
/**
* The fonts associated with this page
*/
protected Vector<PDFFont> fonts;
/**
* The xobjects or other images in the pdf
*/
// protected Vector xobjects;
/**
* These handle the procset for this page.
* Refer to page 140 of the PDF Reference manual
* NB: Text is handled when the fonts Vector is null, and a font is created
* refer to getFont() to see where it's defined
*/
protected boolean hasImageB,hasImageC,hasImageI;
protected procset procset;
/**
* This constructs a Page object, which will hold any contents for this
* page.
*
* <p>Once created, it is added to the document via the PDF.add() method.
* (For Advanced use, via the PDFPages.add() method).
*
* <p>This defaults to a4 media.
*/
public PDFPage()
{
super("/Page");
pageFormat = DEF_FORMAT;
contents = new Vector<PDFObject>();
thumbnail = null;
annotations = new Vector<PDFObject>();
resources = new Vector<String>();
// JM
imageResources = new Vector<String>();
fonts = new Vector<PDFFont>();
procset = null;
}
/**
* Constructs a page using A4 media, but using the supplied orientation.
* @param orientation Orientation: 0, 90 or 270
* @see PageFormat#PORTRAIT
* @see PageFormat#LANDSCAPE
* @see PageFormat#REVERSE_LANDSCAPE
*/
public PDFPage(int orientation)
{
this();
setOrientation(orientation);
}
/**
* Constructs a page using the supplied media size and orientation.
* @param pageFormat PageFormat describing the page size
*/
public PDFPage(PageFormat pageFormat)
{
this();
this.pageFormat = pageFormat;
}
/**
* Adds to procset.
* @param proc the String to be added.
*/
public void addToProcset(String proc) {
if (procset == null) {
addProcset();
}
procset.add(proc);
}
/**
* This returns a PDFGraphics object, which can then be used to render
* on to this page. If a previous PDFGraphics object was used, this object
* is appended to the page, and will be drawn over the top of any previous
* objects.
*
* @return a new PDFGraphics object to be used to draw this page.
*/
public PDFGraphics getGraphics() {
try {
PDFGraphics g = new PDFGraphics();
g.init(this);
return g;
} catch(Exception ex) {
ex.printStackTrace();
}
return null;
}
/**
* Returns a PDFFont, creating it if not yet used.
* @param type Font type, usually /Type1
* @param font Font name
* @param style java.awt.Font style, ie Font.NORMAL
* @return a PDFFont object.
*/
public PDFFont getFont(String type,String font,int style) {
// Search the fonts on this page, and return one that matches this
// font.
// This keeps the number of font definitions down to one per font/style
for(PDFFont ft : fonts) {
if(ft.equals(type,font,style))
return ft;
}
// Ok, the font isn't in the page, so create one.
// We need a procset if we are using fonts, so create it (if not
// already created, and add to our resources
if(fonts.size()==0) {
addProcset();
procset.add("/Text");
}
// finally create and return the font
PDFFont f = pdfDocument.getFont(type,font,style);
fonts.addElement(f);
return f;
}
/**
* Returns the page's PageFormat.
* @return PageFormat describing the page size in device units (72dpi)
*/
public PageFormat getPageFormat() {
return pageFormat;
}
/**
* Gets the dimensions of the page.
* @return a Dimension object containing the width and height of the page.
*/
public Dimension getDimension() {
return new Dimension((int) pageFormat.getWidth(), (int) pageFormat
.getHeight());
}
/**
* Gets the imageable area of the page.
* @return a Rectangle containing the bounds of the imageable area.
*/
public Rectangle getImageableArea() {
return new Rectangle((int) pageFormat.getImageableX(), (int) pageFormat
.getImageableY(),
(int) (pageFormat.getImageableX() + pageFormat
.getImageableWidth()), (int) (pageFormat
.getImageableY() + pageFormat.getImageableHeight()));
}
/**
* Sets the page's orientation.
*
* <p>Normally, this should be done when the page is created, to avoid
* problems.
*
* @param orientation a PageFormat orientation constant:
* PageFormat.PORTRAIT, PageFormat.LANDSACPE or PageFromat.REVERSE_LANDSACPE
*/
public void setOrientation(int orientation) {
pageFormat.setOrientation(orientation);
}
/**
* Returns the pages orientation:
* PageFormat.PORTRAIT, PageFormat.LANDSACPE or PageFromat.REVERSE_LANDSACPE
*
* @see java.awt.print.PageFormat
* @return current orientation of the page
*/
public int getOrientation() {
return pageFormat.getOrientation();
}
/**
* This adds an object that describes some content to this page.
*
* <p><b>Note:</b> Objects that describe contents must be added using this
* method _AFTER_ the PDF.add() method has been called.
*
* @param ob PDFObject describing some contents
*/
public void add(PDFObject ob) {
contents.addElement(ob);
}
/**
* This adds an Annotation to the page.
*
* <p>As with other objects, the annotation must be added to the pdf
* document using PDF.add() before adding to the page.
*
* @param ob Annotation to add.
*/
public void addAnnotation(PDFObject ob) {
annotations.addElement(ob);
}
/**
* This method adds a text note to the document.
* @param note Text of the note
* @param x Coordinate of note
* @param y Coordinate of note
* @param w Width of the note
* @param h Height of the note
* @return Returns the annotation, so other settings can be changed.
*/
public PDFAnnot addNote(String note,int x,int y,int w,int h) {
int xy1[] = cxy(x,y+h);
int xy2[] = cxy(x+w,y);
PDFAnnot ob = new PDFAnnot(xy1[0],xy1[1],
xy2[0],xy2[1],
note);
pdfDocument.add(ob);
annotations.addElement(ob);
return ob;
}
/**
* Adds a hyperlink to the document.
* @param x Coordinate of active area
* @param y Coordinate of active area
* @param w Width of the active area
* @param h Height of the active area
* @param dest Page that will be displayed when the link is activated. When
* displayed, the zoom factor will be changed to fit the display.
* @return Returns the annotation, so other settings can be changed.
*/
public PDFAnnot addLink(int x,int y,int w,int h,PDFObject dest) {
int xy1[] = cxy(x,y+h);
int xy2[] = cxy(x+w,y);
PDFAnnot ob = new PDFAnnot(xy1[0],xy1[1],
xy2[0],xy2[1],
dest
);
pdfDocument.add(ob);
annotations.addElement(ob);
return ob;
}
/**
* Adds a hyperlink to the document.
* @param x Coordinate of active area
* @param y Coordinate of active area
* @param w Width of the active area
* @param h Height of the active area
* @param dest Page that will be displayed when the link is activated
* @param vx Coordinate of view area
* @param vy Coordinate of view area
* @param vw Width of the view area
* @param vh Height of the view area
* @return Returns the annotation, so other settings can be changed.
*/
public PDFAnnot addLink(int x,int y,int w,int h,
PDFObject dest,
int vx,int vy,int vw,int vh) {
int xy1[] = cxy(x,y+h);
int xy2[] = cxy(x+w,y);
int xy3[] = cxy(vx,vy+vh);
int xy4[] = cxy(vx+vw,vy);
PDFAnnot ob = new PDFAnnot(xy1[0],xy1[1],
xy2[0],xy2[1],
dest,
xy3[0],xy3[1],
xy4[0],xy4[1]
);
pdfDocument.add(ob);
annotations.addElement(ob);
return ob;
}
/** Contains the text strings for the xobjects. */
private Vector<String> xobjects = new Vector<String>();
/**
* This adds an XObject resource to the page.
* The string should be of the format
* /Name ObjectNumber RevisionNumber R as in /Image1 13 0 R .
* @param inxobject the XObject resource to be added.
*/
public void addXObject(String inxobject){
xobjects.addElement(inxobject);
}
/**
* This adds a resource to the page.
* @param resource String defining the resource
*/
public void addResource(String resource) {
resources.addElement(resource);
}
// JM
/**
* This adds an image resource to the page.
* @param resource the XObject resource to be added.
*/
public void addImageResource(String resource) {
imageResources.addElement(resource);
}
/**
* This adds an object that describes a thumbnail for this page.
* <p><b>Note:</b> The object must already exist in the PDF, as only the
* object ID is stored.
* @param thumbnail PDFObject containing the thumbnail
*/
public void setThumbnail(PDFObject thumbnail)
{
this.thumbnail = thumbnail;
}
/**
* This method attaches an outline to the current page being generated. When
* selected, the outline displays the top of the page.
* @param title Outline title to attach
* @return PDFOutline object created, for addSubOutline if required.
*/
public PDFOutline addOutline(String title) {
PDFOutline outline = new PDFOutline(title,this);
pdfDocument.add(outline);
pdfDocument.getOutline().add(outline);
return outline;
}
/**
* This method attaches an outline to the current page being generated.
* When selected, the outline displays the top of the page.
*
* <p>Note: If the outline is not in the top level (ie below another
* outline) then it must <b>not</b> be passed to this method.
*
* @param title Outline title to attach
* @param x Left coordinate of region
* @param y Bottom coordinate of region
* @param w Width of region
* @param h Height coordinate of region
* @return PDFOutline object created, for addSubOutline if required.
*/
public PDFOutline addOutline(String title,int x,int y,int w,int h) {
int xy1[] = cxy(x,y+h);
int xy2[] = cxy(x+w,y);
PDFOutline outline = new PDFOutline(title,this,
xy1[0],xy1[1],
xy2[0],xy2[1]);
pdfDocument.add(outline);
pdfDocument.getOutline().add(outline);
return outline;
}
/**
* @param os OutputStream to send the object to
* @exception IOException on error
*/
public void write(OutputStream os) throws IOException
{
// Write the object header
writeStart(os);
// now the objects body
// the /Parent pages object
os.write("/Parent ".getBytes());
os.write(pdfPageList.toString().getBytes());
os.write("\n".getBytes());
// the /MediaBox for the page size
os.write("/MediaBox [".getBytes());
os.write(Integer.toString(0).getBytes());
os.write(" ".getBytes());
os.write(Integer.toString(0).getBytes());
os.write(" ".getBytes());
os.write(Integer.toString((int)pageFormat.getWidth()).getBytes());
os.write(" ".getBytes());
os.write(Integer.toString((int)pageFormat.getHeight()).getBytes());
os.write("]\n".getBytes());
// Rotation (if not zero)
// if(rotate!=0) {
// os.write("/Rotate ".getBytes());
// os.write(Integer.toString(rotate).getBytes());
// os.write("\n".getBytes());
// }
// Now the resources
os.write("/Resources << ".getBytes());
// fonts
if(fonts.size()>0) {
//os.write("/Font << ".getBytes());
os.write("\n/Font << ".getBytes());
for(PDFFont font : fonts) {
os.write(font.getName().getBytes());
os.write(" ".getBytes());
os.write(font.toString().getBytes());
os.write(" ".getBytes());
}
os.write(">> ".getBytes());
}
// Now the XObjects
if (xobjects.size() > 0){
os.write("\n/XObject << ".getBytes());
for(String str : xobjects) {
os.write(str.getBytes());
os.write(" ".getBytes());
}
os.write(">> ".getBytes());
}
// Any other resources
for(String str : resources) {
os.write(str.getBytes());
os.write(" ".getBytes());
}
// JM
if(imageResources.size() > 0) {
os.write("/XObject << ".getBytes());
for(String str : imageResources) {
os.write(str.getBytes());
os.write(" ".getBytes());
}
os.write(" >> ".getBytes());
}
os.write(">>\n".getBytes());
// The thumbnail
if(thumbnail!=null) {
os.write("/Thumb ".getBytes());
os.write(thumbnail.toString().getBytes());
os.write("\n".getBytes());
}
// the /Contents pages object
if(contents.size()>0) {
if(contents.size()==1) {
PDFObject ob = (PDFObject)contents.elementAt(0);
os.write("/Contents ".getBytes());
os.write(ob.toString().getBytes());
os.write("\n".getBytes());
} else {
os.write("/Contents [".getBytes());
os.write(PDFObject.toArray(contents).getBytes());
os.write("\n".getBytes());
}
}
// The /Annots object
if(annotations.size()>0) {
os.write("/Annots ".getBytes());
os.write(PDFObject.toArray(annotations).getBytes());
os.write("\n".getBytes());
}
// finish off with its footer
writeEnd(os);
}
/**
* This creates a procset and sets up the page to reference it
*/
private void addProcset() {
if(procset==null) {
pdfDocument.add(procset = new procset());
resources.addElement("/ProcSet "+procset);
}
}
/**
* This defines a procset
*/
public class procset extends PDFObject {
private Vector<String> set;
/**
* Creates a new procset object.
*/
public procset() {
super(null);
set = new Vector<String>();
// Our default procset (use addElement not add, as we dont want a
// leading space)
set.addElement("/PDF");
}
/**
* @param proc Entry to add to the procset
*/
public void add(String proc) {
set.addElement(" "+proc);
}
/**
* @param os OutputStream to send the object to
* @exception IOException on error
*/
public void write(OutputStream os) throws IOException {
// Write the object header
//writeStart(os);
os.write(Integer.toString(objser).getBytes());
os.write(" 0 obj\n".getBytes());
// now the objects body
os.write("[".getBytes());
for(String str : set)
os.write(str.getBytes());
os.write("]\n".getBytes());
// finish off with its footer
//writeEnd(os);
os.write("endobj\n".getBytes());
}
}
/**
* This utility method converts the y coordinate from Java to User space
* within the page.
* @param x Coordinate in Java space
* @param y Coordinate in Java space
* @return y Coordinate in User space
*/
public int cy(int x,int y) {
return cxy(x,y)[1];
}
/**
* This utility method converts the y coordinate from Java to User space
* within the page.
* @param x Coordinate in Java space
* @param y Coordinate in Java space
* @return x Coordinate in User space
*/
public int cx(int x, int y) {
return cxy(x,y)[0];
}
/**
* This utility method converts the Java coordinates to User space
* within the page.
* @param x Coordinate in Java space
* @param y Coordinate in Java space
* @return array containing the x & y Coordinate in User space
*/
public int[] cxy(int x,int y) {
int r[] = new int[2];
r[0] = x;
r[1] = (int) pageFormat.getHeight() - y;
return r;
}
}
You can’t perform that action at this time.
