Free Support Forum - aspose.com

ASP.NET Word2PDF w/MailMerge (w/o Visual Studio!) -- (Example Code)

Again, all tech support & online documents were pretty incomplete for developers who prefer to roll things themselves without the gigabytes of Visual Studio.NET clogging their systems. For anyone needing complete sample code here you go ...

Works easiest in ASP.NET v2.0 -- Copy Aspose.Words.dll & Aspose.Pdf.dll into a directory named "Bin" in your website directory. Then create your .aspx page and copy the following into it (update the variables at the top as needed - understand our example assumes a base filename and we play with the extensions from there ... .doc .xml .pdf).

=============================

<%@ Page Language="C#" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="Aspose.Words" %>
<%@ Import Namespace="Aspose.Pdf" %>

<%
string file = "c:\\inetpub\\wwwroot\\wordsamples\\mtest3";
string lic = "c:\\inetpub\\wwwroot\\wordsamples\\Aspose.Custom.lic";

// ===================================
// Set Controls Licensing
// ===================================
Aspose.Words.License license_w = new Aspose.Words.License();
license_w.SetLicense(lic);

Aspose.Pdf.License license_p = new Aspose.Pdf.License();
license_p.SetLicense(lic);

// ===================================
// Load Word Doc
// ===================================
Document doc = new Document(file + ".doc");

// ===================================
// MailMerge Data Fields
// ===================================
// *** MANUAL METHOD
//string[] fieldNames = {"Company", "EndNumber", "PolicyNumber", "EffectiveDate", "DateIssued", "CounterSignedDate"};
//object[] fieldValues = {"ABC Corporation of America", "3", "NAC12345", "10/31/2006", "10/28/2006", "10/30/2006"};
//doc.MailMerge.Execute(fieldNames, fieldValues);

// *** XML FILE METHOD
DataSet ds = new DataSet();
System.IO.FileStream fsReadXml = new System.IO.FileStream(file + "_data.xml", System.IO.FileMode.Open);
System.Xml.XmlTextReader myXmlReader = new System.Xml.XmlTextReader(fsReadXml);
ds.ReadXml(myXmlReader);
myXmlReader.Close();
doc.MailMerge.Execute(ds.Tables["header"]);

// ===================================
// Convert Word to PDF
// ===================================
doc.Save(file + ".xml", SaveFormat.FormatAsposePdf);
Aspose.Pdf.Pdf pdf = new Aspose.Pdf.Pdf();
pdf.IsImagesInXmlDeleteNeeded = true;

// ===================================
// OPTIONAL: Set PDF custom properties
// ===================================
pdf.Author = "Demo User";
pdf.Creator = "Demo System";
pdf.Keywords = "Hot Document";
pdf.Subject = "Report of Things";
pdf.Title = "Customer #123";

// ===================================
// OPTIONAL: Set PDF document security
// ===================================
Aspose.Pdf.Security sec = new Aspose.Pdf.Security();
sec.IsContentsModifyingAllowed = false;
pdf.Security = sec;

pdf.BindXML(file + ".xml", null);
pdf.Save(file + ".pdf");

// ===================================
// Cleanup
// ===================================
File.Delete(file + ".xml");
%>

Thank you for sharing. Greatly appreciated.