Free Support Forum -

FileProcessingStrategy.OptimizeMemoryUsage causes corruption during ChangePassword

We are using Aspose.Pdf.Kit version I am attempting to add a password to a pdf in a memory stream with the following code. However, if I am also using Settings.Strategy = FileProcessingStrategy.OptimizeMemoryUsage, then the file that is created contains garbage.


string inputFileName = @"e:\pdffiles\testinput.pdf";

string outputFileName = @"e:\pdffiles\testoutput.pdf";

string password = "DanLee";

MemoryStream inputStream = new MemoryStream(File.ReadAllBytes(inputFileName));

Settings.Strategy = FileProcessingStrategy.OptimizeMemoryUsage;

MemoryStream encryptedStream = new MemoryStream();

PdfFileSecurity s = new PdfFileSecurity(inputStream, encryptedStream);

s.ChangePassword("", password, password);

if (File.Exists(outputFileName))


File.WriteAllBytes(outputFileName, encryptedStream.ToArray());


I have attached both the input and output files.

If I change this code by removing the Settings.Strategy = statement then the resultant pdf is ok. This is not an option for me. Because the Settings class is global I cannot change the FileProcessingStrategy because it would change the behavior of all the other threads in this process.

Can you suggest a work around that would allow me to add a password to a pdf stream while still keeping my OptimizeMemoryUsage settings?


Hi Dan,

Sorry for replying you late. We’re investigating this issue at our end and you’ll be updated with the results shortly.

We’re sorry for the inconvenience.