Is it possible to determine if a byte array represents a pdf file that can not be edited?

9

Context

I have two steps in my service, uploading and downloading the file. In uploading to the service I get the data through an array of ASCII bytes .

This data is stored in a database. In the download, the data related to the file is searched in the database and written in binary to a temporary file.

From there, I put a watermark, and return the final file to the user. This is a PDF file .

The Problem

The problem is that when the user uploads a file (PDF) that can not be edited. This is not a read-only attribute since the data is being fetched from the database, not the user's computer.

What characterizes the "editability" of the file is a permission assigned within the PDF format when the file is protected .

Objective

What I'm trying to understand is if there is a way to identify if the file:

  • Is a protected PDF
  • If the edit permission was assigned by the file's creator
  • So that I can handle the application flow and prevent a non-editable file from being stored in the database (and eventually an error is thrown because I can not add the watermark.)

    Exemplo do byte[]:
    
        [0] 37  
    
        [1] 80  
    
        [2] 68  
    
        [...] ... 
    
        [84006] 10  
    

    Remembering that my solution today is to write the file to a temporary location inside the server to do this check. I want to avoid this step and perform any checks directly on the data array that I get from the database.

    To rebuild the file from the data stored in the database, I use the following feature:

    BinaryWriter Writer = new BinaryWriter(System.IO.File.OpenWrite(fileName));
    Writer.Write("variavel_byte[]");
    
        
    asked by anonymous 14.06.2017 / 16:40

    1 answer

    1

    There is a solution without using the Bytes Array using iTextSharp, with byte array I did not find anything, even in other searches all point to the components.

      

    PM > Install-Package iTextSharp

        using iTextSharp.text.pdf;
    
        private void CheckPdfProtection()
        {
            var filePath = @"c:\temp\EmploymentHistory.pdf";
            using (var reader = new PdfReader(filePath))
            {
                if (!reader.IsEncrypted()) return;
    
                if (!PdfEncryptor.IsPrintingAllowed(Convert.ToInt32(reader.Permissions)))
                    throw new InvalidOperationException("Arquivo protegido para impressão");
                if (!PdfEncryptor.IsModifyContentsAllowed(Convert.ToInt32(reader.Permissions)))
                    throw new InvalidOperationException("Arquivo protegido para escrita");
            }
        }
    
        
    26.11.2017 / 11:38