Hi There,
Currently, I am using an evaluation version of aspose.word trying to insert data Model instance to my word document template save as PDF. Through breakpoints, i can see my data in xml and dataset. but once it generates PDF, it shows fields with blanks. i am not sure which step i did wrong or it is the evaluation version problem. Here is my code below. Thank you for helping.
// fetch data to template
public Document createTemplate()
{
Document doc;
doc = new Document(HttpContext.Current.Server.MapPath("~/Content/TestTemplate.docx"));
var serializer = new XmlSerializer(typeof(ReportModel));
var stream = new MemoryStream();
serializer.Serialize(stream, Report1); // Report 1 is an instance of ReportModel with full filled data
using(StreamReader reader = new StreamReader(stream,Encoding.UTF8))
{
stream.Position = 0 ;
var xml = reader.ReadToEnd();
DataSet ds = new DataSet();
ds.ReadXml(new StringReader(xml)); /// i can see report1 data in “xml”
/// after ds read xml , report1 data is still available to see
Doc.MailMerge.UseNonMergeFields= true;
Doc.MailMerge.CleanupOption = MailMergeCleanupOptions.RemoveUnusedFields;
Doc.MailMerge.ExecuteWithRegions(Ds);
}
return Doc;
}
/// Controller Side
public ActionResult Report(int reportID)
{
/// some logic get report1 instance
Document outputDc = createTemplate();
MemoryStream outStream =new MemoryStream();
outputDc.Save(outStream,SaveFormat.Pdf);
outStream.Position = 0;
return File(outStream,“application/pdf”);
}
///Word Template
{{TableStart:ReportModel}}
<< field1>><< field2>><< field3>>
{{TableEnd:ReportModel}}
/// After pdf return, I got the document with data fields of blanks , nothing is showing except evaluation water mark