1 3 package org.faceless.pdf; 4 5 import java.util.*; 6 import java.io.*; 7 8 217 public final class PDFPage extends PeeredObject 218 { 219 final org.faceless.pdf2.PDFPage page; 220 private PDFStyle tempstyle; 221 private State state; 222 private Stack statestack; 223 private float translatex, translatey, scalex, scaley, canvaswidth, canvasheight; 224 225 private class State 226 { 227 float translatex, translatey, scalex, scaley; 228 public State() 229 { 230 scalex=scaley=1; 231 } 232 } 233 234 235 239 public static final int FILTER_FLATE = 0; 240 241 244 public static final int FILTER_NONE = 0; 245 246 249 public static final int INCHES=4; 250 251 254 public static final int CM=8; 255 256 259 public static final int MM=12; 260 261 264 public static final int PICAS=16; 265 266 271 public static final int PERCENT=20; 272 273 276 public static final int POINTS=24; 277 278 281 public static final int PAGEBOTTOM=0; 282 283 286 public static final int PAGETOP=1; 287 288 291 public static final int PAGELEFT=0; 292 293 296 public static final int PAGERIGHT=2; 297 298 303 public static final int BARCODE39=0; 304 305 311 public static final int BARCODE39CHECKSUM=1; 312 313 319 public static final int BARCODE25=2; 320 321 329 public static final int BARCODE25CHECKSUM=3; 330 331 338 public static final int BARCODE39X=4; 339 340 347 public static final int BARCODE39XCHECKSUM=5; 348 349 358 public static final int BARCODE128=6; 359 360 369 public static final int BARCODEEAN13=7; 370 371 380 public static final int BARCODEUPCA=9; 381 382 389 public static final int BARCODECODABAR=8; 390 391 392 PDFPage(org.faceless.pdf2.PDFPage page) 393 { 394 this.page = page; 395 state=new State(); 396 statestack = new Stack(); 397 setCanvas(0,0,page.getWidth(), page.getHeight(), POINTS, PAGEBOTTOM|PAGELEFT); 398 } 399 400 Object getPeer() 401 { 402 return page; 403 } 404 405 410 public float getWidth() 411 { 412 return page.getWidth(); 413 } 414 415 420 public float getHeight() 421 { 422 return page.getHeight(); 423 } 424 425 429 public int getPageNumber() 430 { 431 return page.getPageNumber(); 432 } 433 434 450 public void setCanvas(float left, float bottom, float width, float height, int scale, int zerocorner) 451 { 452 float newscale=page.UNITS_POINTS; 453 int neworigin=0; 454 455 translatex=left; 456 translatey=bottom; 457 if (scale==POINTS) scalex=scaley=page.UNITS_POINTS; 458 else if (scale==INCHES) scalex=scaley=page.UNITS_INCHES; 459 else if (scale==CM) scalex=scaley=page.UNITS_CM; 460 else if (scale==MM) scalex=scaley=page.UNITS_MM; 461 else if (scale==PICAS) scalex=scaley=page.UNITS_PICAS; 462 else if (scale==PERCENT) { 463 scaley=height/100; 464 scalex=width/100; 465 } 466 467 if ((zerocorner&PAGETOP)==PAGETOP) { scaley=-scaley; translatey+=height; } 468 if ((zerocorner&PAGERIGHT)==PAGERIGHT) { scalex=-scalex; translatex+=width; } 469 470 canvaswidth=width; 471 canvasheight=height; 472 } 473 474 private final float cx(float x) { return (translatex+x*scalex)*state.scalex + state.translatex; } 475 private final float cy(float y) { return (translatey+y*scaley)*state.scaley + state.translatey; } 476 private final float canvasx(float x) { return translatex + x*scalex; } 477 private final float canvasy(float y) { return translatey + y*scaley; } 478 479 480 483 public float getCanvasHeight() 484 { 485 return canvasheight; 486 } 487 488 491 public float getCanvasWidth() 492 { 493 return canvaswidth; 494 } 495 496 499 public void setStyle(PDFStyle style) 500 { 501 page.setStyle(style.style); 502 this.tempstyle=style; 503 } 504 505 510 public PDFStyle getStyle() 511 { 512 return (PDFStyle)PeeredObject.getPeer(page.getStyle()); 513 } 514 515 524 public void drawLine(float x1, float y1, float x2, float y2) 525 { 526 page.drawLine(cx(x1),cy(y1),cx(x2),cy(y2)); 527 } 528 529 544 public void drawRectangle(float x1, float y1, float x2, float y2) 545 { 546 page.drawRectangle(cx(x1),cy(y1),cx(x2),cy(y2)); 547 } 548 549 571 public void drawRoundedRectangle(float x1, float y1, float x2, float y2, float r) 572 { 573 page.drawRoundedRectangle(cx(x1),cy(y1),cx(x2),cy(y2),r); 574 } 575 576 577 595 public void drawPolygon(float[] x, float[] y) 596 { 597 float[] x2 = new float[x.length]; 598 float[] y2 = new float[y.length]; 599 for (int i=0;i<x.length;i++) x2[i]=cx(x[i]); 600 for (int i=0;i<y.length;i++) y2[i]=cy(y[i]); 601 602 page.drawPolygon(x2, y2); 603 } 604 605 606 627 public void drawEllipse(float x1, float y1, float x2, float y2) 628 { 629 page.drawEllipse(cx(x1), cy(y1), cx(x2), cy(y2)); 630 } 631 632 633 644 public void drawCircle(float x, float y, float r) 645 { 646 page.drawEllipse(cx(x-r), cy(y-r), cx(x+r), cy(y+r)); 647 } 648 649 666 public void drawEllipseArc(float x1, float y1, float x2, float y2, float start, float end) 667 { 668 page.drawEllipseArc(cx(x1),cy(y1),cx(x2),cy(y2),start,end); 669 } 670 671 685 public void drawCircleArc(float x, float y, float r, float start, float end) 686 { 687 page.drawEllipseArc(cx(x-r),cy(y-r),cx(x+r),cy(y+r),start,end); 688 } 689 690 691 699 public void pathMove(float x, float y) 700 { 701 page.pathMove(cx(x),cy(y)); 702 } 703 704 711 public void pathLine(float x, float y) 712 { 713 page.pathLine(cx(x),cy(y)); 714 } 715 716 727 public void pathBezier(float cx1, float cy1, float cx2, float cy2, float x, float y) 728 { 729 page.pathBezier(cx(cx1),cy(cy1),cx(cx2),cy(cy2),cx(x),cy(y)); 730 } 731 732 742 public void pathArc(float width, float height, float start, float end) 743 { 744 page.pathArc(width,height,start,end); 745 } 746 747 752 public void pathClose() 753 { 754 page.pathClose(); 755 } 756 757 762 public void pathCancel() 763 { 764 page.pathCancel(); 765 } 766 767 784 public void pathPaint() 785 { 786 page.pathPaint(); 787 } 788 789 795 public void pathClipAndPaint() 796 { 797 page.pathClipAndPaint(); 798 } 799 800 817 public void pathClip() 818 { 819 page.pathClip(); 820 } 821 822 841 public void clipRectangle(float x1, float y1, float x2, float y2) 842 { 843 page.clipRectangle(cx(x1),cy(y1),cx(x2),cy(y2)); 844 } 845 846 847 868 public void clipRoundedRectangle(float x1, float y1, float x2, float y2, float r) 869 { 870 page.clipRoundedRectangle(cx(x1),cy(y1),cx(x2),cy(y2),r); 871 } 872 873 891 public void clipPolygon(float[] x, float[] y) 892 { 893 float[] x2 = new float[x.length]; 894 float[] y2 = new float[y.length]; 895 for (int i=0;i<x.length;i++) x2[i]=cx(x[i]); 896 for (int i=0;i<y.length;i++) y2[i]=cy(y[i]); 897 898 page.clipPolygon(x2,y2); 899 } 900 901 920 public void clipEllipse(float x1, float y1, float x2, float y2) 921 { 922 page.clipEllipse(cx(x1),cy(y1),cx(x2),cy(y2)); 923 } 924 925 943 public void clipCircle(float x, float y, float r) 944 { 945 page.clipCircle(cx(x),cy(y),r); 946 } 947 948 976 public void save() 977 { 978 page.save(); 979 statestack.push(state); 980 } 981 982 988 public void restore() 989 { 990 page.restore(); 991 state = (State)statestack.pop(); 992 } 993 994 1002 public void undo() 1003 { 1004 throw new UnsupportedOperationException ("The undo() method has been removed in version 2 with no replacement. You'll need to rewrite your code or stick with version 1"); 1005 } 1006 1007 1008 1017 public void rotate(float x, float y, double ang) 1018 { 1019 page.rotate(cx(x),cy(y), ang); 1020 } 1021 1022 1031 public void translate(float x, float y) 1032 { 1033 state.translatex += x*state.scalex; 1034 state.translatey += y*state.scaley; 1035 } 1036 1037 1046 public void scale(float x, float y) 1047 { 1048 if (x*y==0) throw new IllegalArgumentException ("X or Y is zero"); 1049 state.scalex *= x; 1050 state.scaley *= y; 1051 } 1052 1053 1064 public void setOpenAction(PDFAction action) 1065 { 1066 page.setAction(org.faceless.pdf2.Event.OPEN, action==null ? null : action.action); 1067 } 1068 1069 1080 public void setCloseAction(PDFAction action) 1081 { 1082 page.setAction(org.faceless.pdf2.Event.CLOSE, action==null ? null : action.action); 1083 } 1084 1085 1093 public PDFAction getOpenAction() 1094 { 1095 return (PDFAction)PeeredObject.getPeer(page.getAction(org.faceless.pdf2.Event.OPEN)); 1096 } 1097 1098 1106 public PDFAction getCloseAction() 1107 { 1108 return (PDFAction)PeeredObject.getPeer(page.getAction(org.faceless.pdf2.Event.CLOSE)); 1109 } 1110 1111 1112 1120 public void setFilter(int filter) 1121 { 1122 } 1124 1125 1132 public void addAnnotation(PDFAnnotation annotation) 1133 { 1134 page.getAnnotations().add(annotation.annot); 1135 } 1136 1137 1143 public void removeAnnotation(PDFAnnotation annotation) 1144 { 1145 page.getAnnotations().remove(annotation.annot); 1146 } 1147 1148 1154 public PDFAnnotation[] getAnnotations() 1155 { 1156 List l = page.getAnnotations(); 1157 PDFAnnotation[] z = new PDFAnnotation[l.size()]; 1158 for (int i=0;i<z.length;i++) { 1159 z[i]=(PDFAnnotation)PeeredObject.getPeer(l.get(i)); 1160 } 1161 return z; 1162 } 1163 1164 1180 public void seekStart() 1181 { 1182 page.seekStart(); 1183 } 1184 1185 1197 public void seekEnd() 1198 { 1199 page.seekEnd(); 1200 } 1201 1202 1203 1220 public void drawImage(PDFImage image, float x1, float y1, float x2, float y2) 1221 { 1222 page.drawImage(image.image,cx(x1),cy(y1),cx(x2),cy(y2)); 1223 } 1224 1225 1257 public String requote(String text) 1258 { 1259 char[] c = text.toCharArray(); 1260 PDFStyle style = getStyle(); 1262 if (style!=null && style.getFont()!=null && style.getFont().requote(c,0,c.length, Locale.getDefault())) { 1263 return new String (c,0,c.length); 1264 } else { 1265 return text; 1266 } 1267 } 1268 1269 1300 public float drawBarCode(int type, String code, float x, float y, boolean showtext, float width) 1301 throws IllegalArgumentException 1302 { 1303 return drawBarCode(type, code, cx(x), cy(y), showtext, width, 18, 2.8f); 1304 } 1305 1306 1340 public float drawBarCode(int type, String code, float x, float y, boolean showtext, float width, int height, float ratio) 1341 throws IllegalArgumentException 1342 { 1343 int newtype; 1344 if (type==BARCODE39) newtype=org.faceless.pdf2.BarCode.CODE39; 1345 else if (type==BARCODE39CHECKSUM) newtype=org.faceless.pdf2.BarCode.CODE39_CHECKSUM; 1346 else if (type==BARCODE39X) newtype=org.faceless.pdf2.BarCode.CODE39X; 1347 else if (type==BARCODE39XCHECKSUM) newtype=org.faceless.pdf2.BarCode.CODE39X_CHECKSUM; 1348 else if (type==BARCODE25) newtype=org.faceless.pdf2.BarCode.INTERLEAVED25; 1349 else if (type==BARCODE25CHECKSUM) newtype=org.faceless.pdf2.BarCode.INTERLEAVED25_CHECKSUM; 1350 else if (type==BARCODE128) newtype=org.faceless.pdf2.BarCode.CODE128; 1351 else if (type==BARCODEEAN13) newtype=org.faceless.pdf2.BarCode.EAN13; 1352 else if (type==BARCODEUPCA) newtype=org.faceless.pdf2.BarCode.UPCA; 1353 else if (type==BARCODECODABAR) newtype=org.faceless.pdf2.BarCode.CODABAR; 1354 else throw new IllegalArgumentException ("Unknown barcode type"); 1355 1356 org.faceless.pdf2.BarCode codeo = new org.faceless.pdf2.BarCode(type, code); 1357 codeo.setShowText(showtext); 1358 codeo.setBarWidth(width); 1359 codeo.setHeight(height); 1360 codeo.setBarRatio(ratio); 1361 1362 float barwidth=codeo.getWidth(); 1363 float fontheight = (showtext ? width*8 : 0)*1.25f; 1364 float barheight=height+fontheight; 1365 1366 page.drawBarCode(codeo, cx(x), cy(y)+(barheight/2)-(fontheight/2), cx(x)+barwidth, cy(y)-(barheight/2)-(fontheight/2)); 1367 return barwidth; 1368 } 1369 1370 1376 public void setMetaData(String xmldata) 1377 { 1378 page.setMetaData(xmldata); 1379 } 1380 1381 1388 public Reader getMetaData() 1389 throws IOException 1390 { 1391 return page.getMetaData(); 1392 } 1393 1394 1429 public void drawPage(PDFPage page, float x1, float y1, float x2, float y2) 1430 { 1431 page.page.flush(); 1432 org.faceless.pdf2.PDFCanvas canvas = new org.faceless.pdf2.PDFCanvas(page.page); 1433 this.page.drawCanvas(canvas, cx(x1), cy(y1), cx(x2), cy(y2)); 1434 1435 if (page.page.getAnnotations().size()>0) { 1436 org.faceless.pdf2.PDFPage clone = new org.faceless.pdf2.PDFPage(page.page); 1437 1438 x1 = canvasx(x1); 1439 y1 = canvasy(y1); 1440 x2 = canvasx(x2); 1441 y2 = canvasy(y2); 1442 if (x1>x2) { float t=x1; x1=x2; x2=t; } 1443 if (y1>y2) { float t=y1; y1=y2; y2=t; } 1444 1445 List annots = clone.getAnnotations(); 1447 for (int i=0;i<annots.size();i++) { 1448 org.faceless.pdf2.PDFAnnotation annot = (org.faceless.pdf2.PDFAnnotation)annots.get(i); 1449 float[] f = annot.getRectangle(); 1450 if (f!=null) { 1451 f[0] = (f[0]/clone.getWidth()*(x2-x1))+x1; 1454 f[1] = (f[1]/clone.getHeight()*(y2-y1))+y1; 1455 f[2] = (f[2]/clone.getWidth()*(x2-x1))+x1; 1456 f[3] = (f[3]/clone.getHeight()*(y2-y1))+y1; 1457 1458 annot.setRectangle(f[0], f[1], f[2], f[3]); 1459 } 1461 this.page.getAnnotations().add(annot); 1462 } 1463 } 1464 } 1465 1466 1478 public void drawText(String text, float x, float y) 1479 { 1480 page.drawText(text,cx(x),cy(y)); 1481 } 1482 1483 1501 public void drawTextLink(String text, float x, float y, PDFAction action) 1502 { 1503 page.drawTextLink(text,cx(x),cy(y),action.action); 1504 } 1505 1506 1532 public void beginText(float x1, float y1, float x2, float y2) 1533 { 1534 page.beginText(cx(x1),cy(y1),cx(x2),cy(y2)); 1535 } 1536 1537 1562 public float continueText(float x1, float y1, float x2, float y2, PDFPage page) 1563 { 1564 return this.page.continueText(cx(x1),cy(y1),cx(x2),cy(y2),page.page); 1565 } 1566 1567 1584 public float endText(boolean justifylast) 1585 { 1586 return page.endText(justifylast); 1587 } 1588 1589 1596 public float discardText() 1597 { 1598 return page.discardText(); 1599 } 1600 1601 1635 public float drawText(String text) 1636 { 1637 return page.drawText(text); 1638 } 1639 1640 1664 public void beginTextLink(PDFAction action, PDFStyle linkstyle) 1665 { 1666 page.beginTextLink(action.action, linkstyle.style); 1667 } 1668 1669 1686 public PDFAnnotation[] endTextLink() 1687 { 1688 org.faceless.pdf2.PDFAnnotation[] newannots = page.endTextLink(); 1689 PDFAnnotation[] oldannots = new PDFAnnotation[newannots.length]; 1690 for (int i=0;i<newannots.length;i++) { 1691 oldannots[i]=(PDFAnnotation)PeeredObject.getPeer(newannots[i]); 1692 } 1693 return oldannots; 1694 } 1695 1696 1702 public void drawLayoutBox(LayoutBox box, float x, float y) 1703 { 1704 page.drawLayoutBox(box.box,cx(x),cy(y)); 1705 } 1706 1707 public String toString() 1708 { 1709 return "{Page #"+getPageNumber()+"}"; 1710 } 1711} 1712 | Popular Tags |