Free Support Forum - aspose.com

Error encountered saving a password protected PDF using Aspose.PDF .NET

Hi,

We are using Aspose.PDF .NET to programmatically encrypt a PDF document but we are encountering an error upon save. This only happens in our client and not in our development environment. Any thoughts on this? Here’s our code looks like:

Public Sub SetSecurityAndSave(fileName As String, password As String, printingAllowed As Boolean, contentModifyingAllowed As Boolean, copyingAllowed As Boolean, contentsModifyingAllowed As Boolean,

  •                      annotationsModifyingAllowed As Boolean, formFillingAllowed As Boolean, screenReadersAllowed As Boolean, documentAssemblyingAllowed As Boolean)*
    
  •    Dim privilege As DocumentPrivilege = DocumentPrivilege.ForbidAll*
    
  •    With privilege*
    
  •        .AllowPrint = printingAllowed*
    
  •        .PrintAllowLevel = 1*
    
  •        .AllowModifyContents = contentModifyingAllowed*
    
  •        .AllowCopy = copyingAllowed*
    
  •        .AllowModifyContents = contentModifyingAllowed*
    
  •        .AllowFillIn = formFillingAllowed*
    
  •        .AllowScreenReaders = screenReadersAllowed*
    
  •        .AllowAssembly = documentAssemblyingAllowed*
    
  •    End With*
    
  •    'open PDF document*
    
  •    Dim fileSecurity As New PdfFileSecurity(doc)*
    
  •    'set document privileges*
    
  •    If Not String.IsNullOrEmpty(password) Then*
    
  •        Try*
    
  •            fileSecurity.EncryptFile(password, "deltek", privilege, KeySize.x128)*
    
  •        Catch ex As Exception*
    
  •            Throw New Framework.Errors.Common.FrameworkException("fileSecurity.EncryptFile: " & ex.Message)*
    
  •        End Try*
    
  •    Else*
    
  •        Try*
    
  •            fileSecurity.SetPrivilege(privilege)*
    
  •        Catch ex As Exception*
    
  •            Throw New Framework.Errors.Common.FrameworkException("fileSecurity.SetPrivilege: " & ex.Message)*
    
  •        End Try*
    
  •    End If*
    
  •    Try*
    
  •        fileSecurity.Save(fileName)*
    
  •    Catch ex As Aspose.Pdf.Exceptions.InvalidPasswordException*
    
  •        Throw New Framework.Errors.Common.FrameworkException("Aspose.Pdf.Exceptions.InvalidPasswordException: " & ex.Message)*
    
  •    Catch ex As Exception*
    
  •        Throw New Framework.Errors.Common.FrameworkException("fileSecurity.Save: " & ex.Message)*
    
  •    End Try*
    
  • End Sub*

The exception happens when it calls the filesecurity.save() and it throws the error “Invalid Password”.

Class:
___. Ť ___

Function:
Initialize
FrameworkException:
Invalid password

Here are the values passed in the method: printingAllowed = true, screenReadersAllowed =true, eveything else false. Password is simple, something like ‘11111998’. We even tried ‘foobar’ and it still throws the same error.

We also have this in the code but the error is not trapped here but on the Throw New Framework.Errors.Common.FrameworkException("fileSecurity.Save: " & ex.Message)

Catch ex As Aspose.Pdf.Exceptions.InvalidPasswordException

  •        Throw New Framework.Errors.Common.FrameworkException("Aspose.Pdf.Exceptions.InvalidPasswordException: " & ex.Message)*
    

We are using Aspose.PDF version 10.6.

@junec,

Thanks for contacting support.

Can you please share source file along with environment details to further investigate this issue. Also please share version details as well.

Hi Adnan.Ahmad,

Please see my previous post. I already shared the code on how we call the method and the version details as well. Let me know if you have further questions.

Thanks,
June

Here I am requesting source PDF file which you used on your end to further investigate issue. Can you please share that file.

Payroll_Remittance_0000001-ReportServer13.pdf (5.1 KB)
Payroll_Remittance_0000001-ReportServer15.pdf (5.1 KB)

Please see the attached PDF files.

@junec,

I like to inform that I have worked with source file and unable to observe the issue. Also you are using very old version of Aspose.PDF. Can you please try to use latest version 20.3 and if there is still an issue than please share working sample project along with complete environment details.

What version did you use in testing, Adnan?

@junec,

I have used Aspose.PDF 20.3 on my end and you are using very old version.