We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Adding a MasterPassword or UserPassword to a PDF document that is a Memory Stream


I am trying to use Aspose.Pdf to add a password to a Pdf document that is a MemoryStream. The scenario is that we are using Aspose.Words to create a document, then save it as a MemoryStream in pdf format. I would then like to add a User and Master password to the document and send it as an email attachment. Could you please help? Note that I do not want to write the file to disk before sending it. I want to go directly from the MemoryStream to the email. Below is the basics of the code

Pseudo Code
// create file using Aspose.Words

// save file to memory stream as pdf
System.IO.MemoryStream msDocument = new System.IO.MemoryStream();
dbOutput.Document.Save(msDocument, SaveFormat.Pdf);

// if I send as an email attachment here, no issues

// try to add the password
Aspose.Pdf.Pdf pdf = new Aspose.Pdf.Pdf(msDocument);
pdf.Security = new Aspose.Pdf.Security();
pdf.Security.MasterPassword = "1234";
pdf.Security.UserPassword = "5678";

// if I call save here, it says I am in direct to file mode
// if I call close here, it complains of a null reference exception
// if I do nothing, I am able to send the document but there is no password



Hello Scott,

Please try using the following code snippet to accomplish your requirements.


MemoryStream documentStream = new MemoryStream();
Document doc = new Document(@"D:\pdftest\Hello_World.doc");
// specify the folder to store the images to some temporary location
doc.SaveOptions.PdfExportImagesFolder = @"d:/pdftest/";
// Save the document in Aspose.Pdf.Xml format.
doc.Save(documentStream, SaveFormat.AsposePdf);

Pdf pdf = new Pdf();
pdf.Security = new Aspose.Pdf.Security();
pdf.Security.MasterPassword = "1234";
pdf.Security.UserPassword = "5678";
pdf.BindXML(documentStream, null);

I've also attached the source word document and the resultant PDF that I've generated. Please take a look.

In case it does not satisfy your requirement or you've any further query, please feel free to contact.

We apologize for your inconvenience.


I have tried your suggestion. I get an exception on the line
pdf.BindXML(documentStream, null);

The exception is:
Data at the root level is invalid. Line 1, position 1.

The source document is fairly complex. The sceanario is this. There are 1-n template documents that comprise the source document. A template document is read in, some token replacement happens, then that whole document is copied into the source document (for each of the 1-n template documents). I did print out the data in the stream (from the save of the Word doc in pdf format) before posting this question. As far as I could tell, it was not an XML document.



Hello Scott,

Once the data is updated in the template document, can you please share the sample word document that you're trying to convert, so that we can test the scenario at our end.

We apologize for your inconvenience.


Here are two versions of the output. test.pdf takes the stream and saves it. It does not open as a pdf. test1.pdf uses the word save function and works as a pdf. Sounds to me like the stream save is the issue. Remember that my goal is to have a pdf file as a stream only (no saves to disk) that is password protected.

Here is the code:

// saving the stream directly to disk as a pdf (this does not work)
System.IO.MemoryStream msDocumentNonSecure = new System.IO.MemoryStream();
dbOutput.Document.Save(msDocumentNonSecure, SaveFormat.Pdf);
System.IO.FileStream fs = new System.IO.FileStream("c:\\test.pdf", System.IO.FileMode.Create);
fs.Write(msDocumentNonSecure.ToArray(), 0, 1);

// calling the word save function without output pdf type (this works)
dbOutput.Document.Save("c:\\test1.pdf", SaveFormat.Pdf);


Hello Scott,

Sorry for replying you late.

As far as I can understand, the Test.Pdf is not displaying because you've used the FileStream object to save the PDF rather than Aspose.Pdf or any other API to create it. Concerning to your requirement on saving the resultant PDF document into Stream, you can try using the public void Save(Stream); overloaded method of Pdf class to accomplish your requirement.

As I've already requested, can you please try sharing the source word document, so that we can test the complete scenario at our end.

We apologize for your inconvenience.


Please see the attached Word document as requested.

I figured out the issue with writing the stream to disk. I was only writing out the first byte which is why the file did not open as a pdf.


Hello Scott,

Thanks for sharing the resource document.

I've tested the scenario using Aspose.Pdf for .NET 4.2.1 and Aspose.Words for .NET 8.0.0 and as per my observations, the password information is successfully been added to the resultant document. I've also attached the document, please take a look (the password information is same as shared in my earlier posts).

Can you please share which version of Aspose.Pdf for .NET you're using ?

We apologize for your inconvenience.

I updated my whole installation of Aspose before replying. I am still having the same issue.
Aspose.Words -
Aspsose.Pdf -

I did go on the site and notice that the individual download of Aspose.Pdf is at a higher version than the Aspose.Total package. I will update the Pdf install, try again, and get back to you.

Updated to Aspose.Pdf 4.1.2

Still having same issue.

Please see the attached zip file that has a small project that demonstrates the issue. Note that I did not put our license in the zip (even though it is referenced in the project).

Hello Scott,

Thanks for sharing the sample project.

After detailed investigation, we've been able to figure out the actual reason of the issue. Please update the following code line from dbOutput.Document.Save(msDocumentNonSecure, SaveFormat.Pdf); to dbOutput.Document.Save(msDocumentNonSecure, SaveFormat.AsposePdf);. Because, SaveFormat.Pdf is used to save the document directly into PDF format without utilizing Aspose.Pdf.

The resultant PDF that I've generated is in attachment, please take a look.

In case it does not satisfy your requirements, or you've any further query, please feel free to contact.

We apologize for your inconvenience.

That did the trick!

Thanks for your help.

Best Regards,

Scott Butler