iTextSharp - Check PDF Document Properties - Content Copying, Content Copying For Accessibility

左心房为你撑大大i 提交于 2019-12-12 04:32:36

问题


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");
}


来源:https://stackoverflow.com/questions/40883003/itextsharp-check-pdf-document-properties-content-copying-content-copying-fo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!