Using Aspose Word with ASP.NET MVC3 to write PDF files

We want to take a word input template, add some data from SQL Server, and send the output to an ASP.NET MVC3 view using the razor engine. Does anyone have any advice on how to do this? Or a sample app with ASP.NET MVC 3.

Hi Graham,

Thanks for your query. It would be great, If you share some more information about your input and output document. Please share your input template. Also, read following documentation link for your kind reference.
https://docs.aspose.com/words/net/mail-merge-and-reporting/
https://docs.aspose.com/words/net/document-builder-overview/

Solved the problem it was:

myDoc = myCreateStatement.CreatePdfDocument(DatabaseName, intCaseID, intEnquiryID, connString);
MemoryStream outStream = new MemoryStream();
myDoc.Save(outStream, SaveFormat.Pdf);
byte[] docBytes = outStream.ToArray();
return File(docBytes,"application/pdf");

Works a treat!!

Just to clarifying you are returning a file from the ASP.NET MVC controller instead of a view…

Hi Graham,

Thanks for your inquiry. It is perfect that you managed to achieve what you were looking for. Secondly, I would like to supplement Tahir’s answer here i.e. you can write your final PDF directly to response stream just like in ASP.Net via HttpResponse class. Please try using the following overload of Save method:

Document.Save Method (HttpResponse, String, ContentDisposition, SaveOptions)

In addition, instead of HttpResponse the MVC uses HttpResponseBase class. The HttpResponseBase class is an abstract class that contains the same members as the HttpResponse class. The HttpResponseBase class enables you to create derived classes that are like the HttpResponse class, but that you can customize and that work outside the ASP.NET pipeline. When you perform unit testing, you typically use a derived class to implement members that have customized behavior that fulfills the scenario you are testing.

The HttpResponseWrapper class derives from the HttpResponseBase class. The HttpResponseWrapper class serves as a wrapper for the HttpResponse class. At run time, you typically use an instance of the HttpResponseWrapper class to call members of the HttpResponse object.

https://docs.microsoft.com/en-us/dotnet/api/system.web.httpresponsebase

If we can help you with anything else, please feel free to ask.

Best Regards,