SetPassword Method (GdPicturePDF)
In This Topic
Decrypts the currently loaded PDF document with entering the password (an user password or an owner password) you have specified. Once the correct password is provided (it does not matter if an user password or an owner password), the PDF document becomes decrypted for subsequent use and any other attempt to set a password will fail.
Please read more about the password and permissions security in the GdPicturePDF.IsEncrypted method.
Syntax
Parameters
- Password
- The user password or the owner password to be used to decrypt the currently loaded PDF document.
Return Value
A member of the GdPictureStatus enumeration. If the method has been successfully followed, then the return value is GdPictureStatus.OK.
We strongly recommend always checking this status first.
Example
How to correctly decrypt the PDF document providing an user or an owner password.
Dim caption As String = "Example: SetPassword"
Dim gdpicturePDF As New GdPicturePDF()
If gdpicturePDF.LoadFromFile("test.pdf", False) = GdPictureStatus.OK Then
'We are going to create an encrypted PDF document here.
If gdpicturePDF.SaveToFile("encrypted.pdf", PdfEncryption.PdfEncryption128BitRC4, "user", "owner", False, False,
False, False, False, False, False, False) = GdPictureStatus.OK Then
If gdpicturePDF.CloseDocument() <> GdPictureStatus.OK Then
Return
End If
If gdpicturePDF.LoadFromFile("encrypted.pdf", False) <> GdPictureStatus.OK Then
Return
End If
'The new encrypted PDF document has been successfully loaded.
Dim isEncrypted As Boolean = gdpicturePDF.IsEncrypted()
Dim status As GdPictureStatus = gdpicturePDF.GetStat()
If status = GdPictureStatus.OK Then
If isEncrypted Then
'Providing an user password.
status = gdpicturePDF.SetPassword("user")
MessageBox.Show("The PDF document was encrypted and now it is decrypted with the status: " + status.ToString(), caption)
'You can work with the PDF document according to the specified access rights (see above).
'Closing and reloading the document to make it encrypted again.
If gdpicturePDF.CloseDocument() <> GdPictureStatus.OK Then
Return
End If
If gdpicturePDF.LoadFromFile("encrypted.pdf", False) <> GdPictureStatus.OK Then
Return
End If
'Providing an owner password.
status = gdpicturePDF.SetPassword("owner")
'You have full access to the document content now.
MessageBox.Show("The PDF document was encrypted and now it is decrypted with the status: " + status.ToString(), caption)
Else
MessageBox.Show("The PDF document is unencrypted.", caption)
End If
End If
Else
MessageBox.Show("The encrypted file has failed to save.", caption)
End If
Else
MessageBox.Show("The file can't be loaded.", caption)
End If
gdpicturePDF.Dispose()
string caption = "Example: SetPassword";
GdPicturePDF gdpicturePDF = new GdPicturePDF();
if (gdpicturePDF.LoadFromFile("test.pdf", false) == GdPictureStatus.OK)
{
//We are going to create an encrypted PDF document here.
if (gdpicturePDF.SaveToFile("encrypted.pdf", PdfEncryption.PdfEncryption128BitRC4, "user", "owner", false, false, false, false, false, false, false, false) == GdPictureStatus.OK)
{
if (gdpicturePDF.CloseDocument() != GdPictureStatus.OK) return;
if (gdpicturePDF.LoadFromFile("encrypted.pdf", false) != GdPictureStatus.OK) return;
//The new encrypted PDF document has been successfully loaded.
bool isEncrypted = gdpicturePDF.IsEncrypted();
GdPictureStatus status = gdpicturePDF.GetStat();
if (status == GdPictureStatus.OK)
{
if (isEncrypted)
{
//Providing an user password.
status = gdpicturePDF.SetPassword("user");
MessageBox.Show("The PDF document was encrypted and now it is decrypted with the status: " + status.ToString(), caption);
//You can work with the PDF document according to the specified access rights (see above).
//Closing and reloading the document to make it encrypted again.
if (gdpicturePDF.CloseDocument() != GdPictureStatus.OK) return;
if (gdpicturePDF.LoadFromFile("encrypted.pdf", false) != GdPictureStatus.OK) return;
//Providing an owner password.
status = gdpicturePDF.SetPassword("owner");
MessageBox.Show("The PDF document was encrypted and now it is decrypted with the status: " + status.ToString(), caption);
//You have full access to the document content now.
}
else
{
MessageBox.Show("The PDF document is unencrypted.", caption);
}
}
}
else
{
MessageBox.Show("The encrypted file has failed to save.", caption);
}
}
else
{
MessageBox.Show("The file can't be loaded.", caption);
}
gdpicturePDF.Dispose();
Example
How to correctly decrypt the PDF document providing an user or an owner password.
Dim caption As String = "Example: SetPassword"
Dim gdpicturePDF As New GdPicturePDF()
If gdpicturePDF.LoadFromFile("test.pdf", False) = GdPictureStatus.OK Then
'We are going to create an encrypted PDF document here.
If gdpicturePDF.SaveToFile("encrypted.pdf", PdfEncryption.PdfEncryption128BitRC4, "user", "owner", False, False,
False, False, False, False, False, False) = GdPictureStatus.OK Then
If gdpicturePDF.CloseDocument() <> GdPictureStatus.OK Then
Return
End If
If gdpicturePDF.LoadFromFile("encrypted.pdf", False) <> GdPictureStatus.OK Then
Return
End If
'The new encrypted PDF document has been successfully loaded.
Dim isEncrypted As Boolean = gdpicturePDF.IsEncrypted()
Dim status As GdPictureStatus = gdpicturePDF.GetStat()
If status = GdPictureStatus.OK Then
If isEncrypted Then
'Providing an user password.
status = gdpicturePDF.SetPassword("user")
MessageBox.Show("The PDF document was encrypted and now it is decrypted with the status: " + status.ToString(), caption)
'You can work with the PDF document according to the specified access rights (see above).
'Closing and reloading the document to make it encrypted again.
If gdpicturePDF.CloseDocument() <> GdPictureStatus.OK Then
Return
End If
If gdpicturePDF.LoadFromFile("encrypted.pdf", False) <> GdPictureStatus.OK Then
Return
End If
'Providing an owner password.
status = gdpicturePDF.SetPassword("owner")
'You have full access to the document content now.
MessageBox.Show("The PDF document was encrypted and now it is decrypted with the status: " + status.ToString(), caption)
Else
MessageBox.Show("The PDF document is unencrypted.", caption)
End If
End If
Else
MessageBox.Show("The encrypted file has failed to save.", caption)
End If
Else
MessageBox.Show("The file can't be loaded.", caption)
End If
gdpicturePDF.Dispose()
string caption = "Example: SetPassword";
GdPicturePDF gdpicturePDF = new GdPicturePDF();
if (gdpicturePDF.LoadFromFile("test.pdf", false) == GdPictureStatus.OK)
{
//We are going to create an encrypted PDF document here.
if (gdpicturePDF.SaveToFile("encrypted.pdf", PdfEncryption.PdfEncryption128BitRC4, "user", "owner", false, false, false, false, false, false, false, false) == GdPictureStatus.OK)
{
if (gdpicturePDF.CloseDocument() != GdPictureStatus.OK) return;
if (gdpicturePDF.LoadFromFile("encrypted.pdf", false) != GdPictureStatus.OK) return;
//The new encrypted PDF document has been successfully loaded.
bool isEncrypted = gdpicturePDF.IsEncrypted();
GdPictureStatus status = gdpicturePDF.GetStat();
if (status == GdPictureStatus.OK)
{
if (isEncrypted)
{
//Providing an user password.
status = gdpicturePDF.SetPassword("user");
MessageBox.Show("The PDF document was encrypted and now it is decrypted with the status: " + status.ToString(), caption);
//You can work with the PDF document according to the specified access rights (see above).
//Closing and reloading the document to make it encrypted again.
if (gdpicturePDF.CloseDocument() != GdPictureStatus.OK) return;
if (gdpicturePDF.LoadFromFile("encrypted.pdf", false) != GdPictureStatus.OK) return;
//Providing an owner password.
status = gdpicturePDF.SetPassword("owner");
MessageBox.Show("The PDF document was encrypted and now it is decrypted with the status: " + status.ToString(), caption);
//You have full access to the document content now.
}
else
{
MessageBox.Show("The PDF document is unencrypted.", caption);
}
}
}
else
{
MessageBox.Show("The encrypted file has failed to save.", caption);
}
}
else
{
MessageBox.Show("The file can't be loaded.", caption);
}
gdpicturePDF.Dispose();
See Also