Free Support Forum -

Word to PDF Example?


I’m trying out your Word and PDF components and couldn’t find an example of using the MailMerge function to generate a word doc from a word template, then save it as a PDF.

I am able to save it as a doc but can’t convert it to a PDF format. I know I need to pass it to your PDF Component but some code examples would be great.

Thanks in advance.


Hi Tim,

Here is an example


Thanks for the quick reply


Is there anyway to save it straight into a steram to the PDF object so we don’t need rely on saving to disk? There are times where users will have have write permissions.

Thanks again.


You can save to a memory stream and then read from it.

Here is another example from the demo included with the product:


/// Stream the document to the client browser in PDF format.

internal static void SendToBrowserAsPdf(Document doc, HttpResponse response)
//Save the document in Aspose.Pdf.Xml format into a memory stream.
MemoryStream stream = new MemoryStream();
doc.Save(stream, SaveFormat.FormatAsposePdf);
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();
pdf.BindXML(xmlDoc, null);

//Now produce the PDF file.
pdf.Save(“Aspose.Word.Demo.pdf”, Aspose.Pdf.SaveType.OpenInBrowser, response);

But note, that if the document contains images, it will still save them into disk files into Windows temporary folder. There is no other way we can pass images at the moment. We are working to make DOC to PDF conversion more straightforward, without going through a stream and XML, but we don’t want to make Aspose.Word dependent on Aspose.Pdf so we are trying to avoid direct calls, hence you need to go through XML and stream or a file.