Exemple #1
0
 /** Entry point to encrypt a PDF document. The encryption parameters are the same as in
  * <code>PdfWriter</code>. The userPassword and the
  *  ownerPassword can be null or have zero length. In this case the ownerPassword
  *  is replaced by a random string. The open permissions for the document can be
  *  AllowPrinting, AllowModifyContents, AllowCopy, AllowModifyAnnotations,
  *  AllowFillIn, AllowScreenReaders, AllowAssembly and AllowDegradedPrinting.
  *  The permissions can be combined by ORing them.
  * @param reader the read PDF
  * @param os the output destination
  * @param userPassword the user password. Can be null or empty
  * @param ownerPassword the owner password. Can be null or empty
  * @param permissions the user permissions
  * @param strength128Bits <code>true</code> for 128 bit key length, <code>false</code> for 40 bit key length
  * @throws DocumentException on error
  * @throws IOException on error */
 public static void Encrypt(PdfReader reader, Stream os, byte[] userPassword, byte[] ownerPassword, int permissions, bool strength128Bits)
 {
     PdfStamper stamper = new PdfStamper(reader, os);
     stamper.SetEncryption(userPassword, ownerPassword, permissions, strength128Bits);
     stamper.Close();
 }
Exemple #2
0
 /** Entry point to encrypt a PDF document. The encryption parameters are the same as in
  * <code>PdfWriter</code>. The userPassword and the
  *  ownerPassword can be null or have zero length. In this case the ownerPassword
  *  is replaced by a random string. The open permissions for the document can be
  *  AllowPrinting, AllowModifyContents, AllowCopy, AllowModifyAnnotations,
  *  AllowFillIn, AllowScreenReaders, AllowAssembly and AllowDegradedPrinting.
  *  The permissions can be combined by ORing them.
  * @param reader the read PDF
  * @param os the output destination
  * @param type the type of encryption. It can be one of STANDARD_ENCRYPTION_40, STANDARD_ENCRYPTION_128 or ENCRYPTION_AES128.
  * Optionally DO_NOT_ENCRYPT_METADATA can be ored to output the metadata in cleartext
  * @param userPassword the user password. Can be null or empty
  * @param ownerPassword the owner password. Can be null or empty
  * @param permissions the user permissions
  * values delete the key in the original info dictionary
  * @throws DocumentException on error
  * @throws IOException on error
  */
 public static void Encrypt(PdfReader reader, Stream os, int type, String userPassword, String ownerPassword, int permissions)
 {
     PdfStamper stamper = new PdfStamper(reader, os);
     stamper.SetEncryption(type, userPassword, ownerPassword, permissions);
     stamper.Close();
 }
 internal void SetStamper(PdfStamper stamper)
 {
     this.stamper = stamper;
 }
Exemple #4
0
 /** Entry point to encrypt a PDF document. The encryption parameters are the same as in
  * <code>PdfWriter</code>. The userPassword and the
  *  ownerPassword can be null or have zero length. In this case the ownerPassword
  *  is replaced by a random string. The open permissions for the document can be
  *  AllowPrinting, AllowModifyContents, AllowCopy, AllowModifyAnnotations,
  *  AllowFillIn, AllowScreenReaders, AllowAssembly and AllowDegradedPrinting.
  *  The permissions can be combined by ORing them.
  * @param reader the read PDF
  * @param os the output destination
  * @param strength <code>true</code> for 128 bit key length, <code>false</code> for 40 bit key length
  * @param userPassword the user password. Can be null or empty
  * @param ownerPassword the owner password. Can be null or empty
  * @param permissions the user permissions
  * @param newInfo an optional <CODE>String</CODE> map to add or change
  * the info dictionary. Entries with <CODE>null</CODE>
  * values delete the key in the original info dictionary
  * @throws DocumentException on error
  * @throws IOException on error
  */
 public static void Encrypt(PdfReader reader, Stream os, bool strength, String userPassword, String ownerPassword, int permissions, Hashtable newInfo)
 {
     PdfStamper stamper = new PdfStamper(reader, os);
     stamper.SetEncryption(strength, userPassword, ownerPassword, permissions);
     stamper.MoreInfo = newInfo;
     stamper.Close();
 }
Exemple #5
0
 /**
 * Applies a digital signature to a document, possibly as a new revision, making
 * possible multiple signatures. The returned PdfStamper
 * can be used normally as the signature is only applied when closing.
 * <p>
 * A possible use for adding a signature without invalidating an existing one is:
 * <p>
 * <pre>
 * KeyStore ks = KeyStore.getInstance("pkcs12");
 * ks.load(new FileInputStream("my_private_key.pfx"), "my_password".toCharArray());
 * String alias = (String)ks.aliases().nextElement();
 * PrivateKey key = (PrivateKey)ks.getKey(alias, "my_password".toCharArray());
 * Certificate[] chain = ks.getCertificateChain(alias);
 * PdfReader reader = new PdfReader("original.pdf");
 * FileOutputStream fout = new FileOutputStream("signed.pdf");
 * PdfStamper stp = PdfStamper.createSignature(reader, fout, '\0', new
 * File("/temp"), true);
 * PdfSignatureAppearance sap = stp.getSignatureAppearance();
 * sap.setCrypto(key, chain, null, PdfSignatureAppearance.WINCER_SIGNED);
 * sap.setReason("I'm the author");
 * sap.setLocation("Lisbon");
 * // comment next line to have an invisible signature
 * sap.setVisibleSignature(new Rectangle(100, 100, 200, 200), 1, null);
 * stp.close();
 * </pre>
 * @param reader the original document
 * @param os the output stream or <CODE>null</CODE> to keep the document in the temporary file
 * @param pdfVersion the new pdf version or '\0' to keep the same version as the original
 * document
 * @param tempFile location of the temporary file. If it's a directory a temporary file will be created there.
 *     If it's a file it will be used directly. The file will be deleted on exit unless <CODE>os</CODE> is null.
 *     In that case the document can be retrieved directly from the temporary file. If it's <CODE>null</CODE>
 *     no temporary file will be created and memory will be used
 * @param append if <CODE>true</CODE> the signature and all the other content will be added as a
 * new revision thus not invalidating existing signatures
 * @return a <CODE>PdfStamper</CODE>
 * @throws DocumentException on error
 * @throws IOException on error
 */
 public static PdfStamper CreateSignature(PdfReader reader, Stream os, char pdfVersion, string tempFile, bool append)
 {
     PdfStamper stp;
     if (tempFile == null) {
         ByteBuffer bout = new ByteBuffer();
         stp = new PdfStamper(reader, bout, pdfVersion, append);
         stp.sigApp = new PdfSignatureAppearance(stp.stamper);
         stp.sigApp.Sigout = bout;
     }
     else {
         if (Directory.Exists(tempFile))
             tempFile = Path.GetTempFileName();
         FileStream fout = new FileStream(tempFile, FileMode.Create, FileAccess.Write);
         stp = new PdfStamper(reader, fout, pdfVersion, append);
         stp.sigApp = new PdfSignatureAppearance(stp.stamper);
         stp.sigApp.SetTempFile(tempFile);
     }
     stp.sigApp.Originalout = os;
     stp.sigApp.SetStamper(stp);
     stp.hasSignature = true;
     PdfDictionary catalog = reader.Catalog;
     PdfDictionary acroForm = (PdfDictionary)PdfReader.GetPdfObject(catalog.Get(PdfName.ACROFORM), catalog);
     if (acroForm != null) {
         acroForm.Remove(PdfName.NEEDAPPEARANCES);
         stp.stamper.MarkUsed(acroForm);
     }
     return stp;
 }