Validation to prevent from uploading password-protected files and multilayer documents

We require to implement a validation to prevent users from attempting to upload password-protected files and multilayer documents in our application based on C# and MVC5.
How can I detect those file attributes to implement this validation? Could you send me some code snippets regarding this issue? Thanks!

@jimmy.rojas

Thank you for contacting support.

You may detect password protected files with IsEncrypted property as in the code snippet below:

PdfFileInfo info = new PdfFileInfo(dataDir + "Aspose.pdf");
if (info.IsEncrypted)
{
    //Source document is password protected
    //TODO Code Here
}
else
{
    //Source document is not password protected
    //TODO Code Here
}

For detecting layers, you may iterate through each page and check if it contains any layers.

Document document = new Document(dataDir + "Aspose.pdf");
bool flag = false;
foreach (Page page in document.Pages)
{
    List<Aspose.Pdf.Layer> layers = page.Layers;
    if (layers != null)
    {
        flag = true;
        Console.WriteLine("Document contains layers!");
    }
}

We hope this will be helpful. Please feel free to contact us if you need any further assistance.

Thanks for your reply. How can I do the same validation for Word Documents (Aspose.Words)? Could you send me some code snippets please?

@jimmy.rojas

We are looking into it and will get back to you soon.

@jimmy.rojas

You can use FileFormatInfo.IsEncrypted property to check either document encrypted or not.

Following code example shows how to use the FileFormatUtil class to detect the document format and other features of the document.

FileFormatInfo info = FileFormatUtil.DetectFileFormat(MyDir + "Document.doc");
Console.WriteLine("The document format is: " + FileFormatUtil.LoadFormatToExtension(info.LoadFormat));
Console.WriteLine("Document is encrypted: " + info.IsEncrypted);
Console.WriteLine("Document has a digital signature: " + info.HasDigitalSignature);

Thank you for you help! I appreciate it.

The file uploaded (Labels - New Graphics Chicken Bar 8.29.19.PDF (475.9 KB)
) is a PDF with layers, however these layers are not detected with the given code snippet:

Aspose.Pdf.Document document = new Aspose.Pdf.Document(FileName);
foreach (Page page in document.Pages)
{
List<Aspose.Pdf.Layer> layers = page.Layers;
if (layers != null)
{

                    }
                }

I tried with other PDF with layers and seems that the code is not working as is expected.
Why is that? What else is missing?

@jimmy.rojas

We have been able to reproduce the issue in our environment. A ticket with ID PDFNET-47175 has been logged in our issue management system for further investigation and resolution. The ticket ID has been linked with this thread so that you will receive notification as soon as the ticket is resolved.

We are sorry for the inconvenience.

Hi,

Any update on this?

@jimmy.rojas

Please note that the issue has been logged under free support model and will be investigated on first come first serve basis. Therefore, it may take some months to resolve. As soon as we have some definite updates regarding ticket resolution, we will let you know.

Furthermore, we also offer paid support model where issues are resolved on urgent basis and have priority over the issues logged under free support model. You may check our Paid Support options for your reference.