com.aspose.pdf.facades

Class PdfFileSecurity

  • All Implemented Interfaces:
    com.aspose.ms.System.IDisposable, IFacade, ISaveableFacade


    public final class PdfFileSecurity
    extends SaveableFacade
    implements com.aspose.ms.System.IDisposable

    Represents encrypting or decrypting a Pdf file with owner or user password, changing the security setting and password.

    • Constructor Detail

      • PdfFileSecurity

        @Deprecated
        public PdfFileSecurity(InputStream inputStream,
                                  OutputStream outputStream)
        Deprecated. 

        Initialize the object of PdfFileSecurity with input and output stream.

        Obsolete("Use constructor without destination.")

        Parameters:
        inputStream - Input Pdf Stream.
        outputStream - Output Pdf Stream.
      • PdfFileSecurity

        @Deprecated
        public PdfFileSecurity(String inputFile,
                                  String outputFile)
        Deprecated. 

        Initializes the object of PdfFileSecurity with input and output file.

        Obsolete("Use constructor without destination.")

        Parameters:
        inputFile - Source input Pdf file.
        outputFile - Output Pdf file.
      • PdfFileSecurity

        public PdfFileSecurity()

        Initialize the object of PdfFileSecurity.

      • PdfFileSecurity

        public PdfFileSecurity(IDocument document)

        Initializes new PdfFileSecurity object on base of the document.

        Parameters:
        document - Pdf document.
      • PdfFileSecurity

        @Deprecated
        public PdfFileSecurity(IDocument document,
                                  String outputFile)
        Deprecated. 

        Initializes new PdfFileSecurity object on base of the document.

        Obsolete("Use constructor without destination.")

        Parameters:
        document - Pdf document.
        outputFile - Output Pdf file.
      • PdfFileSecurity

        @Deprecated
        public PdfFileSecurity(IDocument document,
                                  OutputStream outputStream)
        Deprecated. 

        Initializes new PdfFileSecurity object on base of the document.

        Obsolete("Use constructor without destination.")

        Parameters:
        document - Pdf document.
        outputStream - Output Pdf Stream.
    • Method Detail

      • setInputFile

        @Deprecated
        public void setInputFile(String value)
        Deprecated. 

        Sets the input file.

        Obsolete("Use bindPdf(inputStream) method for facade initialization.")
        Parameters:
        value - String object
      • setInputStream

        @Deprecated
        public void setInputStream(InputStream value)
        Deprecated. 

        Sets the input stream.

        Obsolete("Use bindPdf(inputStream) method for facade initialization.")
        Parameters:
        value - InputStream object
      • setOutputFile

        @Deprecated
        public void setOutputFile(String value)
        Deprecated. 

        Sets the output file.

        Obsolete("Use save(outputStream) method for getting facade results.")
        Parameters:
        value - String object
      • setOutputStream

        @Deprecated
        public void setOutputStream(OutputStream value)
        Deprecated. 

        Sets the output stream.

        Obsolete("Use save(outputStream) method for getting facade results.")
        Parameters:
        value - OutputStream object
      • encryptFile

        public boolean encryptFile(String userPassword,
                          String ownerPassword,
                          DocumentPrivilege privilege,
                          int keySize)

        Encrypts Pdf file with userpassword and ownerpassword and sets the document's privileges to access. The user password and the owner password can be null or empty. The owner password will be replaced with a random string if the input owner password is null or empty.


         String inFile = "..\\AsposePdfKitExample\\example1.pdf"; // The TestPath may be
                                                                                                                                // re-assigned.
         String outFile = "..\\AsposePdfKitExample\\Kit0401.pdf"; // The TestPath may be
                                                                                                                                // re-assigned.
         PdfFileSecurity fileSecurity = new PdfFileSecurity(inFile, outFile);
         fileSecurity.EncryptFile("userpass", "ownerpass", DocumentPrivilege.Print,
                        KeySize.x256);
         
        Parameters:
        userPassword - User password.
        ownerPassword - Owner password.
        privilege - Set privilege.
        keySize - KeySize.x40 for 40 bits encryption, KeySize.x128 for 128 bits encryption and KeySize.x256 for 256 bits encryption.
        Returns:
        True for success, or false.
      • encryptFile

        public boolean encryptFile(String userPassword,
                          String ownerPassword,
                          DocumentPrivilege privilege,
                          int keySize,
                          int cipher)

        Encrypts Pdf file with userpassword and ownerpassword and sets the document's privileges to access. The user password and the owner password can be null or empty. The owner password will be replaced with a random string if the input owner password is null or empty. There are 6 possible combinations of KeySize and Algorithm values. However (KeySize.x40, Algorithm.AES) and (KeySize.x256, Algorithm.RC4) are invalid and corresponding exception will be raised if kit encounters this combination.


         String inFile = "..\\AsposePdfKitExample\\example1.pdf"; // The TestPath may be
                                                                                                                                // re-assigned.
         String outFile = "..\\AsposePdfKitExample\\Kit0401.pdf"; // The TestPath may be
                                                                                                                                // re-assigned.
         PdfFileSecurity fileSecurity = new PdfFileSecurity(inFile, outFile);
         fileSecurity.encryptFile("userpass", "ownerpass", DocumentPrivilege.Print,
                        KeySize.x256, Algorithm.AES);
         
        Parameters:
        userPassword - User password.
        ownerPassword - Owner password.
        privilege - Set privilege.
        keySize - KeySize.x40 for 40 bits encryption, KeySize.x128 for 128 bits encryption and KeySize.x256 for 256 bits encryption.
        cipher - Algorithm.AES to encrypt using AES algorithm or Algorithm.RC4 for RC4 encryption.
        Returns:
        True for success, or false.
      • decryptFile

        public boolean decryptFile(String ownerPassword)

        Decrypts an encrypted Pdf document by owner password. If the document hasn't owner password, it is allow to use user password.


         string inFile = "..\\AsposePdfKitExample\\Kit0401.pdf"; // The TestPath may be
                                                                                                                        // re-assigned.
         string outFile = "..\\AsposePdfKitExample\\Kit0404.pdf"; // The TestPath may be
                                                                                                                                // re-assigned.
         PdfFileSecurity fileSecurity = new PdfFileSecurity(inFile, outFile);
         fileSecurity.DecryptFile("ownerpass");
         
        Parameters:
        ownerPassword - Owner password.
        Returns:
        True for success,or false.
      • setPrivilege

        public boolean setPrivilege(DocumentPrivilege privilege)

        Sets Pdf file security with empty user/owner passwords. The owner password will be added by a random string.


         string inFile = "..\\AsposePdfKitExample\\example1.pdf"; // The TestPath may be re-assigned.
         string outFile = "..\\AsposePdfKitExample\\Kit0405.pdf"; // The TestPath may be re-assigned.
         PdfFileSecurity fileSecurity = new PdfFileSecurity(inFile, outFile);
         fileSecurity.setPrivilege(DocumentPrivilege.Print);
         
        Parameters:
        privilege - Set privilege.
        Returns:
        True for success, or false.
      • setPrivilege

        public boolean setPrivilege(String userPassword,
                           String ownerPassword,
                           DocumentPrivilege privilege)

        Sets Pdf file security with original password.


         string inFile = "..\\AsposePdfKitExample\\example1.pdf"; // The TestPath may be re-assigned.
         string outFile = "..\\AsposePdfKitExample\\Kit0405.pdf"; // The TestPath may be re-assigned.
         PdfFileSecurity fileSecurity = new PdfFileSecurity(inFile, outFile);
         fileSecurity.setPrivilege(userPassword, ownerPassword, DocumentPrivilege.getPrint());
         
        Parameters:
        userPassword - Original user password.
        ownerPassword - Original owner password.
        privilege - Set privilege.
        Returns:
        True for success, or false.
      • changePassword

        public boolean changePassword(String ownerPassword,
                             String newUserPassword,
                             String newOwnerPassword)

        Changes the user password and owner password by owner password, keeps the original security settings. The new user password and the new owner password can be null or empty. The owner password will be replaced with a random string if the new owner password is null or empty.


         string inFile = "..\\AsposePdfKitExample\\Kit0406.pdf"; // The TestPath may be
                                                                                                                        // re-assigned.
         string outFile = "..\\AsposePdfKitExample\\Kit0407.pdf"; // The TestPath may be
                                                                                                                                // re-assigned.
         PdfFileSecurity fileSecurity = new PdfFileSecurity(inFile, outFile);
         fileSecurity.ChangePassword("owner", "newuser", "newowner");
         
        Parameters:
        ownerPassword - Original Owner password.
        newUserPassword - New User password.
        newOwnerPassword - New Owner password.
        Returns:
        True for success,or false.
      • changePassword

        public boolean changePassword(String ownerPassword,
                             String newUserPassword,
                             String newOwnerPassword,
                             DocumentPrivilege privilege,
                             int keySize)

        Changes the user password and password by owner password, allows to reset Pdf documnent security. The new user password and the new owner password can be null or empty. The owner password will be replaced with a random string if the new owner password is null or empty.


         string inFile = "..\\AsposePdfKitExample\\Kit0401.pdf"; // The TestPath may be
                                                                                                                        // re-assigned.
         string outFile = "..\\AsposePdfKitExample\\Kit0407.pdf"; // The TestPath may be
                                                                                                                                // re-assigned.
         PdfFileSecurity fileSecurity = new PdfFileSecurity(inFile, outFile);
         fileSecurity.ChangePassword("owner", "newuser", "newowner",
                        DocumentPrivilege.Print, KeySize.x256);
         
        Parameters:
        ownerPassword - Original owner password.
        newUserPassword - New User password.
        newOwnerPassword - New Owner password.
        privilege - Reset security.
        keySize - KeySize.x40 for 40 bits encryption, KeySize.x128 for 128 bits encryption and KeySize.x256 for 256 bits encryption.
        Returns:
        True for success, or false.
      • changePassword

        public boolean changePassword(String ownerPassword,
                             String newUserPassword,
                             String newOwnerPassword,
                             DocumentPrivilege privilege,
                             int keySize,
                             int cipher)

        Changes the user password and password by owner password, allows to reset Pdf documnent security. The new user password and the new owner password can be null or empty. The owner password will be replaced with a random string if the new owner password is null or empty. There are 6 possible combinations of KeySize and Algorithm values. However (KeySize.x40, Algorithm.AES) and (KeySize.x256, Algorithm.RC4) are invalid and corresponding exception will be raised if kit encounters this combination.


         
         string inFile = "..\\AsposePdfKitExample\\Kit0401.pdf"; // The TestPath may be
                                                                                                                        // re-assigned.
         string outFile = "..\\AsposePdfKitExample\\Kit0407.pdf"; // The TestPath may be
                                                                                                                                // re-assigned.
         PdfFileSecurity fileSecurity = new PdfFileSecurity(inFile, outFile);
         fileSecurity.ChangePassword("owner", "newuser", "newowner",
                        DocumentPrivilege.Print, KeySize.x256, Algorithm.AES);
         
        Parameters:
        ownerPassword - Original owner password.
        newUserPassword - New User password.
        newOwnerPassword - New Owner password.
        privilege - Reset security.
        keySize - KeySize.x40 for 40 bits encryption, KeySize.x128 for 128 bits encryption and KeySize.x256 for 256 bits encryption.
        cipher - Algorithm.AES to encrypt using AES algorithm or Algorithm.RC4 for RC4 encryption.
        Returns:
        True for success, or false.
      • bindPdf

        public void bindPdf(String srcFile)

        Initializes the facade.

        Specified by:
        bindPdf in interface IFacade
        Overrides:
        bindPdf in class Facade
        Parameters:
        srcFile - The PDF file.
      • bindPdf

        public void bindPdf(InputStream srcStream)

        Initializes the facade.

        Specified by:
        bindPdf in interface IFacade
        Overrides:
        bindPdf in class Facade
        Parameters:
        srcStream - The stream of PDF file.
      • close

        public void close()

        Closes the facade.

        Specified by:
        close in interface IFacade
        Overrides:
        close in class Facade
      • dispose

        public void dispose()

        Closes the facade.

        Specified by:
        dispose in interface com.aspose.ms.System.IDisposable
        Overrides:
        dispose in class Facade