Free Support Forum - aspose.com

Assign user password to pdf in stream format

I have a pdf that is already in a MemoryStream object. I need to make the pdf protected with a user password before I email it out. I've tried a bunch of stuff and just can't seem to get it. Here's what I have so far, but this generates an emtpy pdf when I write it to a file.

(The last 4 lines are for debugging purposes only. I don't want to actually write this to a file, I need a memory stream).

Would you mind giving me some assistance?

Thanks

=

Aspose.Pdf.Pdf p = new Aspose.Pdf.Pdf(stream);

p.Security = new Aspose.Pdf.Security();

p.Security.UserPassword = password;

p.Close();

stream.Seek(0, SeekOrigin.Begin);

FileStream outStream = File.OpenWrite(@"e:\pdffiles\output.pdf");

stream.WriteTo(outStream);

outStream.Flush();

outStream.Close();

Hi Dan,

Thanks for using our products.

Please visit the following link for information on
Encrypt PDF File using Different Encryption Types and Algorithms

In case you encounter any issue or you have any further query, please feel free to contact. We apologize for your inconvenience.

PS, I have tested the scenario using Aspose.Pdf for .NET 6.1.0 and the features seems to be working properly

Hi. Thank you for the quick reply. We are using Aspose.Pdf version 5.3.0. In this version, there appears to be no such object as Aspose.Pdf.Document. Is this a new object, or am I totally lost? thanks.

Please supply me with some information about how to do this in version 5.3.0.

Thanks.

Hi Dan,

By the time when Aspose.Pdf for .NET 5.3.0 was released, we used to have two separate components Aspose.Pdf for .NET (for PDF generation) and Aspose.Pdf.Kit for .NET (for existing PDF documents manipulation/editing). But since the release of Aspose.Pdf for .NET 6.0.0, we this single component provides the capabilities to generate as well to manipulate/edit existing PDF documents. All the classes of earlier Aspose.Pdf for .NET have been moved under Aspose.Pdf.Generator namespace and the classes of Aspose.Pdf.Kit for .NET have been moved under Aspose.Pdf.Facades namespace. For more details, please visit
How to Use New Aspose.Pdf for .NET API

As per your requirement on adding password information to an existing PDF document loaded in memory, you need to try using Aspose.Pdf.Facades namespace of Aspose.Pdf for .NET. I am afraid the older release version 5.3.0 can not provide the feature to add password information to existing PDF document. Please visit the following link for information on
Encrypt PDF File using Different Encryption Types and Algorithms (Facades)

However if you are creating a PDF document from scratch and need to add password information, please follow the instructions specified over Set User or Master Password

I have attempted to use the following code to add a password using the Aspose.Pdf.Kit version 5.6.0. Both the input and output file are attached. The output file is, indeed, password protected now, but the contects are garbage. Can you confirm for me that this does not work in version 5.6.0 of Aspose.Pdf.Kit?

MemoryStream encryptedStream = new MemoryStream();

PdfFileSecurity s = new PdfFileSecurity(stream, encryptedStream);

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

I have attempted to use the Aspose.Pdf.Kit version 5.6.0 to add a password with the following code. The resultant pdf file is, indeed, password protected - but the contents of the file are garbage. I have attached both the input and output files.

Can you confirm for me that this is brokent in Aspose.Pdf.Kit version 5.6.0?

MemoryStream encryptedStream = new MemoryStream();

PdfFileSecurity s = new PdfFileSecurity(stream, encryptedStream);

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

Thank you for your help. After further testing this was determined to be my own coding issue.

Hello Dan,

Thanks for sharing the resource files and the code snippet.

I am glad to hear that your problem is resolved. However I have also tested the scenario and I am also unable to notice any issue. Please take a look over the attached resultant PDF document that I have generated. The password is new and owner.

In case you still face any problem or you have any further query, please feel free to contact. We apologize fo ryour incoinvenience.