1 20 package gnu.jpdf; 21 22 import java.awt.*; 23 import java.io.*; 24 import java.util.*; 25 26 37 public class PDFPage extends PDFObject implements Serializable 38 { 39 47 48 51 public static final int PORTRAIT = 0; 52 53 56 public static final int LANDSCAPE = 90; 57 58 61 public static final int INVERTEDPORTRAIT = 180; 62 63 66 public static final int SEASCAPE = 270; 67 68 71 public static final Rectangle MEDIA_letter = new Rectangle(0,0,612,792); 72 73 76 public static final Rectangle MEDIA_note = new Rectangle(0,0,540,720); 77 78 81 public static final Rectangle MEDIA_legal = new Rectangle(0,0,612,1008); 82 83 86 public static final Rectangle MEDIA_a0 = new Rectangle(0,0,2380,3368); 87 88 91 public static final Rectangle MEDIA_a1 = new Rectangle(0,0,1684,2380); 92 93 96 public static final Rectangle MEDIA_a2 = new Rectangle(0,0,1190,1684); 97 98 101 public static final Rectangle MEDIA_a3 = new Rectangle(0,0,842,1190); 102 103 106 public static final Rectangle MEDIA_a4 = new Rectangle(0,0,595,842); 107 108 111 public static final Rectangle MEDIA_a5 = new Rectangle(0,0,421,595); 112 113 116 public static final Rectangle MEDIA_a6 = new Rectangle(0,0,297,421); 117 118 121 public static final Rectangle MEDIA_a7 = new Rectangle(0,0,210,297); 122 123 126 public static final Rectangle MEDIA_a8 = new Rectangle(0,0,148,210); 127 128 131 public static final Rectangle MEDIA_a9 = new Rectangle(0,0,105,148); 132 133 136 public static final Rectangle MEDIA_a10 = new Rectangle(0,0,74,105); 137 138 141 public static final Rectangle MEDIA_b0 = new Rectangle(0,0,2836,4008); 142 143 146 public static final Rectangle MEDIA_b1 = new Rectangle(0,0,2004,2836); 147 148 151 public static final Rectangle MEDIA_b2 = new Rectangle(0,0,1418,2004); 152 153 156 public static final Rectangle MEDIA_b3 = new Rectangle(0,0,1002,1418); 157 158 161 public static final Rectangle MEDIA_b4 = new Rectangle(0,0,709,1002); 162 163 166 public static final Rectangle MEDIA_b5 = new Rectangle(0,0,501,709); 167 168 171 public static final Rectangle MEDIA_archE = new Rectangle(0,0,2592,3456); 172 173 176 public static final Rectangle MEDIA_archD = new Rectangle(0,0,1728,2592); 177 178 181 public static final Rectangle MEDIA_archC = new Rectangle(0,0,1296,1728); 182 183 186 public static final Rectangle MEDIA_archB = new Rectangle(0,0,864,1296); 187 188 191 public static final Rectangle MEDIA_archA = new Rectangle(0,0,648,864); 192 193 196 public static final Rectangle MEDIA_flsa = new Rectangle(0,0,612,936); 197 198 201 public static final Rectangle MEDIA_flse = new Rectangle(0,0,612,936); 202 203 206 public static final Rectangle MEDIA_halfletter = new Rectangle(0,0,396,612); 207 208 211 public static final Rectangle MEDIA_11x17 = new Rectangle(0,0,792,1224); 212 213 216 public static final Rectangle MEDIA_ledger = new Rectangle(0,0,1224,792); 217 218 221 protected Rectangle mediabox; 222 223 227 protected PDFObject pdfPageList; 228 229 232 protected Vector contents; 233 234 243 protected int rotate; 244 245 249 protected PDFObject thumbnail; 250 251 254 protected Vector annotations; 255 256 259 protected Vector resources; 260 261 protected Vector imageResources; 263 264 267 protected Vector fonts; 268 269 272 274 280 protected boolean hasImageB,hasImageC,hasImageI; 281 protected procset procset; 282 283 292 public PDFPage() 293 { 294 super("/Page"); 295 mediabox = MEDIA_letter; 296 contents = new Vector(); 297 rotate = 0; 298 thumbnail = null; 299 annotations = new Vector(); 300 resources = new Vector(); 301 imageResources = new Vector(); 303 fonts = new Vector(); 304 procset = null; 305 } 306 307 312 public PDFPage(Rectangle mediabox) 313 { 314 this(); 315 setMedia(mediabox); 316 } 317 318 326 public PDFPage(int rotate) 327 { 328 this(); 329 setOrientation(rotate); 330 } 331 332 341 public PDFPage(Rectangle mediabox,int rotate) 342 { 343 this(); 344 setMedia(mediabox); 345 setOrientation(rotate); 346 } 347 348 public void addToProcset(String proc) { 349 if (procset == null) { 350 addProcset(); 351 } 352 procset.add(proc); 353 } 354 355 361 public PDFGraphics getGraphics() { 362 try { 363 PDFGraphics g = new PDFGraphics(); 364 g.init(this); 365 return g; 366 } catch(Exception ex) { 367 ex.printStackTrace(); 368 } 369 370 return null; 371 } 372 373 379 public PDFFont getFont(String type,String font,int style) { 380 for(Enumeration en = fonts.elements(); en.hasMoreElements(); ) { 384 PDFFont ft = (PDFFont) en.nextElement(); 385 if(ft.equals(type,font,style)) 386 return ft; 387 } 388 389 391 if(fonts.size()==0) { 394 addProcset(); 395 procset.add("/Text"); 396 } 397 398 PDFFont f = pdfDocument.getFont(type,font,style); 400 fonts.addElement(f); 401 return f; 402 } 403 404 412 public void setMedia(Rectangle mediabox) 413 { 414 this.mediabox = mediabox; 415 } 416 417 421 public Rectangle getMedia() { 422 return mediabox; 423 } 424 425 public Dimension getDimension() { 426 Rectangle r = getMedia(); 427 428 int rot = getOrientation(); 431 if(rot==90 || rot==270) { 432 return new Dimension(r.height-r.y,r.width-r.x); 433 } 434 return new Dimension(r.width-r.x,r.height-r.y); 435 } 436 437 438 446 public void setOrientation(int rotate) 447 { 448 this.rotate = rotate - (rotate%90); } 450 451 459 public int getOrientation() { 460 return rotate; 461 } 462 463 471 public void add(PDFObject ob) { 472 contents.addElement(ob); 473 } 474 475 483 public void addAnnotation(PDFObject ob) { 484 annotations.addElement(ob); 485 } 486 487 496 public PDFAnnot addNote(String note,int x,int y,int w,int h) { 497 int xy1[] = cxy(x,y+h); 498 int xy2[] = cxy(x+w,y); 499 PDFAnnot ob = new PDFAnnot(xy1[0],xy1[1], 500 xy2[0],xy2[1], 501 note); 502 pdfDocument.add(ob); 503 annotations.addElement(ob); 504 return ob; 505 } 506 507 517 public PDFAnnot addLink(int x,int y,int w,int h,PDFObject dest) { 518 int xy1[] = cxy(x,y+h); 519 int xy2[] = cxy(x+w,y); 520 PDFAnnot ob = new PDFAnnot(xy1[0],xy1[1], 521 xy2[0],xy2[1], 522 dest 523 ); 524 pdfDocument.add(ob); 525 annotations.addElement(ob); 526 return ob; 527 } 528 529 541 public PDFAnnot addLink(int x,int y,int w,int h, 542 PDFObject dest, 543 int vx,int vy,int vw,int vh) { 544 int xy1[] = cxy(x,y+h); 545 int xy2[] = cxy(x+w,y); 546 int xy3[] = cxy(vx,vy+vh); 547 int xy4[] = cxy(vx+vw,vy); 548 PDFAnnot ob = new PDFAnnot(xy1[0],xy1[1], 549 xy2[0],xy2[1], 550 dest, 551 xy3[0],xy3[1], 552 xy4[0],xy4[1] 553 ); 554 pdfDocument.add(ob); 555 annotations.addElement(ob); 556 return ob; 557 } 558 559 560 private Vector xobjects = new Vector(); 561 565 public void addXObject(String inxobject){ 566 xobjects.addElement(inxobject); 567 } 568 569 573 public void addResource(String resource) { 574 resources.addElement(resource); 575 } 576 577 public void addImageResource(String resource) { 579 imageResources.addElement(resource); 580 } 581 582 588 public void setThumbnail(PDFObject thumbnail) 589 { 590 this.thumbnail = thumbnail; 591 } 592 593 599 public PDFOutline addOutline(String title) { 600 PDFOutline outline = new PDFOutline(title,this); 601 pdfDocument.add(outline); 602 pdfDocument.getOutline().add(outline); 603 return outline; 604 } 605 606 620 public PDFOutline addOutline(String title,int x,int y,int w,int h) { 621 int xy1[] = cxy(x,y+h); 622 int xy2[] = cxy(x+w,y); 623 PDFOutline outline = new PDFOutline(title,this, 624 xy1[0],xy1[1], 625 xy2[0],xy2[1]); 626 pdfDocument.add(outline); 627 pdfDocument.getOutline().add(outline); 628 return outline; 629 } 630 631 635 public void write(OutputStream os) throws IOException 636 { 637 writeStart(os); 639 640 642 os.write("/Parent ".getBytes()); 644 os.write(pdfPageList.toString().getBytes()); 645 os.write("\n".getBytes()); 646 647 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).getBytes()); 654 os.write(" ".getBytes()); 655 os.write(Integer.toString(mediabox.y+mediabox.height).getBytes()); 656 os.write("]\n".getBytes()); 657 658 if(rotate!=0) { 660 os.write("/Rotate ".getBytes()); 661 os.write(Integer.toString(rotate).getBytes()); 662 os.write("\n".getBytes()); 663 } 664 665 os.write("/Resources << ".getBytes()); 667 if(fonts.size()>0) { 669 os.write("\n/Font << ".getBytes()); 671 for(Enumeration en=fonts.elements();en.hasMoreElements();) { 672 PDFFont font = (PDFFont)en.nextElement(); 673 os.write(font.getName().getBytes()); 674 os.write(" ".getBytes()); 675 os.write(font.toString().getBytes()); 676 os.write(" ".getBytes()); 677 } 678 os.write(">> ".getBytes()); 679 } 680 if (xobjects.size() > 0){ 682 os.write("\n/XObject << ".getBytes()); 683 for(Enumeration en=xobjects.elements();en.hasMoreElements();) { 684 os.write(en.nextElement().toString().getBytes()); 685 os.write(" ".getBytes()); 686 } 687 os.write(">> ".getBytes()); 688 } 689 for(Enumeration en=resources.elements();en.hasMoreElements();) { 691 String str = en.nextElement().toString(); 692 os.write(str.getBytes()); 693 os.write(" ".getBytes()); 694 } 695 if(imageResources.size() > 0) { 697 os.write("/XObject << ".getBytes()); 698 for(Enumeration en=imageResources.elements();en.hasMoreElements();) { 699 String str = en.nextElement().toString(); 700 os.write(str.getBytes()); 701 os.write(" ".getBytes()); 702 } 703 os.write(" >> ".getBytes()); 704 } 705 os.write(">>\n".getBytes()); 706 707 if(thumbnail!=null) { 709 os.write("/Thumb ".getBytes()); 710 os.write(thumbnail.toString().getBytes()); 711 os.write("\n".getBytes()); 712 } 713 714 if(contents.size()>0) { 716 if(contents.size()==1) { 717 PDFObject ob = (PDFObject)contents.elementAt(0); 718 os.write("/Contents ".getBytes()); 719 os.write(ob.toString().getBytes()); 720 os.write("\n".getBytes()); 721 } else { 722 os.write("/Contents [".getBytes()); 723 os.write(PDFObject.toArray(contents).getBytes()); 724 os.write("\n".getBytes()); 725 } 726 } 727 728 if(annotations.size()>0) { 730 os.write("/Annots ".getBytes()); 731 os.write(PDFObject.toArray(annotations).getBytes()); 732 os.write("\n".getBytes()); 733 } 734 735 writeEnd(os); 737 } 738 739 742 private void addProcset() { 743 if(procset==null) { 744 pdfDocument.add(procset = new procset()); 745 resources.addElement("/ProcSet "+procset); 746 } 747 } 748 749 752 public class procset extends PDFObject { 753 private Vector set; 754 755 public procset() { 756 super(null); 757 set = new Vector(); 758 759 set.addElement("/PDF"); 762 } 763 764 767 public void add(String proc) { 768 set.addElement(" "+proc); 769 } 770 771 775 public void write(OutputStream os) throws IOException { 776 779 os.write(Integer.toString(objser).getBytes()); 780 os.write(" 0 obj\n".getBytes()); 781 782 os.write("[".getBytes()); 784 for(Enumeration en = set.elements(); en.hasMoreElements(); ) 785 os.write(en.nextElement().toString().getBytes()); 786 os.write("]\n".getBytes()); 787 788 os.write("endobj\n".getBytes()); 791 } 792 } 793 794 800 public int cy(int x,int y) { 801 return cxy(x,y)[1]; 802 } 803 804 810 public int cx(int x,int y) { 811 return cxy(x,y)[0]; 812 } 813 814 821 public int[] cxy(int x,int y) { 822 int nx = x, ny = y; 823 int mw = mediabox.width, mh = mediabox.height; 824 825 switch(rotate) 826 { 827 case 0: 828 ny = mh - y; 831 break; 832 833 case 90: 834 nx = y; 836 ny = x; 837 break; 838 839 case 180: 840 nx = mw - x; 842 break; 844 845 case 270: 846 nx = mw - y; 848 ny = mh - x; 849 break; 850 } 851 852 int r[] = new int[2]; 853 r[0] = nx; 854 r[1] = ny; 855 return r; 856 } 857 858 } 859 | Popular Tags |