1 47 package com.lowagie.text.pdf; 48 49 import java.io.File ; 50 import java.io.FileOutputStream ; 51 import java.io.IOException ; 52 import java.io.InputStream ; 53 import java.io.OutputStream ; 54 import java.security.SignatureException ; 55 import java.util.HashMap ; 56 import java.util.List ; 57 58 import com.lowagie.text.DocWriter; 59 import com.lowagie.text.DocumentException; 60 import com.lowagie.text.ExceptionConverter; 61 import com.lowagie.text.Image; 62 import com.lowagie.text.Rectangle; 63 import com.lowagie.text.pdf.collection.PdfCollection; 64 import com.lowagie.text.pdf.interfaces.PdfEncryptionSettings; 65 import com.lowagie.text.pdf.interfaces.PdfViewerPreferences; 66 import java.security.cert.Certificate ; 67 68 77 public class PdfStamper 78 implements PdfViewerPreferences, PdfEncryptionSettings { 79 82 protected PdfStamperImp stamper; 83 private HashMap moreInfo; 84 private boolean hasSignature; 85 private PdfSignatureAppearance sigApp; 86 87 94 public PdfStamper(PdfReader reader, OutputStream os) throws DocumentException, IOException { 95 stamper = new PdfStamperImp(reader, os, '\0', false); 96 } 97 98 108 public PdfStamper(PdfReader reader, OutputStream os, char pdfVersion) throws DocumentException, IOException { 109 stamper = new PdfStamperImp(reader, os, pdfVersion, false); 110 } 111 112 124 public PdfStamper(PdfReader reader, OutputStream os, char pdfVersion, boolean append) throws DocumentException, IOException { 125 stamper = new PdfStamperImp(reader, os, pdfVersion, append); 126 } 127 128 133 public HashMap getMoreInfo() { 134 return this.moreInfo; 135 } 136 137 143 public void setMoreInfo(HashMap moreInfo) { 144 this.moreInfo = moreInfo; 145 } 146 147 154 public void insertPage(int pageNumber, Rectangle mediabox) { 155 stamper.insertPage(pageNumber, mediabox); 156 } 157 158 162 public PdfSignatureAppearance getSignatureAppearance() { 163 return sigApp; 164 } 165 166 175 public void close() throws DocumentException, IOException { 176 if (!hasSignature) { 177 stamper.close(moreInfo); 178 return; 179 } 180 sigApp.preClose(); 181 PdfSigGenericPKCS sig = sigApp.getSigStandard(); 182 PdfLiteral lit = (PdfLiteral)sig.get(PdfName.CONTENTS); 183 int totalBuf = (lit.getPosLength() - 2) / 2; 184 byte buf[] = new byte[8192]; 185 int n; 186 InputStream inp = sigApp.getRangeStream(); 187 try { 188 while ((n = inp.read(buf)) > 0) { 189 sig.getSigner().update(buf, 0, n); 190 } 191 } 192 catch (SignatureException se) { 193 throw new ExceptionConverter(se); 194 } 195 buf = new byte[totalBuf]; 196 byte[] bsig = sig.getSignerContents(); 197 System.arraycopy(bsig, 0, buf, 0, bsig.length); 198 PdfString str = new PdfString(buf); 199 str.setHexWriting(true); 200 PdfDictionary dic = new PdfDictionary(); 201 dic.put(PdfName.CONTENTS, str); 202 sigApp.close(dic); 203 stamper.reader.close(); 204 } 205 206 212 public PdfContentByte getUnderContent(int pageNum) { 213 return stamper.getUnderContent(pageNum); 214 } 215 216 222 public PdfContentByte getOverContent(int pageNum) { 223 return stamper.getOverContent(pageNum); 224 } 225 226 230 public boolean isRotateContents() { 231 return stamper.isRotateContents(); 232 } 233 234 239 public void setRotateContents(boolean rotateContents) { 240 stamper.setRotateContents(rotateContents); 241 } 242 243 255 public void setEncryption(byte userPassword[], byte ownerPassword[], int permissions, boolean strength128Bits) throws DocumentException { 256 if (stamper.isAppend()) 257 throw new DocumentException("Append mode does not support changing the encryption status."); 258 if (stamper.isContentWritten()) 259 throw new DocumentException("Content was already written to the output."); 260 stamper.setEncryption(userPassword, ownerPassword, permissions, strength128Bits ? PdfWriter.STANDARD_ENCRYPTION_128 : PdfWriter.STANDARD_ENCRYPTION_40); 261 } 262 263 276 public void setEncryption(byte userPassword[], byte ownerPassword[], int permissions, int encryptionType) throws DocumentException { 277 if (stamper.isAppend()) 278 throw new DocumentException("Append mode does not support changing the encryption status."); 279 if (stamper.isContentWritten()) 280 throw new DocumentException("Content was already written to the output."); 281 stamper.setEncryption(userPassword, ownerPassword, permissions, encryptionType); 282 } 283 284 297 public void setEncryption(boolean strength, String userPassword, String ownerPassword, int permissions) throws DocumentException { 298 setEncryption(DocWriter.getISOBytes(userPassword), DocWriter.getISOBytes(ownerPassword), permissions, strength); 299 } 300 301 315 public void setEncryption(int encryptionType, String userPassword, String ownerPassword, int permissions) throws DocumentException { 316 setEncryption(DocWriter.getISOBytes(userPassword), DocWriter.getISOBytes(ownerPassword), permissions, encryptionType); 317 } 318 319 332 public void setEncryption(Certificate [] certs, int[] permissions, int encryptionType) throws DocumentException { 333 if (stamper.isAppend()) 334 throw new DocumentException("Append mode does not support changing the encryption status."); 335 if (stamper.isContentWritten()) 336 throw new DocumentException("Content was already written to the output."); 337 stamper.setEncryption(certs, permissions, encryptionType); 338 } 339 340 346 public PdfImportedPage getImportedPage(PdfReader reader, int pageNumber) { 347 return stamper.getImportedPage(reader, pageNumber); 348 } 349 350 353 public PdfWriter getWriter() { 354 return stamper; 355 } 356 357 360 public PdfReader getReader() { 361 return stamper.reader; 362 } 363 364 368 public AcroFields getAcroFields() { 369 return stamper.getAcroFields(); 370 } 371 372 377 public void setFormFlattening(boolean flat) { 378 stamper.setFormFlattening(flat); 379 } 380 381 385 public void setFreeTextFlattening(boolean flat) { 386 stamper.setFreeTextFlattening(flat); 387 } 388 389 395 public void addAnnotation(PdfAnnotation annot, int page) { 396 stamper.addAnnotation(annot, page); 397 } 398 399 404 public void addComments(FdfReader fdf) throws IOException { 405 stamper.addComments(fdf); 406 } 407 408 413 public void setOutlines(List outlines) { 414 stamper.setOutlines(outlines); 415 } 416 417 424 public void setThumbnail(Image image, int page) throws PdfException, DocumentException { 425 stamper.setThumbnail(image, page); 426 } 427 428 438 public boolean partialFormFlattening(String name) { 439 return stamper.partialFormFlattening(name); 440 } 441 442 446 public void addJavaScript(String js) { 447 stamper.addJavaScript(js, !PdfEncodings.isPdfDocEncoding(js)); 448 } 449 450 459 public void addFileAttachment(String description, byte fileStore[], String file, String fileDisplay) throws IOException { 460 addFileAttachment(description, PdfFileSpecification.fileEmbedded(stamper, file, fileDisplay, fileStore)); 461 } 462 463 467 public void addFileAttachment(String description, PdfFileSpecification fs) throws IOException { 468 stamper.addFileAttachment(description, fs); 469 } 470 471 484 public void makePackage( PdfName initialView ) { 485 PdfCollection collection = new PdfCollection(0); 486 collection.put(PdfName.VIEW, initialView); 487 stamper.makePackage( collection ); 488 } 489 490 494 public void makePackage(PdfCollection collection) { 495 stamper.makePackage(collection); 496 } 497 498 503 public void setViewerPreferences(int preferences) { 504 stamper.setViewerPreferences(preferences); 505 } 506 507 512 513 public void addViewerPreference(PdfName key, PdfObject value) { 514 stamper.addViewerPreference(key, value); 515 } 516 517 522 public void setXmpMetadata(byte[] xmp) { 523 stamper.setXmpMetadata(xmp); 524 } 525 526 530 public boolean isFullCompression() { 531 return stamper.isFullCompression(); 532 } 533 534 538 public void setFullCompression() { 539 if (stamper.isAppend()) 540 return; 541 stamper.setFullCompression(); 542 } 543 544 552 public void setPageAction(PdfName actionType, PdfAction action, int page) throws PdfException { 553 stamper.setPageAction(actionType, action, page); 554 } 555 556 561 public void setDuration(int seconds, int page) { 562 stamper.setDuration(seconds, page); 563 } 564 565 570 public void setTransition(PdfTransition transition, int page) { 571 stamper.setTransition(transition, page); 572 } 573 574 613 public static PdfStamper createSignature(PdfReader reader, OutputStream os, char pdfVersion, File tempFile, boolean append) throws DocumentException, IOException { 614 PdfStamper stp; 615 if (tempFile == null) { 616 ByteBuffer bout = new ByteBuffer(); 617 stp = new PdfStamper(reader, bout, pdfVersion, append); 618 stp.sigApp = new PdfSignatureAppearance(stp.stamper); 619 stp.sigApp.setSigout(bout); 620 } 621 else { 622 if (tempFile.isDirectory()) 623 tempFile = File.createTempFile("pdf", null, tempFile); 624 FileOutputStream fout = new FileOutputStream (tempFile); 625 stp = new PdfStamper(reader, fout, pdfVersion, append); 626 stp.sigApp = new PdfSignatureAppearance(stp.stamper); 627 stp.sigApp.setTempFile(tempFile); 628 } 629 stp.sigApp.setOriginalout(os); 630 stp.sigApp.setStamper(stp); 631 stp.hasSignature = true; 632 PdfDictionary catalog = reader.getCatalog(); 633 PdfDictionary acroForm = (PdfDictionary)PdfReader.getPdfObject(catalog.get(PdfName.ACROFORM), catalog); 634 if (acroForm != null) { 635 acroForm.remove(PdfName.NEEDAPPEARANCES); 636 stp.stamper.markUsed(acroForm); 637 } 638 return stp; 639 } 640 641 674 public static PdfStamper createSignature(PdfReader reader, OutputStream os, char pdfVersion) throws DocumentException, IOException { 675 return createSignature(reader, os, pdfVersion, null, false); 676 } 677 678 713 public static PdfStamper createSignature(PdfReader reader, OutputStream os, char pdfVersion, File tempFile) throws DocumentException, IOException 714 { 715 return createSignature(reader, os, pdfVersion, tempFile, false); 716 } 717 } | Popular Tags |