Free Support Forum - aspose.com

How do you encrypt PDF on the fly

This is what I’m doing to encrypt a pdf file:


FileStream input = new FileStream(filename, FileMode.Open, FileAccess.Read);
FileStream output = new FileStream(filename + “.aspose.enc.pdf”, FileMode.Create, FileAccess.Write);

PdfFileSecurity fileSecurity = new PdfFileSecurity();
fileSecurity.BindPdf(input);

fileSecurity.EncryptFile(“pass1”, “pass”, DocumentPrivilege.AllowAll, KeySize.x128, Algorithm.RC4);
fileSecurity.Save(output);

Problem is, when the function fileSecurity.EncryptFile gets called, the entire pdf document gets loaded into memory. And on the next line fileSecurity.Save, more memory gets used. So to encryption a 80MB file, using RC4-128bit will use up 155MB of memory. Using AES-128bit will use up 322MB of memory, which is even worse.

Is there a different way to do this so that the encryption is really done on the fly and not by loading the whole document into memory? I’m using PDF for .net version with dll file version 7.3.0.0, product version 2012.08.28

Hi Ed,


Thanks for using our products.

In order to encrypt the PDF document, Aspose.Pdf for .NET loads the entire PDF document but I am afraid it does not support the feature to load the fraction of PDF file and perform encryption. However I have observed that you are using an older release version. Can you please try using the latest release of Aspose.Pdf for .NET 7.9.0 and see if it provides some improvement in terms of performance.

In case you still notice the performance issue, please share the source PDF file so that we can test the scenario at our end. You may upload the file on some FTP and share the link address. We are sorry for this inconvenience.