GdPicture.NET.14.API
GdPicture14 Namespace / GdPicturePDF Class / SetPassword Method
The user password or the owner password to be used to decrypt the currently loaded PDF document.
Example





In This Topic
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
'Declaration
 
Public Function SetPassword( _
   ByVal Password As String _
) As GdPictureStatus
public GdPictureStatus SetPassword( 
   string Password
)
public function SetPassword( 
    Password: String
): GdPictureStatus; 
public function SetPassword( 
   Password : String
) : GdPictureStatus;
public: GdPictureStatus SetPassword( 
   string* Password
) 
public:
GdPictureStatus SetPassword( 
   String^ Password
) 

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.

Remarks
The basic rules for applying the user and the owner passwords are:

1. A PDF document was encrypted using the user (open) password only. By the definition this means that the owner password is the same as the user password. Providing the correct user password on opening allows you to fully access the document content.

2. A PDF document was encrypted using the owner (permission, master) password only. You are allowed to open and view the document content without providing the password, but you need to respect the access rights which has been set. To change these rights to be able to control the access to the document content you need to provide the owner password.

3. A PDF document was encrypted using both passwords, the user and the owner as well, assuming they are not the same. Providing the user password on opening leads to the point 2. Providing the owner password on opening allows you to fully access the document content directly.

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