Skip to content

Instantly share code, notes, and snippets.

@anjackson
Created March 25, 2013 13:25
Show Gist options
  • Save anjackson/5237071 to your computer and use it in GitHub Desktop.
Save anjackson/5237071 to your computer and use it in GitHub Desktop.
This shows the logic needed to test for two the different PDF encryption modes. It is written in C#, and has been tested with version 1.2.1 of PDFBox, cross-compiled to .net using IKVM (http://www.ikvm.net/).
Console.WriteLine("Document encrypted = {0}", document.isEncrypted().ToString());
// Can it be opened?
bool isOpenable = true;
if (document.isEncrypted())
{
DecryptionMaterial decryptionMaterial = new StandardDecryptionMaterial("");
try
{
document.openProtection(decryptionMaterial);
AccessPermission ap = document.getCurrentAccessPermission();
if (ap.isOwnerPermission())
{
Console.WriteLine("You have owner rights.");
}
else
{
Console.WriteLine("You do not have owner rights.");
}
}
catch (CryptographyException e)
{
Console.WriteLine("Cannot open document. :: {0}",e.getMessage());
isOpenable = false;
}
}
Console.WriteLine("Document openable = {0}", isOpenable);
// Detailed rights:
if (isOpenable)
{
Console.WriteLine("Rights: canExtractContent = {0}", document.getCurrentAccessPermission().canExtractContent().ToString());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment