Reference Guide
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 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 oGdPicturePDF As New GdPicturePDF()
    If oGdPicturePDF.LoadFromFile("test.pdf", False) = GdPictureStatus.OK Then
        'We are going to create an encrypted PDF document here.
        If oGdPicturePDF.SaveToFile("encrypted.pdf", PdfEncryption.PdfEncryption128BitRC4, "user", "owner", False, False,
            False, False, False, False, False, False) = GdPictureStatus.OK Then
            If oGdPicturePDF.CloseDocument() <> GdPictureStatus.OK Then
                Return
            End If
            If oGdPicturePDF.LoadFromFile("encrypted.pdf", False) <> GdPictureStatus.OK Then
                Return
            End If
            'The new encrypted PDF document has been successfully loaded.
                
            Dim isEncrypted As Boolean = oGdPicturePDF.IsEncrypted()
            Dim status As GdPictureStatus = oGdPicturePDF.GetStat()
            If status = GdPictureStatus.OK Then
                If isEncrypted Then
                    'Providing an user password.
                    status = oGdPicturePDF.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 oGdPicturePDF.CloseDocument() <> GdPictureStatus.OK Then
                        Return
                    End If
                    If oGdPicturePDF.LoadFromFile("encrypted.pdf", False) <> GdPictureStatus.OK Then
                        Return
                    End If
                
                    'Providing an owner password.
                    status = oGdPicturePDF.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
    oGdPicturePDF.Dispose()
    string caption = "Example: SetPassword";
    GdPicturePDF oGdPicturePDF = new GdPicturePDF();
    if (oGdPicturePDF.LoadFromFile("test.pdf", false) == GdPictureStatus.OK)
    {
        //We are going to create an encrypted PDF document here.
        if (oGdPicturePDF.SaveToFile("encrypted.pdf", PdfEncryption.PdfEncryption128BitRC4, "user", "owner", false, false, false, false, false, false, false, false) == GdPictureStatus.OK)
        {
            if (oGdPicturePDF.CloseDocument() != GdPictureStatus.OK) return;
            if (oGdPicturePDF.LoadFromFile("encrypted.pdf", false) != GdPictureStatus.OK) return;
            //The new encrypted PDF document has been successfully loaded.
                
            bool isEncrypted = oGdPicturePDF.IsEncrypted();
            GdPictureStatus status = oGdPicturePDF.GetStat();
            if (status == GdPictureStatus.OK)
            {
                if (isEncrypted)
                {
                     //Providing an user password.
                    status = oGdPicturePDF.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 (oGdPicturePDF.CloseDocument() != GdPictureStatus.OK) return;
                    if (oGdPicturePDF.LoadFromFile("encrypted.pdf", false) != GdPictureStatus.OK) return;
                
                    //Providing an owner password.
                    status = oGdPicturePDF.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);
    }
    oGdPicturePDF.Dispose();
    See Also