Example





In This Topic

IsEncrypted Method (GdPicturePDF)

In This Topic
Returns if the currently loaded PDF document is encrypted or not using the standard security handler encryption mechanism. This approach allows you to define access permissions and up to two passwords - an user password and an owner password. You can use the SetPassword method to decrypt the PDF documents. Once this method has been applied, the currently loaded document remains decrypted for subsequent use.

Please read more about providing the passwords in the SetPassword method.

Syntax
'Declaration
 
Public Function IsEncrypted() As Boolean
public bool IsEncrypted()
public function IsEncrypted(): Boolean; 
public function IsEncrypted() : boolean;
public: bool IsEncrypted(); 
public:
bool IsEncrypted(); 

Return Value

true if the currently loaded PDF document is encrypted using the standard security handler, otherwise false.

Once the loaded PDF document is decrypted, the return value will be always false.

Remarks
A PDF document can be encrypted to protect its content from unauthorized access. An user (or open) password allows users to open the document with respect to the defined access permissions. The document cannot be opened at all without providing this password if one is set. An owner (or permission or master) password will give users full access to the document. This access restriction password does not affect user's ability to open and view the PDF document, but prevents user from editing, changing, printing and more according to which access rights has been set.
Example
How to correctly find out if the PDF document is encrypted, decrypted or unencrypted.
Dim caption As String = "Example: IsEncrypted"
Dim gdpicturePDF As New GdPicturePDF()
'We assume the test.pdf is unencrypted PDF document.
If gdpicturePDF.LoadFromFile("test.pdf", False) = GdPictureStatus.OK Then
    Dim encrypted As Boolean = gdpicturePDF.IsEncrypted()
    If gdpicturePDF.GetStat() = GdPictureStatus.OK Then
        If encrypted Then
            'This is an incorrect option.
            MessageBox.Show("This PDF document is encrypted.", caption)
        Else
            'This is a correct option.
            MessageBox.Show("This PDF document is unencrypted.", caption)
        End If
    Else
        MessageBox.Show("The IsEncrypted() method has failed.", caption)
    End If
            
    If gdpicturePDF.CloseDocument() <> GdPictureStatus.OK Then
        Return
    End If
Else
    MessageBox.Show("The file can't be loaded.", caption)
End If
            
'Please see the example in the SetPassword method for creating this file.
If gdpicturePDF.LoadFromFile("encrypted.pdf", False) = GdPictureStatus.OK Then
    Dim encrypted As Boolean = gdpicturePDF.IsEncrypted()
    If gdpicturePDF.GetStat() = GdPictureStatus.OK Then
        If encrypted Then
            'This is a correct option, you can provide a password here.
            MessageBox.Show("This PDF document is encrypted.", caption)
            'Providing an user password.
            Dim status As GdPictureStatus = gdpicturePDF.SetPassword("user")
            If status = GdPictureStatus.OK Then
                encrypted = gdpicturePDF.IsEncrypted()
                status = gdpicturePDF.GetStat()
                If status = GdPictureStatus.OK Then
                    If encrypted Then
                        'This is an incorrect option.
                        MessageBox.Show("This PDF document is still encrypted.", caption)
                    Else
                        'This is a correct option.
                        MessageBox.Show("This PDF document is decrypted now.", caption)
                    End If
                Else
                    MessageBox.Show("The IsEncrypted() method has failed with the status: " + status.ToString(), caption)
                End If
            Else
                MessageBox.Show("The SetPassword() method has failed with the status: " + status.ToString(), caption)
            End If
        Else
            'This is an incorrect option.
            MessageBox.Show("This PDF is unencrypted.", caption)
        End If
    Else
        MessageBox.Show("The IsEncrypted() method has failed.", caption)
    End If
Else
    MessageBox.Show("The file can't be loaded.", caption)
End If
gdpicturePDF.Dispose()
string caption = "Example: IsEncrypted";
GdPicturePDF gdpicturePDF = new GdPicturePDF();
//We assume the test.pdf is unencrypted PDF document.
if (gdpicturePDF.LoadFromFile("test.pdf", false) == GdPictureStatus.OK)
{
    bool encrypted = gdpicturePDF.IsEncrypted();
    if (gdpicturePDF.GetStat() == GdPictureStatus.OK)
    {
        if (encrypted)
            //This is an incorrect option.
            MessageBox.Show("This PDF document is encrypted.", caption);
        else
            //This is a correct option.
            MessageBox.Show("This PDF document is unencrypted.", caption);
    }
    else
        MessageBox.Show("The IsEncrypted() method has failed.", caption);
            
    if (gdpicturePDF.CloseDocument() != GdPictureStatus.OK) return;
}
else
{
    MessageBox.Show("The file can't be loaded.", caption);
}
            
//Please see the example in the SetPassword method for creating this file.
if (gdpicturePDF.LoadFromFile("encrypted.pdf", false) == GdPictureStatus.OK)
{
    bool encrypted = gdpicturePDF.IsEncrypted();
    if (gdpicturePDF.GetStat() == GdPictureStatus.OK)
    {
        if (encrypted)
        {
            //This is a correct option, you can provide a password here.
            MessageBox.Show("This PDF document is encrypted.", caption);
            //Providing an user password.
            GdPictureStatus status = gdpicturePDF.SetPassword("user");
            if (status == GdPictureStatus.OK)
            {
                encrypted = gdpicturePDF.IsEncrypted();
                status = gdpicturePDF.GetStat();
                if (status == GdPictureStatus.OK)
                {
                    if (encrypted)
                        //This is an incorrect option.
                        MessageBox.Show("This PDF document is still encrypted.", caption);
                    else
                        //This is a correct option.
                        MessageBox.Show("This PDF document is decrypted now.", caption);
                }
                else
                    MessageBox.Show("The IsEncrypted() method has failed with the status: " + status.ToString(), caption);
            }
            else
                MessageBox.Show("The SetPassword() method has failed with the status: " + status.ToString(), caption);
        }
        else
            //This is an incorrect option.
            MessageBox.Show("This PDF is unencrypted.", caption);
    }
    else
        MessageBox.Show("The IsEncrypted() method has failed.", caption);
}
else
{
    MessageBox.Show("The file can't be loaded.", caption);
}
gdpicturePDF.Dispose();
See Also