Applying pdf Encryption and/or privileges without saving into a new file

Hi Aspose Tech Support,

I am just wondering, how do you apply pdf encryption and/or privileges to an opened pdf file?

This is a simple code to convert doc to pdf , but I would like to apply privileges to the pdf file without closing the pdf file, could I do that?

_____________________________________________

Dim doc As Document = New Document(".\\Test.doc")

Dim stream As MemoryStream = New MemoryStream()

doc.Save(stream, SaveFormat.FormatAsposePdf)

stream.Seek(0, SeekOrigin.Begin)

Dim xmlDoc As XmlDocument = New XmlDocument()

xmlDoc.Load(stream)

Dim pdf As Aspose.Pdf.Pdf = New Aspose.Pdf.Pdf()

pdf.IsImagesInXmlDeleteNeeded = True

pdf.BindXML(xmlDoc, Nothing)

pdf.IsTruetypeFontMapCached = True

pdf.TruetypeFontMapPath = Path.GetTempPath()

pdf.Save(".\\Test.pdf")

_____________________________________________

the pdf.kit example to apply encryption or privileges from the Aspose website is as follow,

_____________________________________________

Dim inFile As String = ".\\Test.pdf"

Dim outFile As String = ".\\Test_encrypted.pdf"

Dim fileSecurity As PdfFileSecurity = New PdfFileSecurity(inFile, outFile)

fileSecurity.EncryptFile("userpassword", "ownerpassword", PdfPrivilege.Copy Or PdfPrivilege.Print, False)

_____________________________________________

the problem is, there is additional process to create a new pdf file, I just want to avoid this process.

Thank you.

Regards,

Jarry Wijaya

Thanks for considering Aspose.

I just do a little modification of your codes shown below:

_____________________________________________

Dim doc As Document = New Document(".\\Test.doc")

Dim stream As MemoryStream = New MemoryStream()

doc.Save(stream, SaveFormat.FormatAsposePdf)

stream.Seek(0, SeekOrigin.Begin)

Dim xmlDoc As XmlDocument = New XmlDocument()

xmlDoc.Load(stream)

Dim pdf As Aspose.Pdf.Pdf = New Aspose.Pdf.Pdf()

pdf.IsImagesInXmlDeleteNeeded = True

pdf.BindXML(xmlDoc, Nothing)

pdf.IsTruetypeFontMapCached = True

pdf.TruetypeFontMapPath = Path.GetTempPath()

' Save the result in MemoryStream objects

Dim memStream As System.IO.MemoryStream= New System.IO.MemoryStream

Dim outputStream As System.IO.MemoryStream= New System.IO.MemoryStream

pdf.Save(memStream)

memStream.Seek(0, SeekOrigin.Begin)

Dim fileSecurity As PdfFileSecurity = New PdfFileSecurity(memStream, outputStream)

fileSecurity.EncryptFile("userpassword", "ownerpassword", PdfPrivilege.Copy Or PdfPrivilege.Print, False)

_____________________________________________

Hi Kevin,

I have got these errors at line: "fileSecurity.EncryptFile("userpassword", "ownerpassword", PdfPrivilege.Copy Or PdfPrivilege.Print, False)"

a. "Invalid pdf format:pdf head signature is not found!"

b. "A first chance exception of type 'System.IO.IOException' occurred in Aspose.Pdf.Kit.dll"

any clue?

Another question,

1. When calling pdf.save function, do we need to call "pdf.Save(outputStream)" after the Encription? because I need the final PDF file with Encryption.

Thank you.

Regards,

Jarry Wijaya

I have updated the codes above by adding this line

memStream.Seek(0, SeekOrigin.Begin)

please try again.

In addition, you should call "pdf.Save(outputStream)" before do Encription.because this method is to save result into a memory object which acts as a parameter needed by new a PdfFileSecurity object.

Hi Kevin,

Thanks, it's working fine.

but, I am just wondering, how do I generate the final encrypted physical PDF file?

because this code doesn't generate pdf file.

regards,

Jarry Wijaya

Thanks Kevin,

I have found the solution.

Regards,

Jarry