Source Code Cross Referenced for PDFPage.java in  » PDF » gnujpdf » gnu » jpdf » 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 » gnujpdf » gnu.jpdf 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $Id: PDFPage.java,v 1.3 2002/03/04 20:33:18 ezb Exp $
003:         *
004:         * $Date: 2002/03/04 20:33:18 $
005:         *
006:         * This library is free software; you can redistribute it and/or
007:         * modify it under the terms of the GNU Lesser General Public
008:         * License as published by the Free Software Foundation; either
009:         * version 2.1 of the License, or (at your option) any later version.
010:         *
011:         * This library is distributed in the hope that it will be useful,
012:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
013:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014:         * Lesser General Public License for more details.
015:         *
016:         * You should have received a copy of the GNU Lesser General Public
017:         * License along with this library; if not, write to the Free Software
018:         * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
019:         */
020:        package gnu.jpdf;
021:
022:        import java.awt.*;
023:        import java.io.*;
024:        import java.util.*;
025:
026:        /**
027:         * <p>This class defines a single page within a document.  It is linked to a
028:         * single PDFGraphics object</p>
029:         *
030:         * @author Peter T Mount
031:         * @author Eric Z. Beard, ericzbeard@hotmail.com
032:         * @author $Author: ezb $
033:         * @version $Revision: 1.3 $, $Date: 2002/03/04 20:33:18 $
034:         *
035:         *
036:         */
037:        public class PDFPage extends PDFObject implements  Serializable {
038:            /*
039:             * NOTE: The original class is the work of Peter T. Mount, who released it
040:             * in the uk.org.retep.pdf package.  It was modified by Eric Z. Beard as
041:             * follows:
042:             * The package name was changed to gnu.pdf.
043:             * The formatting was changed a little bit.
044:             * It is still licensed under the LGPL.
045:             */
046:
047:            /**
048:             * Specifies that the page is in PORTRAIT orientation.
049:             */
050:            public static final int PORTRAIT = 0;
051:
052:            /**
053:             * Specifies that the page is in LANDSCAPE orientation.
054:             */
055:            public static final int LANDSCAPE = 90;
056:
057:            /**
058:             * Specifies that the page is in INVERTEDPORTRAIT orientation.
059:             */
060:            public static final int INVERTEDPORTRAIT = 180;
061:
062:            /**
063:             * Specifies that the page is in SEASCAPE orientation.
064:             */
065:            public static final int SEASCAPE = 270;
066:
067:            /**
068:             * Rectangle defining a page in letter format.
069:             */
070:            public static final Rectangle MEDIA_letter = new Rectangle(0, 0,
071:                    612, 792);
072:
073:            /**
074:             * Rectangle defining a page in note format.
075:             */
076:            public static final Rectangle MEDIA_note = new Rectangle(0, 0, 540,
077:                    720);
078:
079:            /**
080:             * Rectangle defining a page in legal format.
081:             */
082:            public static final Rectangle MEDIA_legal = new Rectangle(0, 0,
083:                    612, 1008);
084:
085:            /**
086:             * Rectangle defining a page in a0 format.
087:             */
088:            public static final Rectangle MEDIA_a0 = new Rectangle(0, 0, 2380,
089:                    3368);
090:
091:            /**
092:             * Rectangle defining a page in a1 format.
093:             */
094:            public static final Rectangle MEDIA_a1 = new Rectangle(0, 0, 1684,
095:                    2380);
096:
097:            /**
098:             * Rectangle defining a page in a2 format.
099:             */
100:            public static final Rectangle MEDIA_a2 = new Rectangle(0, 0, 1190,
101:                    1684);
102:
103:            /**
104:             * Rectangle defining a page in a3 format.
105:             */
106:            public static final Rectangle MEDIA_a3 = new Rectangle(0, 0, 842,
107:                    1190);
108:
109:            /**
110:             * Rectangle defining a page in a4 format.
111:             */
112:            public static final Rectangle MEDIA_a4 = new Rectangle(0, 0, 595,
113:                    842);
114:
115:            /**
116:             * Rectangle defining a page in a5 format.
117:             */
118:            public static final Rectangle MEDIA_a5 = new Rectangle(0, 0, 421,
119:                    595);
120:
121:            /**
122:             * Rectangle defining a page in a6 format.
123:             */
124:            public static final Rectangle MEDIA_a6 = new Rectangle(0, 0, 297,
125:                    421);
126:
127:            /**
128:             * Rectangle defining a page in a7 format.
129:             */
130:            public static final Rectangle MEDIA_a7 = new Rectangle(0, 0, 210,
131:                    297);
132:
133:            /**
134:             * Rectangle defining a page in a8 format.
135:             */
136:            public static final Rectangle MEDIA_a8 = new Rectangle(0, 0, 148,
137:                    210);
138:
139:            /**
140:             * Rectangle defining a page in a9 format.
141:             */
142:            public static final Rectangle MEDIA_a9 = new Rectangle(0, 0, 105,
143:                    148);
144:
145:            /**
146:             * Rectangle defining a page in a10 format.
147:             */
148:            public static final Rectangle MEDIA_a10 = new Rectangle(0, 0, 74,
149:                    105);
150:
151:            /**
152:             * Rectangle defining a page in b0 format.
153:             */
154:            public static final Rectangle MEDIA_b0 = new Rectangle(0, 0, 2836,
155:                    4008);
156:
157:            /**
158:             * Rectangle defining a page in b1 format.
159:             */
160:            public static final Rectangle MEDIA_b1 = new Rectangle(0, 0, 2004,
161:                    2836);
162:
163:            /**
164:             * Rectangle defining a page in b2 format.
165:             */
166:            public static final Rectangle MEDIA_b2 = new Rectangle(0, 0, 1418,
167:                    2004);
168:
169:            /**
170:             * Rectangle defining a page in b3 format.
171:             */
172:            public static final Rectangle MEDIA_b3 = new Rectangle(0, 0, 1002,
173:                    1418);
174:
175:            /**
176:             * Rectangle defining a page in b4 format.
177:             */
178:            public static final Rectangle MEDIA_b4 = new Rectangle(0, 0, 709,
179:                    1002);
180:
181:            /**
182:             * Rectangle defining a page in b5 format.
183:             */
184:            public static final Rectangle MEDIA_b5 = new Rectangle(0, 0, 501,
185:                    709);
186:
187:            /**
188:             * Rectangle defining a page in archE format.
189:             */
190:            public static final Rectangle MEDIA_archE = new Rectangle(0, 0,
191:                    2592, 3456);
192:
193:            /**
194:             * Rectangle defining a page in archD format.
195:             */
196:            public static final Rectangle MEDIA_archD = new Rectangle(0, 0,
197:                    1728, 2592);
198:
199:            /**
200:             * Rectangle defining a page in archC format.
201:             */
202:            public static final Rectangle MEDIA_archC = new Rectangle(0, 0,
203:                    1296, 1728);
204:
205:            /**
206:             * Rectangle defining a page in archB format.
207:             */
208:            public static final Rectangle MEDIA_archB = new Rectangle(0, 0,
209:                    864, 1296);
210:
211:            /**
212:             * Rectangle defining a page in archA format.
213:             */
214:            public static final Rectangle MEDIA_archA = new Rectangle(0, 0,
215:                    648, 864);
216:
217:            /**
218:             * Rectangle defining a page in flsa format.
219:             */
220:            public static final Rectangle MEDIA_flsa = new Rectangle(0, 0, 612,
221:                    936);
222:
223:            /**
224:             * Rectangle defining a page in flse format.
225:             */
226:            public static final Rectangle MEDIA_flse = new Rectangle(0, 0, 612,
227:                    936);
228:
229:            /**
230:             * Rectangle defining a page in halfletter format.
231:             */
232:            public static final Rectangle MEDIA_halfletter = new Rectangle(0,
233:                    0, 396, 612);
234:
235:            /**
236:             * Rectangle defining a page in 11x17 format.
237:             */
238:            public static final Rectangle MEDIA_11x17 = new Rectangle(0, 0,
239:                    792, 1224);
240:
241:            /**
242:             * Rectangle defining a page in ledger format.
243:             */
244:            public static final Rectangle MEDIA_ledger = new Rectangle(0, 0,
245:                    1224, 792);
246:
247:            /**
248:             * This is this pages media box, ie the size of the page
249:             */
250:            protected Rectangle mediabox;
251:
252:            /**
253:             * This is the pages object id that this page belongs to.
254:             * It is set by the pages object when it is added to it.
255:             */
256:            protected PDFObject pdfPageList;
257:
258:            /**
259:             * This holds the contents of the page.
260:             */
261:            protected Vector contents;
262:
263:            /**
264:             * Specifies the number of degrees the page should be rotated clockwise
265:             * when it is displayed. This value must be zero (the default), or a
266:             * multiple of 90.
267:             * @see #PORTRAIT
268:             * @see #LANDSCAPE
269:             * @see #INVERTEDPORTRAIT
270:             * @see #SEASCAPE
271:             */
272:            protected int rotate;
273:
274:            /**
275:             * Object ID that contains a thumbnail sketch of the page.
276:             * -1 indicates no thumbnail.
277:             */
278:            protected PDFObject thumbnail;
279:
280:            /**
281:             * This holds any Annotations contained within this page.
282:             */
283:            protected Vector annotations;
284:
285:            /**
286:             * This holds any resources for this page
287:             */
288:            protected Vector resources;
289:
290:            /**
291:             * The fonts associated with this page
292:             */
293:            protected Vector fonts;
294:
295:            /**
296:             * The xobjects or other images in the pdf
297:             */
298:            //    protected Vector xobjects;
299:            /**
300:             * These handle the procset for this page.
301:             * Refer to page 140 of the PDF Reference manual
302:             * NB: Text is handled when the fonts Vector is null, and a font is created
303:             * refer to getFont() to see where it's defined
304:             */
305:            protected boolean hasImageB, hasImageC, hasImageI;
306:            protected procset procset;
307:
308:            /**
309:             * This constructs a Page object, which will hold any contents for this
310:             * page.
311:             *
312:             * <p>Once created, it is added to the document via the PDF.add() method.
313:             * (For Advanced use, via the PDFPages.add() method).
314:             *
315:             * <p>This defaults to a4 media.
316:             */
317:            public PDFPage() {
318:                super ("/Page");
319:                mediabox = MEDIA_letter;
320:                contents = new Vector();
321:                rotate = 0;
322:                thumbnail = null;
323:                annotations = new Vector();
324:                resources = new Vector();
325:                fonts = new Vector();
326:                procset = null;
327:            }
328:
329:            /**
330:             * Constructs a page, using the supplied media size.
331:             *
332:             * @param mediabox Rectangle describing the page size
333:             */
334:            public PDFPage(Rectangle mediabox) {
335:                this ();
336:                setMedia(mediabox);
337:            }
338:
339:            /**
340:             * Constructs a page using A4 media, but using the supplied orientation.
341:             * @param rotate Rotation: 0, 90, 180 or 270
342:             * @see #PORTRAIT
343:             * @see #LANDSCAPE
344:             * @see #INVERTEDPORTRAIT
345:             * @see #SEASCAPE
346:             */
347:            public PDFPage(int rotate) {
348:                this ();
349:                setOrientation(rotate);
350:            }
351:
352:            /**
353:             * Constructs a page using the supplied media size and orientation.
354:             * @param mediabox Rectangle describing the page size
355:             * @param rotate Rotation: 0, 90, 180 or 270
356:             * @see #PORTRAIT
357:             * @see #LANDSCAPE
358:             * @see #INVERTEDPORTRAIT
359:             * @see #SEASCAPE
360:             */
361:            public PDFPage(Rectangle mediabox, int rotate) {
362:                this ();
363:                setMedia(mediabox);
364:                setOrientation(rotate);
365:            }
366:
367:            public void addToProcset(String proc) {
368:                if (procset == null) {
369:                    addProcset();
370:                }
371:                procset.add(proc);
372:            }
373:
374:            /**
375:             * This returns a PDFGraphics object, which can then be used to render
376:             * on to this page. If a previous PDFGraphics object was used, this object
377:             * is appended to the page, and will be drawn over the top of any previous
378:             * objects.
379:             */
380:            public PDFGraphics getGraphics() {
381:                try {
382:                    PDFGraphics g = new PDFGraphics();
383:                    g.init(this );
384:                    return g;
385:                } catch (Exception ex) {
386:                    ex.printStackTrace();
387:                }
388:
389:                return null;
390:            }
391:
392:            /**
393:             * Returns a PDFFont, creating it if not yet used.
394:             * @para type Font type, usually /Type1
395:             * @param font Font name
396:             * @param style java.awt.Font style, ie Font.NORMAL
397:             */
398:            public PDFFont getFont(String type, String font, int style) {
399:                // Search the fonts on this page, and return one that matches this
400:                // font.
401:                // This keeps the number of font definitions down to one per font/style
402:                for (Enumeration en = fonts.elements(); en.hasMoreElements();) {
403:                    PDFFont ft = (PDFFont) en.nextElement();
404:                    if (ft.equals(type, font, style))
405:                        return ft;
406:                }
407:
408:                // Ok, the font isn't in the page, so create one.
409:
410:                // We need a procset if we are using fonts, so create it (if not
411:                // already created, and add to our resources
412:                if (fonts.size() == 0) {
413:                    addProcset();
414:                    procset.add("/Text");
415:                }
416:
417:                // finally create and return the font
418:                PDFFont f = pdfDocument.getFont(type, font, style);
419:                fonts.addElement(f);
420:                return f;
421:            }
422:
423:            /**
424:             * Sets the media size for this page.
425:             *
426:             * <p>Normally, this should be done when the page is created, to avoid
427:             * problems.
428:             *
429:             * @param mediabox Rectangle describing the page size
430:             */
431:            public void setMedia(Rectangle mediabox) {
432:                this .mediabox = mediabox;
433:            }
434:
435:            /**
436:             * Returns the page's media.
437:             * @return Rectangle describing the page size in device units (72dpi)
438:             */
439:            public Rectangle getMedia() {
440:                return mediabox;
441:            }
442:
443:            public Dimension getDimension() {
444:                Rectangle r = getMedia();
445:
446:                // if were landscape or seascape, then we swap the dimensions which
447:                // should fool existing code.
448:                int rot = getOrientation();
449:                if (rot == 90 || rot == 270) {
450:                    return new Dimension(r.height - r.y, r.width - r.x);
451:                }
452:                return new Dimension(r.width - r.x, r.height - r.y);
453:            }
454:
455:            /**
456:             * Sets the page's orientation.
457:             *
458:             * <p>Normally, this should be done when the page is created, to avoid
459:             * problems.
460:             *
461:             * @param rotate Rotation: 0, 90, 180 or 270
462:             */
463:            public void setOrientation(int rotate) {
464:                this .rotate = rotate - (rotate % 90); // must be modulus of 90
465:            }
466:
467:            /**
468:             * Returns the pages orientation
469:             * @see #PORTRAIT
470:             * @see #LANDSCAPE
471:             * @see #INVERTEDPORTRAIT
472:             * @see #SEASCAPE
473:             * @return current rotation of the page
474:             */
475:            public int getOrientation() {
476:                return rotate;
477:            }
478:
479:            /**
480:             * This adds an object that describes some content to this page.
481:             *
482:             * <p><b>Note:</b> Objects that describe contents must be added using this
483:             * method _AFTER_ the PDF.add() method has been called.
484:             *
485:             * @param ob PDFObject describing some contents
486:             */
487:            public void add(PDFObject ob) {
488:                contents.addElement(ob);
489:            }
490:
491:            /**
492:             * This adds an Annotation to the page.
493:             *
494:             * <p>As with other objects, the annotation must be added to the pdf
495:             * document using PDF.add() before adding to the page.
496:             *
497:             * @param ob Annotation to add.
498:             */
499:            public void addAnnotation(PDFObject ob) {
500:                annotations.addElement(ob);
501:            }
502:
503:            /**
504:             * This method adds a text note to the document.
505:             * @param note Text of the note
506:             * @param x Coordinate of note
507:             * @param y Coordinate of note
508:             * @param w Width of the note
509:             * @param h Height of the note
510:             * @return Returns the annotation, so other settings can be changed.
511:             */
512:            public PDFAnnot addNote(String note, int x, int y, int w, int h) {
513:                int xy1[] = cxy(x, y + h);
514:                int xy2[] = cxy(x + w, y);
515:                PDFAnnot ob = new PDFAnnot(xy1[0], xy1[1], xy2[0], xy2[1], note);
516:                pdfDocument.add(ob);
517:                annotations.addElement(ob);
518:                return ob;
519:            }
520:
521:            /**
522:             * Adds a hyperlink to the document.
523:             * @param x Coordinate of active area
524:             * @param y Coordinate of active area
525:             * @param w Width of the active area
526:             * @param h Height of the active area
527:             * @param dest Page that will be displayed when the link is activated. When
528:             * displayed, the zoom factor will be changed to fit the display.
529:             * @return Returns the annotation, so other settings can be changed.
530:             */
531:            public PDFAnnot addLink(int x, int y, int w, int h, PDFObject dest) {
532:                int xy1[] = cxy(x, y + h);
533:                int xy2[] = cxy(x + w, y);
534:                PDFAnnot ob = new PDFAnnot(xy1[0], xy1[1], xy2[0], xy2[1], dest);
535:                pdfDocument.add(ob);
536:                annotations.addElement(ob);
537:                return ob;
538:            }
539:
540:            /**
541:             * Adds a hyperlink to the document.
542:             * @param x Coordinate of active area
543:             * @param y Coordinate of active area
544:             * @param w Width of the active area
545:             * @param h Height of the active area
546:             * @param dest Page that will be displayed when the link is activated
547:             * @param view Rectangle defining what part of the page should be displayed
548:             * (defined in Java coordinates). If this is null, then the page is fitted to
549:             * the display.
550:             * @return Returns the annotation, so other settings can be changed.
551:             */
552:            public PDFAnnot addLink(int x, int y, int w, int h, PDFObject dest,
553:                    int vx, int vy, int vw, int vh) {
554:                int xy1[] = cxy(x, y + h);
555:                int xy2[] = cxy(x + w, y);
556:                int xy3[] = cxy(vx, vy + vh);
557:                int xy4[] = cxy(vx + vw, vy);
558:                PDFAnnot ob = new PDFAnnot(xy1[0], xy1[1], xy2[0], xy2[1],
559:                        dest, xy3[0], xy3[1], xy4[0], xy4[1]);
560:                pdfDocument.add(ob);
561:                annotations.addElement(ob);
562:                return ob;
563:            }
564:
565:            /** Contains the text strings for the xobjects. */
566:            private Vector xobjects = new Vector();
567:
568:            /**
569:             * This adds an XObject resource to the page.
570:             * The string should be of the format /Name ObjectNumber RevisionNumber R as in /Image1 13 0 R .
571:             */
572:            public void addXObject(String inxobject) {
573:                xobjects.addElement(inxobject);
574:            }
575:
576:            /**
577:             * This adds a resource to the page.
578:             * @param resource String defining the resource
579:             */
580:            public void addResource(String resource) {
581:                resources.addElement(resource);
582:            }
583:
584:            /**
585:             * This adds an object that describes a thumbnail for this page.
586:             * <p><b>Note:</b> The object must already exist in the PDF, as only the
587:             * object ID is stored.
588:             * @param thumbnail PDFObject containing the thumbnail
589:             */
590:            public void setThumbnail(PDFObject thumbnail) {
591:                this .thumbnail = thumbnail;
592:            }
593:
594:            /**
595:             * This method attaches an outline to the current page being generated. When
596:             * selected, the outline displays the top of the page.
597:             * @param title Outline title to attach
598:             * @return PDFOutline object created, for addSubOutline if required.
599:             */
600:            public PDFOutline addOutline(String title) {
601:                PDFOutline outline = new PDFOutline(title, this );
602:                pdfDocument.add(outline);
603:                pdfDocument.getOutline().add(outline);
604:                return outline;
605:            }
606:
607:            /**
608:             * This method attaches an outline to the current page being generated.
609:             * When selected, the outline displays the top of the page.
610:             *
611:             * <p>Note: If the outline is not in the top level (ie below another
612:             * outline) then it must <b>not</b> be passed to this method.
613:             *
614:             * @param title Outline title to attach
615:             * @param l Left coordinate of region
616:             * @param b Bottom coordinate of region
617:             * @param r Right coordinate of region
618:             * @param t Top coordinate of region
619:             * @return PDFOutline object created, for addSubOutline if required.
620:             */
621:            public PDFOutline addOutline(String title, int x, int y, int w,
622:                    int h) {
623:                int xy1[] = cxy(x, y + h);
624:                int xy2[] = cxy(x + w, y);
625:                PDFOutline outline = new PDFOutline(title, this , xy1[0],
626:                        xy1[1], xy2[0], xy2[1]);
627:                pdfDocument.add(outline);
628:                pdfDocument.getOutline().add(outline);
629:                return outline;
630:            }
631:
632:            /**
633:             * @param os OutputStream to send the object to
634:             * @exception IOException on error
635:             */
636:            public void write(OutputStream os) throws IOException {
637:                // Write the object header
638:                writeStart(os);
639:
640:                // now the objects body
641:
642:                // the /Parent pages object
643:                os.write("/Parent ".getBytes());
644:                os.write(pdfPageList.toString().getBytes());
645:                os.write("\n".getBytes());
646:
647:                // the /MediaBox for the page size
648:                os.write("/MediaBox [".getBytes());
649:                os.write(Integer.toString(mediabox.x).getBytes());
650:                os.write(" ".getBytes());
651:                os.write(Integer.toString(mediabox.y).getBytes());
652:                os.write(" ".getBytes());
653:                os.write(Integer.toString(mediabox.x + mediabox.width)
654:                        .getBytes());
655:                os.write(" ".getBytes());
656:                os.write(Integer.toString(mediabox.y + mediabox.height)
657:                        .getBytes());
658:                os.write("]\n".getBytes());
659:
660:                // Rotation (if not zero)
661:                if (rotate != 0) {
662:                    os.write("/Rotate ".getBytes());
663:                    os.write(Integer.toString(rotate).getBytes());
664:                    os.write("\n".getBytes());
665:                }
666:
667:                // Now the resources
668:                os.write("/Resources << ".getBytes());
669:                // fonts
670:                if (fonts.size() > 0) {
671:                    //os.write("/Font << ".getBytes());
672:                    os.write("\n/Font << ".getBytes());
673:                    for (Enumeration en = fonts.elements(); en
674:                            .hasMoreElements();) {
675:                        PDFFont font = (PDFFont) en.nextElement();
676:                        os.write(font.getName().getBytes());
677:                        os.write(" ".getBytes());
678:                        os.write(font.toString().getBytes());
679:                        os.write(" ".getBytes());
680:                    }
681:                    os.write(">> ".getBytes());
682:                }
683:                // Now the XObjects
684:                if (xobjects.size() > 0) {
685:                    os.write("\n/XObject << ".getBytes());
686:                    for (Enumeration en = xobjects.elements(); en
687:                            .hasMoreElements();) {
688:                        os.write(en.nextElement().toString().getBytes());
689:                        os.write(" ".getBytes());
690:                    }
691:                    os.write(">> ".getBytes());
692:                }
693:                // Any other resources
694:                for (Enumeration en = resources.elements(); en
695:                        .hasMoreElements();) {
696:                    os.write(en.nextElement().toString().getBytes());
697:                    os.write(" ".getBytes());
698:                }
699:                os.write(">>\n".getBytes());
700:
701:                // The thumbnail
702:                if (thumbnail != null) {
703:                    os.write("/Thumb ".getBytes());
704:                    os.write(thumbnail.toString().getBytes());
705:                    os.write("\n".getBytes());
706:                }
707:
708:                // the /Contents pages object
709:                if (contents.size() > 0) {
710:                    if (contents.size() == 1) {
711:                        PDFObject ob = (PDFObject) contents.elementAt(0);
712:                        os.write("/Contents ".getBytes());
713:                        os.write(ob.toString().getBytes());
714:                        os.write("\n".getBytes());
715:                    } else {
716:                        os.write("/Contents [".getBytes());
717:                        os.write(PDFObject.toArray(contents).getBytes());
718:                        os.write("\n".getBytes());
719:                    }
720:                }
721:
722:                // The /Annots object
723:                if (annotations.size() > 0) {
724:                    os.write("/Annots ".getBytes());
725:                    os.write(PDFObject.toArray(annotations).getBytes());
726:                    os.write("\n".getBytes());
727:                }
728:
729:                // finish off with its footer
730:                writeEnd(os);
731:            }
732:
733:            /**
734:             * This creates a procset and sets up the page to reference it
735:             */
736:            private void addProcset() {
737:                if (procset == null) {
738:                    pdfDocument.add(procset = new procset());
739:                    resources.addElement("/ProcSet " + procset);
740:                }
741:            }
742:
743:            /**
744:             * This defines a procset
745:             */
746:            public class procset extends PDFObject {
747:                private Vector set;
748:
749:                public procset() {
750:                    super (null);
751:                    set = new Vector();
752:
753:                    // Our default procset (use addElement not add, as we dont want a
754:                    // leading space)
755:                    set.addElement("/PDF");
756:                }
757:
758:                /**
759:                 * @param proc Entry to add to the procset
760:                 */
761:                public void add(String proc) {
762:                    set.addElement(" " + proc);
763:                }
764:
765:                /**
766:                 * @param os OutputStream to send the object to
767:                 * @exception IOException on error
768:                 */
769:                public void write(OutputStream os) throws IOException {
770:                    // Write the object header
771:                    //writeStart(os);
772:
773:                    os.write(Integer.toString(objser).getBytes());
774:                    os.write(" 0 obj\n".getBytes());
775:
776:                    // now the objects body
777:                    os.write("[".getBytes());
778:                    for (Enumeration en = set.elements(); en.hasMoreElements();)
779:                        os.write(en.nextElement().toString().getBytes());
780:                    os.write("]\n".getBytes());
781:
782:                    // finish off with its footer
783:                    //writeEnd(os);
784:                    os.write("endobj\n".getBytes());
785:                }
786:            }
787:
788:            /**
789:             * This utility method converts the y coordinate from Java to User space
790:             * within the page.
791:             * @param y Coordinate in Java space
792:             * @return y Coordinate in User space
793:             */
794:            public int cy(int x, int y) {
795:                return cxy(x, y)[1];
796:            }
797:
798:            /**
799:             * This utility method converts the y coordinate from Java to User space
800:             * within the page.
801:             * @param y Coordinate in Java space
802:             * @return y Coordinate in User space
803:             */
804:            public int cx(int x, int y) {
805:                return cxy(x, y)[0];
806:            }
807:
808:            /**
809:             * This utility method converts the Java coordinates to User space
810:             * within the page.
811:             * @param x Coordinate in Java space
812:             * @param y Coordinate in Java space
813:             * @return array containing the x & y Coordinate in User space
814:             */
815:            public int[] cxy(int x, int y) {
816:                int nx = x, ny = y;
817:                int mw = mediabox.width, mh = mediabox.height;
818:
819:                switch (rotate) {
820:                case 0:
821:                    // Portrait
822:                    //nx = x;
823:                    ny = mh - y;
824:                    break;
825:
826:                case 90:
827:                    // Landscape
828:                    nx = y;
829:                    ny = x;
830:                    break;
831:
832:                case 180:
833:                    // Inverse Portrait
834:                    nx = mw - x;
835:                    //ny = y;
836:                    break;
837:
838:                case 270:
839:                    // Seascape
840:                    nx = mw - y;
841:                    ny = mh - x;
842:                    break;
843:                }
844:
845:                int r[] = new int[2];
846:                r[0] = nx;
847:                r[1] = ny;
848:                return r;
849:            }
850:
851:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.