Search code examples
c#itextpdfsharpasposepdfclown

Designing PDF component for easy access


I have seen open source and commercial PDF components which support Dot net implementation, I think almost every available component in market,but the strange to identify a document that is protected or not, every one is showing in the form of exception rather than a property.Is there anything tricky behind this? I would expect

Component.Load(inputFile.pdf);
If(Component.isProtected)
{
Component.Open(inputFile.pdf,password);
}
else
{
Component.Open(inputFile.pdf);
}

instead of the following regular approach

Try{
Component.Open(inputFile.pdf);
}
catch(Exception ex)
{
//bad password
//Some exception
}

Solution

  • This is possible with Aspose.Pdf for .NET, which is a commercial .NET component. It has a boolean property IsEncrypted for encrypted file detection. Sample code is given below.

    // load the source PDF doucment
    PdfFileInfo fileInfo = new PdfFileInfo(dataDir + "protected.pdf");
    // determine that source PDF file is Encrypted with password
    bool encrypted = fileInfo.IsEncrypted;
    MessageBox.Show("Encrypted: " + encrypted);
    

    I work for Aspose as a Developer Evangelist.