Free Support Forum -

Request for Example - Create the whole document process in memory

Dear Aspose team,

I think someone asked about this before but I cannot find the it anymore.

Here is the example that I want:

1) open the Word template

2) Perform MailMerge

3) Save to PDF

4) Append additiaonl PDF files to the current output

5) Set the Security on the PDF to Print only

6) Finally save the output to the physical file

Thanks for the help in advance.

The 1-3 is easy:

// Open document.

Document doc = new Document(filename);

// Execute MailMerge with the data source.


// Save in PDF.

MemoryStream stream = new MemoryStream();

doc.Save(stream, SaveFormat.FormatAsposePdf);

doc.Save(name + ".xml", SaveFormat.FormatAsposePdf);

//Seek to the beginning so it can be read by XmlDocument.

stream.Seek(0, SeekOrigin.Begin);

//Load the document into an XmlDocument

XmlDocument xmlDoc = new XmlDocument();


//Load the XML document into Aspose.Pdf

Aspose.Pdf.Pdf pdf = new Aspose.Pdf.Pdf();

//Make sure the images that were saved by Aspose.Word into Windows temporary

//folder are automatically deleted by Aspose.Pdf when they are no longer needed.

pdf.IsImagesInXmlDeleteNeeded = true;

pdf.BindXML(xmlDoc, null);

pdf.IsTruetypeFontMapCached = true;

pdf.TruetypeFontMapPath = Path.GetTempPath();

//Now produce the PDF file.


For the rest of the example you should address PDF team.

I am moving this thread to Aspose.Pdf forum so that they could answer you properly.

Here is the answer to rest of questions:

//Append additiaonl PDF files to the current output.

PdfFileEditor pdfEditor = new PdfFileEditor();
int start = 1;
int end = 3;

//Set the Security on the PDF to Print only and Finally save the output to the physical file

string inFile = "outFile.pdf";
string outFile = "Result.pdf";
PdfFileSecurity fileSecurity = new PdfFileSecurity(inFile,outFile);

Dear Aspsose team,

Thanks for the help and I still have some questions.

First, can I not perform the pdf.Save(pdffilename); and go directly to PdfFileEditor pdfEditor = new PdfFileEditor();?

Second, can I do all the operation in pdfEditor (append and set security) in memoey?

We want to perform everything in memory because we want to reduce the intermediate files in between process and speed up the file creation.

Thanks in advance.


All the methods in Aspose.Pdf and Aspose.Pdf.Kit can process both disk file and memory stream. You can save the pdf into a memory stream and then use Aspose.Pdf.Kit to process the pdf stream. All these can be done in memory.

Any more question is wellcome.

Dear Tommy,

Could you please give us the example? Thanks in advance!

Here is the complete sample of your requirement.

//MemoryStream to store the result from Aspose.Words
MemoryStream inputms = new MemoryStream();
//MemoryStream to store the result after appended
MemoryStream outputms = new MemoryStream();

//Now save the result from Aspose.Words.

//Specify the beginning page and ending page
int start = 1;
int end = 3;

//A pdf needed to append
string ToAppend = "ToAppend.pdf";

//read input Pdf documents
FileStream fs = File.Open(ToAppend,FileMode.Open,FileAccess.Read,FileShare.Read);

Aspose.Pdf.Kit.PdfFileEditor pfe = new Aspose.Pdf.Kit.PdfFileEditor();


string resultFile = "Result.pdf";

FileStream fs = File.Open(resultFile,FileMode.Open,FileAccess.Read,FileShare.Read);

Aspose.Pdf.Kit.PdfFileSecurity pfs = new Aspose.Pdf.Kit.PdfFileSecurity(outputms,fs);


Thanks, I will give it a try now.