PdfFileSecurity

Inheritance: java.lang.Object, com.aspose.pdf.facades.IVentureLicenseTarget, com.aspose.pdf.facades.Facade, com.aspose.pdf.facades.SaveableFacade

All Implemented Interfaces: com.aspose.ms.System.IDisposable

public final class PdfFileSecurity extends SaveableFacade implements System.IDisposable

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

Constructors

ConstructorDescription
PdfFileSecurity(InputStream inputStream, OutputStream outputStream)Initialize the object of PdfFileSecurity with input and output stream.
PdfFileSecurity(String inputFile, String outputFile)Initializes the object of PdfFileSecurity with input and output file.
PdfFileSecurity()Initialize the object of PdfFileSecurity.
PdfFileSecurity(IDocument document)Initializes new PdfFileSecurity object on base of the document .
PdfFileSecurity(IDocument document, String outputFile)Initializes new PdfFileSecurity object on base of the document .
PdfFileSecurity(IDocument document, OutputStream outputStream)Initializes new PdfFileSecurity object on base of the document .

Methods

MethodDescription
setInputFile(String value)Sets the input file.
setInputStream(InputStream value)Sets the input stream.
setOutputFile(String value)Sets the output file.
setOutputStream(OutputStream value)Sets the output stream.
getAllowExceptions()If this value set to true, exception will be thrown on operation failure.
setAllowExceptions(boolean value)If this value set to true, exception will be thrown on operation failure.
getLastException()Returns exception which was thrown by last operation.
encryptFile(String userPassword, String ownerPassword, DocumentPrivilege privilege, KeySize keySize)Encrypts Pdf file with userpassword and ownerpassword and sets the document’s privileges to access.
tryEncryptFile(String userPassword, String ownerPassword, DocumentPrivilege privilege, KeySize keySize)Encrypts Pdf file with userpassword and ownerpassword and sets the document’s privileges to access.
encryptFile(String userPassword, String ownerPassword, DocumentPrivilege privilege, KeySize keySize, Algorithm cipher)Encrypts Pdf file with userpassword and ownerpassword and sets the document’s privileges to access.
decryptFile(String ownerPassword)Decrypts an encrypted Pdf document by owner password.
tryDecryptFile(String ownerPassword)Decrypts an encrypted Pdf document by owner password.
setPrivilege(DocumentPrivilege privilege)Sets Pdf file security with empty user/owner passwords.
setPrivilege(String userPassword, String ownerPassword, DocumentPrivilege privilege)Sets Pdf file security with original password.
trySetPrivilege(String userPassword, String ownerPassword, DocumentPrivilege privilege)Sets Pdf file security with original password.
changePassword(String ownerPassword, String newUserPassword, String newOwnerPassword)Changes the user password and owner password by owner password, keeps the original security settings.
tryChangePassword(String ownerPassword, String newUserPassword, String newOwnerPassword)Changes the user password and owner password by owner password, keeps the original security settings.
changePassword(String ownerPassword, String newUserPassword, String newOwnerPassword, DocumentPrivilege privilege, KeySize keySize)Changes the user password and password by owner password, allows to reset Pdf documnent security.
tryChangePassword(String ownerPassword, String newUserPassword, String newOwnerPassword, DocumentPrivilege privilege, KeySize keySize)Changes the user password and password by owner password, allows to reset Pdf documnent security.
changePassword(String ownerPassword, String newUserPassword, String newOwnerPassword, DocumentPrivilege privilege, KeySize keySize, Algorithm cipher)Changes the user password and password by owner password, allows to reset Pdf documnent security.
tryChangePassword(String ownerPassword, String newUserPassword, String newOwnerPassword, DocumentPrivilege privilege, KeySize keySize, Algorithm cipher)Changes the user password and password by owner password, allows to reset Pdf document security.
bindPdf(String srcFile)Initializes the facade.
bindPdf(InputStream srcStream)Initializes the facade.
close()Closes the facade.
dispose()Closes the facade.

PdfFileSecurity(InputStream inputStream, OutputStream outputStream)

public PdfFileSecurity(InputStream inputStream, OutputStream outputStream)

Initialize the object of PdfFileSecurity with input and output stream.

Obsolete(“Use constructor without destination.”)

Parameters:

ParameterTypeDescription
inputStreamjava.io.InputStreamInput Pdf Stream.
outputStreamjava.io.OutputStreamOutput Pdf Stream.

PdfFileSecurity(String inputFile, String outputFile)

public PdfFileSecurity(String inputFile, String outputFile)

Initializes the object of PdfFileSecurity with input and output file.

Obsolete(“Use constructor without destination.”)

Parameters:

ParameterTypeDescription
inputFilejava.lang.StringSource input Pdf file.
outputFilejava.lang.StringOutput Pdf file.

PdfFileSecurity()

public PdfFileSecurity()

Initialize the object of PdfFileSecurity.

PdfFileSecurity(IDocument document)

public PdfFileSecurity(IDocument document)

Initializes new PdfFileSecurity object on base of the document .

Parameters:

ParameterTypeDescription
documentIDocumentPdf document.

PdfFileSecurity(IDocument document, String outputFile)

public PdfFileSecurity(IDocument document, String outputFile)

Initializes new PdfFileSecurity object on base of the document .

Obsolete(“Use constructor without destination.”)

Parameters:

ParameterTypeDescription
documentIDocumentPdf document.
outputFilejava.lang.StringOutput Pdf file.

PdfFileSecurity(IDocument document, OutputStream outputStream)

public PdfFileSecurity(IDocument document, OutputStream outputStream)

Initializes new PdfFileSecurity object on base of the document .

Obsolete(“Use constructor without destination.”)

Parameters:

ParameterTypeDescription
documentIDocumentPdf document.
outputStreamjava.io.OutputStreamOutput Pdf Stream.

setInputFile(String value)

public void setInputFile(String value)

Sets the input file.

Obsolete(“Use bindPdf(inputStream) method for facade initialization.”)

Parameters:

ParameterTypeDescription
valuejava.lang.StringString object

setInputStream(InputStream value)

public void setInputStream(InputStream value)

Sets the input stream.

Obsolete(“Use bindPdf(inputStream) method for facade initialization.”)

Parameters:

ParameterTypeDescription
valuejava.io.InputStreamInputStream object

setOutputFile(String value)

public void setOutputFile(String value)

Sets the output file.

Obsolete(“Use save(outputStream) method for getting facade results.”)

Parameters:

ParameterTypeDescription
valuejava.lang.StringString object

setOutputStream(OutputStream value)

public void setOutputStream(OutputStream value)

Sets the output stream.

Obsolete(“Use save(outputStream) method for getting facade results.”)

Parameters:

ParameterTypeDescription
valuejava.io.OutputStreamOutputStream object

getAllowExceptions()

public final boolean getAllowExceptions()

If this value set to true, exception will be thrown on operation failure. Else, method returns false on failure and last exception can be checked with LastException property.

Returns: boolean - boolean value

setAllowExceptions(boolean value)

public final void setAllowExceptions(boolean value)

If this value set to true, exception will be thrown on operation failure. Else, method returns false on failure and last exception can be checked with LastException property.

Parameters:

ParameterTypeDescription
valuebooleanboolean value

getLastException()

public final RuntimeException getLastException()

Returns exception which was thrown by last operation.

Returns: java.lang.RuntimeException - java.lang.RuntimeException

encryptFile(String userPassword, String ownerPassword, DocumentPrivilege privilege, KeySize keySize)

public boolean encryptFile(String userPassword, String ownerPassword, DocumentPrivilege privilege, KeySize 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. Throws exception if process failed.


String inFile = "input.pdf"; // The TestPath may be
 															// re-assigned.
 String outFile = "output.pdf"; // The TestPath may be
 															// re-assigned.
 PdfFileSecurity fileSecurity = new PdfFileSecurity(inFile, outFile);
 fileSecurity.EncryptFile("userpass", "ownerpass", DocumentPrivilege.Print,
 		KeySize.x256);

Parameters:

ParameterTypeDescription
userPasswordjava.lang.StringUser password.
ownerPasswordjava.lang.StringOwner password.
privilegeDocumentPrivilegeSet privilege.
keySizeKeySizeKeySize.x40 for 40 bits encryption, KeySize.x128 for 128 bits encryption and KeySize.x256 for 256 bits encryption.

Returns: boolean - True for success.

tryEncryptFile(String userPassword, String ownerPassword, DocumentPrivilege privilege, KeySize keySize)

public final boolean tryEncryptFile(String userPassword, String ownerPassword, DocumentPrivilege privilege, KeySize 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. Does not throw an exception if process failed.

string inFile = "input.pdf"; //The TestPath may be re-assigned.
 string outFile = "output.pdf"; //The TestPath may be re-assigned.
 PdfFileSecurity fileSecurity = new PdfFileSecurity(inFile,outFile);
 bool result = fileSecurity.tryEncryptFile("userpass", "ownerpass", DocumentPrivilege.Print, KeySize.x256);

Parameters:

ParameterTypeDescription
userPasswordjava.lang.StringUser password.
ownerPasswordjava.lang.StringOwner password.
privilegeDocumentPrivilegeSet privilege.
keySizeKeySizeKeySize.x40 for 40 bits encryption, KeySize.x128 for 128 bits encryption and KeySize.x256 for 256 bits encryption.

Returns: boolean - True for success, or false.

encryptFile(String userPassword, String ownerPassword, DocumentPrivilege privilege, KeySize keySize, Algorithm cipher)

public boolean encryptFile(String userPassword, String ownerPassword, DocumentPrivilege privilege, KeySize keySize, Algorithm 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. Throws an exception if process failed.


String inFile = "input.pdf"; // The TestPath may be
 															// re-assigned.
 String outFile = "output.pdf"; // The TestPath may be
 															// re-assigned.
 PdfFileSecurity fileSecurity = new PdfFileSecurity(inFile, outFile);
 fileSecurity.encryptFile("userpass", "ownerpass", DocumentPrivilege.Print,
 		KeySize.x256, Algorithm.AES);

Parameters:

ParameterTypeDescription
userPasswordjava.lang.StringUser password.
ownerPasswordjava.lang.StringOwner password.
privilegeDocumentPrivilegeSet privilege.
keySizeKeySizeKeySize.x40 for 40 bits encryption, KeySize.x128 for 128 bits encryption and KeySize.x256 for 256 bits encryption.
cipherAlgorithmAlgorithm.AES to encrypt using AES algorithm or Algorithm.RC4 for RC4 encryption.

Returns: boolean - True for success, or false.

decryptFile(String ownerPassword)

public final 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. Throws an exception if process failed.

string inFile = "input.pdf"; //The TestPath may be re-assigned.
 string outFile = "output.pdf"; //The TestPath may be re-assigned.
 PdfFileSecurity fileSecurity = new PdfFileSecurity(inFile,outFile);
 fileSecurity.decryptFile("ownerpass");

Parameters:

ParameterTypeDescription
ownerPasswordjava.lang.StringOwner password.

Returns: boolean - True for success.

tryDecryptFile(String ownerPassword)

public final boolean tryDecryptFile(String ownerPassword)

Decrypts an encrypted Pdf document by owner password. If the document hasn’t owner password, it is allow to use user password. Does not throw an exception if process failed.

string inFile = "input.pdf"; //The TestPath may be re-assigned.
 string outFile = "output.pdf"; //The TestPath may be re-assigned.
 PdfFileSecurity fileSecurity = new PdfFileSecurity(inFile,outFile);
 bool result = fileSecurity.TryDecryptFile("ownerpass");

Parameters:

ParameterTypeDescription
ownerPasswordjava.lang.StringOwner password.

Returns: boolean - True for success,or false.

setPrivilege(DocumentPrivilege privilege)

public boolean setPrivilege(DocumentPrivilege privilege)

Sets Pdf file security with empty user/owner passwords. The owner password will be added by a random string. Throws an exception if process failed.


string inFile = "input.pdf"; // The TestPath may be re-assigned.
 string outFile = "output.pdf"; // The TestPath may be re-assigned.
 PdfFileSecurity fileSecurity = new PdfFileSecurity(inFile, outFile);
 fileSecurity.setPrivilege(DocumentPrivilege.Print);

Parameters:

ParameterTypeDescription
privilegeDocumentPrivilegeSet privilege.

Returns: boolean - True for success.

setPrivilege(String userPassword, String ownerPassword, DocumentPrivilege privilege)

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

Sets Pdf file security with original password. Throws an exception if process failed.


string inFile = "input.pdf"; // The TestPath may be re-assigned.
 string outFile = "output.pdf"; // The TestPath may be re-assigned.
 PdfFileSecurity fileSecurity = new PdfFileSecurity(inFile, outFile);
 fileSecurity.setPrivilege(userPassword, ownerPassword, DocumentPrivilege.getPrint());

Parameters:

ParameterTypeDescription
userPasswordjava.lang.StringOriginal user password.
ownerPasswordjava.lang.StringOriginal owner password.
privilegeDocumentPrivilegeSet privilege.

Returns: boolean - True for success.

trySetPrivilege(String userPassword, String ownerPassword, DocumentPrivilege privilege)

public final boolean trySetPrivilege(String userPassword, String ownerPassword, DocumentPrivilege privilege)

Sets Pdf file security with original password. Does not throw an exception if process failed.

string inFile = "D:\\input.pdf"; //The TestPath may be re-assigned.
 string outFile = "D:\\output.pdf"; //The TestPath may be re-assigned.
 PdfFileSecurity fileSecurity = new PdfFileSecurity(inFile,outFile);
 bool result = fileSecurity.trySetPrivilege(userPassword, ownerPassword, DocumentPrivilege.Print);

Parameters:

ParameterTypeDescription
userPasswordjava.lang.StringOriginal user password.
ownerPasswordjava.lang.StringOriginal owner password.
privilegeDocumentPrivilegeSet privilege.

Returns: boolean - True for success, or false.

changePassword(String ownerPassword, String newUserPassword, String newOwnerPassword)

public final 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. Throws an exception if process failed.

string inFile = "D:\\input.pdf"; //The TestPath may be re-assigned.
  string outFile = "D:\\output.pdf";	//The TestPath may be re-assigned.
  PdfFileSecurity fileSecurity = new PdfFileSecurity(inFile,outFile);
  fileSecurity.changePassword("owner","newuser","newowner");

Parameters:

ParameterTypeDescription
ownerPasswordjava.lang.StringOriginal Owner password.
newUserPasswordjava.lang.StringNew User password.
newOwnerPasswordjava.lang.StringNew Owner password.

Returns: boolean - True for success.

tryChangePassword(String ownerPassword, String newUserPassword, String newOwnerPassword)

public final boolean tryChangePassword(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 Does not throw an exception if process failed. with a random string if the new owner password is null or empty.

string inFile = "D:\\input.pdf"; //The TestPath may be re-assigned.
  string outFile = "D:\\output.pdf";	//The TestPath may be re-assigned.
  PdfFileSecurity fileSecurity = new PdfFileSecurity(inFile,outFile);
  bool result = fileSecurity.tryChangePassword("owner","newuser","newowner");

Parameters:

ParameterTypeDescription
ownerPasswordjava.lang.StringOriginal Owner password.
newUserPasswordjava.lang.StringNew User password.
newOwnerPasswordjava.lang.StringNew Owner password.

Returns: boolean - True for success,or false.

changePassword(String ownerPassword, String newUserPassword, String newOwnerPassword, DocumentPrivilege privilege, KeySize keySize)

public boolean changePassword(String ownerPassword, String newUserPassword, String newOwnerPassword, DocumentPrivilege privilege, KeySize 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.

Throws an exception if process failed.


string inFile = "input.pdf"; // The TestPath may be
 														// re-assigned.
 string outFile = "output.pdf"; // The TestPath may be
 															// re-assigned.
 PdfFileSecurity fileSecurity = new PdfFileSecurity(inFile, outFile);
 fileSecurity.changePassword("owner", "newuser", "newowner",
 		DocumentPrivilege.Print, KeySize.x256);

Parameters:

ParameterTypeDescription
ownerPasswordjava.lang.StringOriginal owner password.
newUserPasswordjava.lang.StringNew User password.
newOwnerPasswordjava.lang.StringNew Owner password.
privilegeDocumentPrivilegeReset security.
keySizeKeySizeKeySize.x40 for 40 bits encryption, KeySize.x128 for 128 bits encryption and KeySize.x256 for 256 bits encryption.

Returns: boolean - True for success.

tryChangePassword(String ownerPassword, String newUserPassword, String newOwnerPassword, DocumentPrivilege privilege, KeySize keySize)

public final boolean tryChangePassword(String ownerPassword, String newUserPassword, String newOwnerPassword, DocumentPrivilege privilege, KeySize 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. Does not throw an exception if process failed.

string inFile = ".D:\\input.pdf"; //The TestPath may be re-assigned.
 string outFile = "D:\\output.pdf";	//The TestPath may be re-assigned.
 PdfFileSecurity fileSecurity = new PdfFileSecurity(inFile,outFile);
 bool result = fileSecurity.tryChangePassword("owner","newuser","newowner", DocumentPrivilege.Print,KeySize.x256);

Parameters:

ParameterTypeDescription
ownerPasswordjava.lang.StringOriginal owner password.
newUserPasswordjava.lang.StringNew User password.
newOwnerPasswordjava.lang.StringNew Owner password.
privilegeDocumentPrivilegeReset security.
keySizeKeySizeKeySize.x40 for 40 bits encryption, KeySize.x128 for 128 bits encryption and KeySize.x256 for 256 bits encryption.

Returns: boolean - True for success, or false.

changePassword(String ownerPassword, String newUserPassword, String newOwnerPassword, DocumentPrivilege privilege, KeySize keySize, Algorithm cipher)

public boolean changePassword(String ownerPassword, String newUserPassword, String newOwnerPassword, DocumentPrivilege privilege, KeySize keySize, Algorithm 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. Throws an exception if process failed.


string inFile = "input.pdf"; // The TestPath may be
 														// re-assigned.
 string outFile = "output.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:

ParameterTypeDescription
ownerPasswordjava.lang.StringOriginal owner password.
newUserPasswordjava.lang.StringNew User password.
newOwnerPasswordjava.lang.StringNew Owner password.
privilegeDocumentPrivilegeReset security.
keySizeKeySizeKeySize.x40 for 40 bits encryption, KeySize.x128 for 128 bits encryption and KeySize.x256 for 256 bits encryption.
cipherAlgorithmAlgorithm.AES to encrypt using AES algorithm or Algorithm.RC4 for RC4 encryption.

Returns: boolean - True for success.

tryChangePassword(String ownerPassword, String newUserPassword, String newOwnerPassword, DocumentPrivilege privilege, KeySize keySize, Algorithm cipher)

public final boolean tryChangePassword(String ownerPassword, String newUserPassword, String newOwnerPassword, DocumentPrivilege privilege, KeySize keySize, Algorithm cipher)

Changes the user password and password by owner password, allows to reset Pdf document 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. Does not throw an exception if process failed.

string inFile = "D:\\input.pdf"; //The TestPath may be re-assigned.
 string outFile = "D:\\output.pdf";	//The TestPath may be re-assigned.
 PdfFileSecurity fileSecurity = new PdfFileSecurity(inFile,outFile);
 bool result = fileSecurity.changePassword("owner","newuser","newowner", DocumentPrivilege.Print,KeySize.x256,Algorithm.AES);

Parameters:

ParameterTypeDescription
ownerPasswordjava.lang.StringOriginal owner password.
newUserPasswordjava.lang.StringNew User password.
newOwnerPasswordjava.lang.StringNew Owner password.
privilegeDocumentPrivilegeReset security.
keySizeKeySizeKeySize.x40 for 40 bits encryption, KeySize.x128 for 128 bits encryption and KeySize.x256 for 256 bits encryption.
cipherAlgorithmAlgorithm.AES to encrypt using AES algorithm or Algorithm.RC4 for RC4 encryption.

Returns: boolean - True for success, or false.

bindPdf(String srcFile)

public void bindPdf(String srcFile)

Initializes the facade.

Parameters:

ParameterTypeDescription
srcFilejava.lang.StringThe PDF file.

bindPdf(InputStream srcStream)

public void bindPdf(InputStream srcStream)

Initializes the facade.

Parameters:

ParameterTypeDescription
srcStreamjava.io.InputStreamThe stream of PDF file.

close()

public void close()

Closes the facade.

dispose()

public void dispose()

Closes the facade.