Free Support Forum - aspose.com

How to read the property “DocumentPrivilege”

Hi:

This is how I set the security properties in a pdf file:

using (var documenttosecure = new Aspose.Pdf.Document(inputFilePath))
{
using (var pdfOutput = newPdfFileSecurity(documenttosecure))
{
var docP = DocumentPrivilege.AllowAll;
docP.AllowPrint = true;
docP.AllowCopy = true;
docP.AllowModifyContents = false;
pdfOutput.SetPrivilege(docP);
documenttosecure.Save(outputFile);
}
}

But then I need to load again the same pdf file and I need to access the DocumentPrivilege from the pdf document to know if the property “AllowmodifyContents” is false or true.

This is how I load the pdf file:

public static bool HasPdfFileAnyTypeOfPasswordProtection(string inputFilePath)
{
const int acSecurityLevelPermissions = 3900;
try
{
var pdfFileInfo = new PdfFileInfo(inputFilePath);
//Find if pdf file has security to open
if (pdfFileInfo.HasOpenPassword)
{
return true;
}
//Find if the pdf document has security to edit
if (pdfFileInfo.HasEditPassword)
{
return true;
}

        return false;
  }
  catch (Exception exception)
  {
       Logger.Error(exception, $"Could not open the file {inputFilePath}. Probably because is password protected.");
       throw;
  }

}

Can you help me to find the “AllowModifyContents” property?

Regards;
Rikard

@rikardbo

Thanks for contacting support.

GetDocumentPrivilege() method of Aspose.Pdf.Facades.PdfFileInfo class, returns an object of type DocumentPrivilege, through which you can determine the value of AllowModifyContents property. Please check following code snippet, in order to achieve your requirement:

// Instantiate PdfFileInfo Class object.
Aspose.Pdf.Facades.PdfFileInfo info = new Aspose.Pdf.Facades.PdfFileInfo();
// Load your encrypted PDF document.
info.BindPdf(dataDir + "EncryptedDocument.pdf");
// Get DocumentPrivilege
Facades.DocumentPrivilege documentPrivilege = info.GetDocumentPrivilege();
// Determine AllowModifyContents
bool AllowModifyContents = documentPrivilege.AllowModifyContents;

In case you still face any issue, please share your PDF document along with complete sample code snippet. We will test the scenario in our environment and address it accordingly.

Thank you very much for your answer Asad Ali. It is actually what we were looking for.

@rikardbo

Thanks for your kind feedback.

Please keep using our API and in case of any further assistance, feel free to create a new topic in our forums. We will be more than happy to assist you accordingly.