checking if pdf is password protected using itextsharp

前端 未结 4 989
情歌与酒
情歌与酒 2020-12-15 13:06

I want to check if a pdf file is password protected or not to view. That is i want to know if the pdf file has user password or not.

I found some help in some forum

4条回答
  •  佛祖请我去吃肉
    2020-12-15 13:52

      private void CheckPdfProtection(string filePath)
            {
                try
                {
                    PdfReader reader = new PdfReader(filePath);
                    if (!reader.IsEncrypted()) return;
                    if (!PdfEncryptor.IsPrintingAllowed(reader.Permissions))
                        throw new InvalidOperationException("the selected file is print protected and cannot be imported");
                    if (!PdfEncryptor.IsModifyContentsAllowed(reader.Permissions))
                        throw new InvalidOperationException("the selected file is write protected and cannot be imported");
                }
                catch (BadPasswordException) { throw new InvalidOperationException("the selected file is password protected and cannot be imported"); }
                catch (BadPdfFormatException) { throw new InvalidDataException("the selected file is having invalid format and cannot be imported"); }
            }
    

提交回复
热议问题