checking if pdf is password protected using itextsharp

前端 未结 4 987
情歌与酒
情歌与酒 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:55

    Just in case it ends up helping someone, here's a simple solution I have been using in vb.net. The problem with checking with fullpermissions (As mentioned above) is that you can't actually open a PDF that has a password that prevents you from opening it. I also have something I do about check for that in the code below. itextsharp.text.pdf has a few exceptions you might find useful actually, check it out if this isn't doing what you need.

    Dim PDFDoc As PdfReader
    Try
        PDFDoc = New PdfReader(PDFToCheck)
    If PDFDoc.IsOpenedWithFullPermissions = False Then
       'PDF prevents things but it can still be opened. e.g. printing.
    end if
    Catch ex As iTextSharp.text.pdf.BadPasswordException
        'this exception means the PDF can't be opened at all. 
    Finally 
        'do whatever if things are normal!
    End Try
    

提交回复
热议问题