Handling incorrect document password exception

SignatureΒ class supports handling incorrect password passed to open protected documents over classΒ IncorrectPasswordException.

Here are the steps to handle incorrect password exception when working with protected documents with GroupDocs.Signature:

Handling incorrect password exception

Following example demonstrates how to handle incorrect password exception.

// initialize LoadOptions with incorrect Password
LoadOptions loadOptions = new LoadOptions() { Password = "1" };
using (Signature signature = new Signature("ProtectedPwd.pdf", loadOptions))
{
    try
    {
        QrCodeSignOptions options = new QrCodeSignOptions("JohnSmith")
        {
            EncodeType = QrCodeTypes.QR,
            Left = 100,
            Top = 100
        };
        // try to sign document to file, we expect for PasswordRequiredException
        signature.Sign(outputFilePath, options);
        Console.WriteLine("\nSource document signed successfully.\nFile saved at " + outputFilePath);
    }
    catch (IncorrectPasswordException ex)
    {
        Console.WriteLine($"HandlingIncorrectPasswordException: {ex.Message}");
    }
    catch (GroupDocsSignatureException ex)
    {
        Console.WriteLine($"Common GroupDocsSignatureException: {ex.Message}");
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Common Exception happens only at user code level: {ex.Message}");
    }
    finally
    {
    }
}

More resources

GitHub Examples

You may easily run the code above and see the feature in action in ourΒ GitHub examples:

Free Online Apps

Along with the full-featured .NET library, we provide simple but powerful free online apps.

To sign PDF, Word, Excel, PowerPoint, and other documents you can use the online apps from the GroupDocs.Signature App Product Family.