1 package org.faceless.pdf; 3 4 import java.io.*; 5 import java.util.*; 6 7 64 public class PDF 65 { 66 private org.faceless.pdf2.PDF pdf; 67 68 71 public static final String VERSION="BFOPDF $Revision: 1.9 $"; 72 73 76 public static final String SINGLEPAGE = "SinglePage"; 77 78 81 public static final String ONECOLUMN = "OneColumn"; 82 83 87 public static final String TWOCOLUMNLEFT = "TwoColumnLeft"; 88 89 93 public static final String TWOCOLUMNRIGHT = "TwoColumnRight"; 94 95 98 public static final String PAGESIZE_A3 = "842x1190"; 99 100 103 public static final String PAGESIZE_A3_LANDSCAPE = "1190x842"; 104 105 108 public static final String PAGESIZE_A4 = "595x842"; 109 110 113 public static final String PAGESIZE_A4_LANDSCAPE = "842x595"; 114 115 118 public static final String PAGESIZE_A5 = "421x595"; 119 120 123 public static final String PAGESIZE_A5_LANDSCAPE = "595x421"; 124 125 128 public static final String PAGESIZE_A6 = "297x421"; 129 130 133 public static final String PAGESIZE_A6_LANDSCAPE = "421x297"; 134 135 138 public static final String PAGESIZE_LETTER = "612x792"; 139 140 143 public static final String PAGESIZE_LETTER_LANDSCAPE = "792x612"; 144 145 148 public static final String PAGESIZE_LEGAL = "612x1008"; 149 150 153 public static final String PAGESIZE_LEGAL_LANDSCAPE = "1008x612"; 154 155 160 public static final int ACCESS_ALL = 0xFFFFFFFF; 161 162 169 public static final int ACCESS_PRINT = 4; 170 171 175 public static final int ACCESS_EDIT = 8; 176 177 181 public static final int ACCESS_COPY = 16; 182 183 188 public static final int ACCESS_ANNOTATE = 32; 189 190 196 public static final int ACCESS_FORMS = 256; 197 198 204 public static final int ACCESS_EXTRACT = 512; 205 206 213 public static final int ACCESS_ASSEMBLE = 1024; 214 215 224 public static final int ACCESS_HQPRINT = 2048; 225 226 232 public static final int ENCRYPT_NONE = 0; 233 234 240 public static final int ENCRYPT_40BIT = 40; 241 242 250 public static final int ENCRYPT_128BIT = 128; 251 252 253 public PDF(org.faceless.pdf2.PDF pdf) 254 { 255 this.pdf=pdf; 256 } 257 258 261 public PDF() 262 { 263 pdf = new org.faceless.pdf2.PDF(); 264 } 265 266 273 public PDF(PDFReader reader) 274 { 275 pdf = new org.faceless.pdf2.PDF(reader.reader); 276 } 277 278 291 public PDF(PDFReader reader, int revision) 292 { 293 pdf = new org.faceless.pdf2.PDF(reader.reader, revision); 294 } 295 296 303 public PDFPage newPage(String pagesize) 304 { 305 return (PDFPage)PeeredObject.getPeer(pdf.newPage(pagesize)); 306 } 307 308 313 public PDFPage newPage(float width, float height) 314 { 315 return (PDFPage)PeeredObject.getPeer(pdf.newPage((int)width,(int)height)); 316 } 317 318 323 public PDFPage getLastPage() 324 { 325 return getPage(getNumberOfPages()-1); 326 } 327 328 335 public PDFPage getPage(int pagenum) 336 throws ArrayIndexOutOfBoundsException 337 { 338 return (PDFPage)PeeredObject.getPeer(pdf.getPage(pagenum)); 339 } 340 341 345 public int getNumberOfPages() 346 { 347 return pdf.getNumberOfPages(); 348 } 349 350 357 public int getNumberOfRevisions() 358 { 359 return pdf.getNumberOfRevisions(); 360 } 361 362 368 public List getPages() 369 { 370 377 378 return new ListPeer(pdf.getPages()) 379 { 380 public void add(int i, Object o) 381 { 382 super.add(i,o); 383 List l = ((PDFPage)o).page.getAnnotations(); 384 for (i=0;i<l.size();i++) { 385 if (l.get(i) instanceof org.faceless.pdf2.WidgetAnnotation) { 386 org.faceless.pdf2.WidgetAnnotation annot = (org.faceless.pdf2.WidgetAnnotation)l.get(i); 387 pdf.getForm().getElements().put(annot.getField().getForm().getName(annot.getField()), annot.getField()); 388 } 389 } 390 } 391 392 public Object set(int i, Object o) 393 { 394 Object old = super.set(i,o); 395 List l = ((PDFPage)o).page.getAnnotations(); 396 for (i=0;i<l.size();i++) { 397 if (l.get(i) instanceof org.faceless.pdf2.WidgetAnnotation) { 398 org.faceless.pdf2.WidgetAnnotation annot = (org.faceless.pdf2.WidgetAnnotation)l.get(i); 399 pdf.getForm().getElements().put(annot.getField().getForm().getName(annot.getField()), annot.getField()); 400 } 401 } 402 return old; 403 } 404 }; 405 } 406 407 420 public void setLocale(Locale locale) 421 { 422 pdf.setLocale(locale); 423 } 424 425 429 public Locale getLocale() 430 { 431 return pdf.getLocale(); 432 } 433 434 443 public void setLayout(String layout, boolean bookmarks) 444 { 445 pdf.setLayout(layout, bookmarks ? "UseOutlines" : "UseNone"); 446 } 447 448 475 public void setInfo(String key, String val) 476 { 477 pdf.setInfo(key,val); 478 } 479 480 491 public String getInfo(String key) 492 { 493 return pdf.getInfo(key); 494 } 495 496 510 public Map getInfo() 511 { 512 Map m = pdf.getInfo(); 516 Map out = new HashMap(); 517 for (Iterator i = m.entrySet().iterator();i.hasNext();) { 518 Map.Entry entry = (Map.Entry)i.next(); 519 if (entry.getValue() instanceof String || entry.getValue() instanceof Date) { 520 out.put(entry.getKey(), entry.getValue()); 521 } 522 } 523 return Collections.unmodifiableMap(out); 524 } 525 526 533 public void setOpenFullScreen(boolean fullscreen) 534 { 535 pdf.setViewerPreference("FullScreen", fullscreen); 536 } 537 538 578 public void setMetaData(String xmldata) 579 { 580 pdf.setMetaData(xmldata); 581 } 582 583 606 public Reader getMetaData() 607 throws IOException 608 { 609 return pdf.getMetaData(); 610 } 611 612 631 public void setViewerPreference(String key, boolean val) 632 { 633 pdf.setViewerPreference(key, val); 634 } 635 636 642 public boolean getViewerPreference(String key) 643 { 644 return pdf.getViewerPreference(key); 645 } 646 647 659 public void setOpenAction(PDFAction action) 660 { 661 pdf.setAction(org.faceless.pdf2.Event.OPEN, action==null ? null : action.action); 662 } 663 664 671 public PDFAction getOpenAction() 672 { 673 return (PDFAction)PeeredObject.getPeer(pdf.getAction(org.faceless.pdf2.Event.OPEN)); 674 } 675 676 700 public void setNamedAction(String name, PDFAction action) 701 { 702 if (action==null) { 703 pdf.getNamedActions().remove(name); 704 } else { 705 pdf.getNamedActions().put(name, action.action); 706 } 707 } 708 709 718 public Map getNamedActions() 719 { 720 TreeMap map = new TreeMap(); 722 for (Iterator i = pdf.getNamedActions().entrySet().iterator();i.hasNext();) { 723 Map.Entry entry = (Map.Entry)i.next(); 724 map.put(entry.getKey(), PeeredObject.getPeer(entry.getValue())); 725 } 726 return map; 727 } 728 729 738 public List getBookmarks() 739 { 740 return new ListPeer(pdf.getBookmarks()); 741 } 742 743 745 757 public void setSecurityPassword(String s) 758 { 759 org.faceless.pdf2.StandardEncryptionHandler handler = (org.faceless.pdf2.StandardEncryptionHandler)pdf.getEncryptionHandler(); 760 if (s==null) s=""; 761 if (handler==null) { 762 handler = new org.faceless.pdf2.StandardEncryptionHandler(); 763 pdf.setEncryptionHandler(handler); 764 } 765 handler.setOwnerPassword(s); 766 } 767 768 779 public void setPassword(String s) 780 { 781 org.faceless.pdf2.StandardEncryptionHandler handler = (org.faceless.pdf2.StandardEncryptionHandler)pdf.getEncryptionHandler(); 782 if (s==null) s=""; 783 if (handler==null) { 784 handler = new org.faceless.pdf2.StandardEncryptionHandler(); 785 pdf.setEncryptionHandler(handler); 786 } 787 handler.setUserPassword(s); 788 } 789 790 801 public void setEncryptionAlgorithm(int type) 802 { 803 if (type==ENCRYPT_NONE) { 804 pdf.setEncryptionHandler(null); 805 } else { 806 org.faceless.pdf2.StandardEncryptionHandler handler = (org.faceless.pdf2.StandardEncryptionHandler)pdf.getEncryptionHandler(); 807 if (handler==null) { 808 handler = new org.faceless.pdf2.StandardEncryptionHandler(); 809 pdf.setEncryptionHandler(handler); 810 } 811 if (type==ENCRYPT_128BIT) { 812 handler.setAcrobat5Level(handler.PRINT_HIGHRES, handler.EXTRACT_ALL, handler.CHANGE_ALL); 813 } else { 814 handler.setAcrobat3Level(true, true, true, true); 815 } 816 } 817 } 818 819 841 public void setAccessLevel(int level) 842 { 843 org.faceless.pdf2.StandardEncryptionHandler handler = (org.faceless.pdf2.StandardEncryptionHandler)pdf.getEncryptionHandler(); 844 if (handler==null) { 845 handler = new org.faceless.pdf2.StandardEncryptionHandler(); 846 pdf.setEncryptionHandler(handler); 847 } 848 849 if (handler.getVersion()==2) { 850 boolean print = (level&ACCESS_PRINT)!=0; 851 boolean annotate = (level&ACCESS_ANNOTATE)!=0; 852 boolean edit = (level&ACCESS_EDIT)!=0; 853 boolean copy = (level&ACCESS_COPY)!=0; 854 boolean forms = (level&ACCESS_FORMS)!=0; 855 boolean extract = (level&ACCESS_EXTRACT)!=0; 856 boolean assemble = (level&ACCESS_ASSEMBLE)!=0; 857 boolean hqprint = (level&ACCESS_HQPRINT)!=0; 858 859 int newchange=handler.CHANGE_NONE, newprint=handler.PRINT_NONE, newextract=handler.EXTRACT_NONE; 860 861 if (print && hqprint) { 862 newprint=handler.PRINT_HIGHRES; 863 } else if (print) { 864 newprint=handler.PRINT_LOWRES; 865 } else { 866 newprint=handler.PRINT_NONE; 867 } 868 869 if (extract && copy) { 870 newprint=handler.EXTRACT_ALL; 871 } else if (extract) { 872 newprint=handler.EXTRACT_ACCESSIBILITY; 873 } else { 874 newprint=handler.EXTRACT_NONE; 875 } 876 877 if (assemble && annotate && edit) { 878 newchange = handler.CHANGE_ALL; 879 } else if (assemble && annotate) { 880 newchange = handler.CHANGE_ANNOTATIONS; 881 } else if (forms) { 882 newchange = handler.CHANGE_FORMS; 883 } else if (assemble) { 884 newchange = handler.CHANGE_LAYOUT; 885 } else { 886 newchange = handler.CHANGE_NONE; 887 } 888 889 handler.setAcrobat5Level(newprint, newextract, newchange); 890 } 891 else 892 { 893 boolean print = (level&ACCESS_PRINT)!=0; 894 boolean annotate = (level&ACCESS_ANNOTATE)!=0; 895 boolean edit = (level&ACCESS_EDIT)!=0; 896 boolean copy = (level&ACCESS_COPY)!=0; 897 handler.setAcrobat3Level(print, annotate, copy, edit); 898 } 899 } 900 901 902 904 905 913 public Form getForm() 914 { 915 return (Form)PeeredObject.getPeer(pdf.getForm()); 916 } 917 918 928 public void setJavaScript(String javascript) 929 { 930 pdf.setJavaScript(javascript); 931 } 932 933 940 public String getJavaScript() 941 { 942 return pdf.getJavaScript(); 943 } 944 945 956 public synchronized void render(OutputStream out) 957 throws IOException 958 { 959 pdf.render(out); 960 } 961 962 968 public static void setLicenseKey(String key) 969 { 970 org.faceless.pdf2.PDF.setLicenseKey(key); 971 } 972 973 981 public void importFDF(FDF fdf) 982 { 983 pdf.importFDF((org.faceless.pdf2.FDF)fdf.fdf); 984 } 985 } 986 | Popular Tags |