Is it possible to determine whether an array of bytes represents a pdf file that cannot be edited?

Asked

Viewed 292 times

9

Context

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

This data is stored in a database. In the download, the data referring to the file is fetched from the database and written in binary to a temporary file.

From there, I place a watermark, and return the download of the final file to the user. It is a PDF file.

The Problem

The problem is that when the user uploads a file (PDF) it cannot be edited. This is not a read-only attribute, because the data is being searched in the database, and not in the user’s computer.

What characterizes the "editability" the file is an assigned permission within the PDF format when the file is protégé.

Goal

What I try to understand is if there is a way to identify if the file:

  1. It’s a protected PDF
  2. If the editing permission has been assigned by the file creator

So I can handle the application flow and prevent a non-enterable file from being stored in the database (and eventually an error is thrown in by not being able to add the watermark.).

Exemplo do byte[]:

    [0] 37  

    [1] 80  

    [2] 68  

    [...] ... 

    [84006] 10  

Remembering that, currently, my solution is to write the file in a temporary location inside the server to make this check. I want to avoid this step and perform any verification directly in the data array I receive from the database.

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

BinaryWriter Writer = new BinaryWriter(System.IO.File.OpenWrite(fileName));
Writer.Write("variavel_byte[]");
  • Friend, I took a look at the specification of the PDF (link below) and I found an excerpt that indicates if a document is ENCRYPTED, however for lack of time (I confess) I could not study deeply the subject to help you. Since your need is pressing, maybe by giving you the way you can finish the study. At the end of the PDF text there is a session called Trailer. If there is a key /Encrypt this indicates that the document has some protection. The numbers that follow /Encrypt indicate which object is responsible for the encryption and which access levels are guaranteed (continue).

  • Thus a Trailer with "/Encrypt 30 0 R" content indicates that you should look at the contents of the "30 0" object to verify its permissions. Look for the section "30 0 obj" to find the access levels of the document, within this object there will be some other specifications. Note the /Filter (which indicates which filter will be used) and /R (which indicates the revision), they have influence on the values to be searched and where to look for them. Assuming a /Standard value the document contains information on how to manage these permissions (pp. 74 to 77).

  • (continuing). Look for the /P values to identify permissions. Your study continues from here... rsrsrs... Follow the link to the reference of the PDF 1.4 specification (for other versions I do not know if there are modifications in these permissions). https://www.adobe.com/content/dam/acom/en/devnet/pdf/pdfs/pdf_reference_archives/PDFReference.pdf

1 answer

1

There is a solution without using the Byte Array using iTextSharp, with byte array found nothing, including 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");
        }
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.