1 50 51 package com.lowagie.text.pdf; 52 53 import java.io.IOException ; 54 import java.net.URL ; 55 import java.util.ArrayList ; 56 57 import com.lowagie.text.ExceptionConverter; 58 import com.lowagie.text.pdf.collection.PdfTargetDictionary; 59 60 65 66 public class PdfAction extends PdfDictionary { 67 68 70 public static final int FIRSTPAGE = 1; 71 73 public static final int PREVPAGE = 2; 74 76 public static final int NEXTPAGE = 3; 77 79 public static final int LASTPAGE = 4; 80 81 83 public static final int PRINTDIALOG = 5; 84 85 86 public static final int SUBMIT_EXCLUDE = 1; 87 88 public static final int SUBMIT_INCLUDE_NO_VALUE_FIELDS = 2; 89 90 public static final int SUBMIT_HTML_FORMAT = 4; 91 92 public static final int SUBMIT_HTML_GET = 8; 93 94 public static final int SUBMIT_COORDINATES = 16; 95 96 public static final int SUBMIT_XFDF = 32; 97 98 public static final int SUBMIT_INCLUDE_APPEND_SAVES = 64; 99 100 public static final int SUBMIT_INCLUDE_ANNOTATIONS = 128; 101 102 public static final int SUBMIT_PDF = 256; 103 104 public static final int SUBMIT_CANONICAL_FORMAT = 512; 105 106 public static final int SUBMIT_EXCL_NON_USER_ANNOTS = 1024; 107 108 public static final int SUBMIT_EXCL_F_KEY = 2048; 109 110 public static final int SUBMIT_EMBED_FORM = 8196; 111 112 public static final int RESET_EXCLUDE = 1; 113 114 116 118 public PdfAction() { 119 } 120 121 126 127 public PdfAction(URL url) { 128 this(url.toExternalForm()); 129 } 130 131 136 public PdfAction(URL url, boolean isMap) { 137 this(url.toExternalForm(), isMap); 138 } 139 140 145 146 public PdfAction(String url) { 147 this(url, false); 148 } 149 150 155 156 public PdfAction(String url, boolean isMap) { 157 put(PdfName.S, PdfName.URI); 158 put(PdfName.URI, new PdfString(url)); 159 if (isMap) 160 put(PdfName.ISMAP, PdfBoolean.PDFTRUE); 161 } 162 163 167 168 PdfAction(PdfIndirectReference destination) { 169 put(PdfName.S, PdfName.GOTO); 170 put(PdfName.D, destination); 171 } 172 173 178 179 public PdfAction(String filename, String name) { 180 put(PdfName.S, PdfName.GOTOR); 181 put(PdfName.F, new PdfString(filename)); 182 put(PdfName.D, new PdfString(name)); 183 } 184 185 190 191 public PdfAction(String filename, int page) { 192 put(PdfName.S, PdfName.GOTOR); 193 put(PdfName.F, new PdfString(filename)); 194 put(PdfName.D, new PdfLiteral("[" + (page - 1) + " /FitH 10000]")); 195 } 196 197 201 public PdfAction(int named) { 202 put(PdfName.S, PdfName.NAMED); 203 switch (named) { 204 case FIRSTPAGE: 205 put(PdfName.N, PdfName.FIRSTPAGE); 206 break; 207 case LASTPAGE: 208 put(PdfName.N, PdfName.LASTPAGE); 209 break; 210 case NEXTPAGE: 211 put(PdfName.N, PdfName.NEXTPAGE); 212 break; 213 case PREVPAGE: 214 put(PdfName.N, PdfName.PREVPAGE); 215 break; 216 case PRINTDIALOG: 217 put(PdfName.S, PdfName.JAVASCRIPT); 218 put(PdfName.JS, new PdfString("this.print(true);\r")); 219 break; 220 default: 221 throw new RuntimeException ("Invalid named action."); 222 } 223 } 224 225 235 public PdfAction(String application, String parameters, String operation, String defaultDir) { 236 put(PdfName.S, PdfName.LAUNCH); 237 if (parameters == null && operation == null && defaultDir == null) 238 put(PdfName.F, new PdfString(application)); 239 else { 240 PdfDictionary dic = new PdfDictionary(); 241 dic.put(PdfName.F, new PdfString(application)); 242 if (parameters != null) 243 dic.put(PdfName.P, new PdfString(parameters)); 244 if (operation != null) 245 dic.put(PdfName.O, new PdfString(operation)); 246 if (defaultDir != null) 247 dic.put(PdfName.D, new PdfString(defaultDir)); 248 put(PdfName.WIN, dic); 249 } 250 } 251 252 263 public static PdfAction createLaunch(String application, String parameters, String operation, String defaultDir) { 264 return new PdfAction(application, parameters, operation, defaultDir); 265 } 266 267 275 public static PdfAction rendition(String file, PdfFileSpecification fs, String mimeType, PdfIndirectReference ref) throws IOException { 276 PdfAction js = new PdfAction(); 277 js.put(PdfName.S, PdfName.RENDITION); 278 js.put(PdfName.R, new PdfRendition(file, fs, mimeType)); 279 js.put(new PdfName("OP"), new PdfNumber(0)); 280 js.put(new PdfName("AN"), ref); 281 return js; 282 } 283 284 294 public static PdfAction javaScript(String code, PdfWriter writer, boolean unicode) { 295 PdfAction js = new PdfAction(); 296 js.put(PdfName.S, PdfName.JAVASCRIPT); 297 if (unicode && code.length() < 50) { 298 js.put(PdfName.JS, new PdfString(code, PdfObject.TEXT_UNICODE)); 299 } 300 else if (!unicode && code.length() < 100) { 301 js.put(PdfName.JS, new PdfString(code)); 302 } 303 else { 304 try { 305 byte b[] = PdfEncodings.convertToBytes(code, unicode ? PdfObject.TEXT_UNICODE : PdfObject.TEXT_PDFDOCENCODING); 306 PdfStream stream = new PdfStream(b); 307 stream.flateCompress(); 308 js.put(PdfName.JS, writer.addToBody(stream).getIndirectReference()); 309 } 310 catch (Exception e) { 311 throw new ExceptionConverter(e); 312 } 313 } 314 return js; 315 } 316 317 324 public static PdfAction javaScript(String code, PdfWriter writer) { 325 return javaScript(code, writer, false); 326 } 327 328 334 static PdfAction createHide(PdfObject obj, boolean hide) { 335 PdfAction action = new PdfAction(); 336 action.put(PdfName.S, PdfName.HIDE); 337 action.put(PdfName.T, obj); 338 if (!hide) 339 action.put(PdfName.H, PdfBoolean.PDFFALSE); 340 return action; 341 } 342 343 349 public static PdfAction createHide(PdfAnnotation annot, boolean hide) { 350 return createHide(annot.getIndirectReference(), hide); 351 } 352 353 359 public static PdfAction createHide(String name, boolean hide) { 360 return createHide(new PdfString(name), hide); 361 } 362 363 static PdfArray buildArray(Object names[]) { 364 PdfArray array = new PdfArray(); 365 for (int k = 0; k < names.length; ++k) { 366 Object obj = names[k]; 367 if (obj instanceof String ) 368 array.add(new PdfString((String )obj)); 369 else if (obj instanceof PdfAnnotation) 370 array.add(((PdfAnnotation)obj).getIndirectReference()); 371 else 372 throw new RuntimeException ("The array must contain String or PdfAnnotation."); 373 } 374 return array; 375 } 376 377 383 public static PdfAction createHide(Object names[], boolean hide) { 384 return createHide(buildArray(names), hide); 385 } 386 387 394 public static PdfAction createSubmitForm(String file, Object names[], int flags) { 395 PdfAction action = new PdfAction(); 396 action.put(PdfName.S, PdfName.SUBMITFORM); 397 PdfDictionary dic = new PdfDictionary(); 398 dic.put(PdfName.F, new PdfString(file)); 399 dic.put(PdfName.FS, PdfName.URL); 400 action.put(PdfName.F, dic); 401 if (names != null) 402 action.put(PdfName.FIELDS, buildArray(names)); 403 action.put(PdfName.FLAGS, new PdfNumber(flags)); 404 return action; 405 } 406 407 413 public static PdfAction createResetForm(Object names[], int flags) { 414 PdfAction action = new PdfAction(); 415 action.put(PdfName.S, PdfName.RESETFORM); 416 if (names != null) 417 action.put(PdfName.FIELDS, buildArray(names)); 418 action.put(PdfName.FLAGS, new PdfNumber(flags)); 419 return action; 420 } 421 422 427 public static PdfAction createImportData(String file) { 428 PdfAction action = new PdfAction(); 429 action.put(PdfName.S, PdfName.IMPORTDATA); 430 action.put(PdfName.F, new PdfString(file)); 431 return action; 432 } 433 434 437 public void next(PdfAction na) { 438 PdfObject nextAction = get(PdfName.NEXT); 439 if (nextAction == null) 440 put(PdfName.NEXT, na); 441 else if (nextAction.isDictionary()) { 442 PdfArray array = new PdfArray(nextAction); 443 array.add(na); 444 put(PdfName.NEXT, array); 445 } 446 else { 447 ((PdfArray)nextAction).add(na); 448 } 449 } 450 451 457 public static PdfAction gotoLocalPage(int page, PdfDestination dest, PdfWriter writer) { 458 PdfIndirectReference ref = writer.getPageReference(page); 459 dest.addPage(ref); 460 PdfAction action = new PdfAction(); 461 action.put(PdfName.S, PdfName.GOTO); 462 action.put(PdfName.D, dest); 463 return action; 464 } 465 466 472 public static PdfAction gotoLocalPage(String dest, boolean isName) { 473 PdfAction action = new PdfAction(); 474 action.put(PdfName.S, PdfName.GOTO); 475 if (isName) 476 action.put(PdfName.D, new PdfName(dest)); 477 else 478 action.put(PdfName.D, new PdfString(dest, null)); 479 return action; 480 } 481 482 490 public static PdfAction gotoRemotePage(String filename, String dest, boolean isName, boolean newWindow) { 491 PdfAction action = new PdfAction(); 492 action.put(PdfName.F, new PdfString(filename)); 493 action.put(PdfName.S, PdfName.GOTOR); 494 if (isName) 495 action.put(PdfName.D, new PdfName(dest)); 496 else 497 action.put(PdfName.D, new PdfString(dest, null)); 498 if (newWindow) 499 action.put(PdfName.NEWWINDOW, PdfBoolean.PDFTRUE); 500 return action; 501 } 502 503 510 public static PdfAction gotoEmbedded(String filename, PdfTargetDictionary target, String dest, boolean isName, boolean newWindow) { 511 if (isName) 512 return gotoEmbedded(filename, target, new PdfName(dest), newWindow); 513 else 514 return gotoEmbedded(filename, target, new PdfString(dest, null), newWindow); 515 } 516 517 525 public static PdfAction gotoEmbedded(String filename, PdfTargetDictionary target, PdfObject dest, boolean newWindow) { 526 PdfAction action = new PdfAction(); 527 action.put(PdfName.S, PdfName.GOTOE); 528 action.put(PdfName.T, target); 529 action.put(PdfName.D, dest); 530 action.put(PdfName.NEWWINDOW, new PdfBoolean(newWindow)); 531 if (filename != null) { 532 action.put(PdfName.F, new PdfString(filename)); 533 } 534 return action; 535 } 536 537 559 public static PdfAction setOCGstate(ArrayList state, boolean preserveRB) { 560 PdfAction action = new PdfAction(); 561 action.put(PdfName.S, PdfName.SETOCGSTATE); 562 PdfArray a = new PdfArray(); 563 for (int k = 0; k < state.size(); ++k) { 564 Object o = state.get(k); 565 if (o == null) 566 continue; 567 if (o instanceof PdfIndirectReference) 568 a.add((PdfIndirectReference)o); 569 else if (o instanceof PdfLayer) 570 a.add(((PdfLayer)o).getRef()); 571 else if (o instanceof PdfName) 572 a.add((PdfName)o); 573 else if (o instanceof String ) { 574 PdfName name = null; 575 String s = (String )o; 576 if (s.equalsIgnoreCase("on")) 577 name = PdfName.ON; 578 else if (s.equalsIgnoreCase("off")) 579 name = PdfName.OFF; 580 else if (s.equalsIgnoreCase("toggle")) 581 name = PdfName.TOGGLE; 582 else 583 throw new IllegalArgumentException ("A string '" + s + " was passed in state. Only 'ON', 'OFF' and 'Toggle' are allowed."); 584 a.add(name); 585 } 586 else 587 throw new IllegalArgumentException ("Invalid type was passed in state: " + o.getClass().getName()); 588 } 589 action.put(PdfName.STATE, a); 590 if (!preserveRB) 591 action.put(PdfName.PRESERVERB, PdfBoolean.PDFFALSE); 592 return action; 593 } 594 } 595 | Popular Tags |