iTextSharp - Check PDF Document Properties - Conte

2019-09-08 03:23发布

问题:

I'm trying to check whether PDF document to be uploaded has the following document properties - Content Copying & Content Copying For Accessibility Allowed / Not Allowed using iTextSharp PDFReader. Is there any property to verify this functionality. I have pasted a sample code which is NOT returning the expected result.

Looking for solution using iTextSharp

Sample Code:

            using (PdfReader r = new PdfReader(@"xxx\yyy.pdf"))
            {
                if (PdfEncryptor.IsScreenReadersAllowed((int)(r.Permissions)))
                {
                    Console.WriteLine("Content Accessibility Enabled");
                }

                if (PdfEncryptor.IsCopyAllowed((int)(r.Permissions)))
                {
                    Console.WriteLine("Copy Enabled");
                }

                if (PdfEncryptor.IsAssemblyAllowed((int)(r.Permissions)))
                {
                    Console.WriteLine("Document Assembly Enabled");
                }
            }

回答1:

The Permissions value you check is initialized only for encrypted PDFs. The sample dialog you pasted here, on the other hand, shows No Security, so your sample document is not encrypted. Thus, the Permissions value is not set to any meaningful value.

None of the restrictions a PDF can get as part of the encryption process apply to non-encrypted PDFs. Thus, you might want to update your tests to

if (PdfEncryptor.IsScreenReadersAllowed((int)(r.Permissions)) || !r.IsEncrypted())
{
    Console.WriteLine("Content Accessibility Enabled");
}

if (PdfEncryptor.IsCopyAllowed((int)(r.Permissions)) || !r.IsEncrypted())
{
    Console.WriteLine("Copy Enabled");
}

if (PdfEncryptor.IsAssemblyAllowed((int)(r.Permissions)) || !r.IsEncrypted())
{
    Console.WriteLine("Document Assembly Enabled");
}