public static MemoryStream MergePDFAndXML(string sourceFileName, string xml, bool makeReadOnly, string extraSubmitURLParms) {
System.IO.MemoryStream msOut = new MemoryStream();
//license info
Aspose.Pdf.License lic = new Aspose.Pdf.License();
lic.SetLicense(“Aspose.Total.lic”); //set aspose pdf kit license
//create a pdf form…bind pdf using source filename
Aspose.Pdf.Facades.Form pdfForm = new Aspose.Pdf.Facades.Form();
pdfForm.BindPdf(sourceFileName);
//if xml is not empty import xml into the pdf form and save the pdf form
if (!String.IsNullOrEmpty(xml)) {
var stringBytes = System.Text.Encoding.ASCII.GetBytes(xml);
MemoryStream msXML = new MemoryStream();
msXML.Write(stringBytes, 0, stringBytes.Length);
//StringReader srXDoc = new StringReader(xml);
//XmlTextReader RdrObjXDoc = new XmlTextReader(srXDoc);
//XmlDocument xDocument = new XmlDocument();
msXML.Position = 0;
pdfForm.ImportXml(msXML);
msXML.Close();
msXML.Dispose();
}
//create an array of fieldNames
string[] fields = pdfForm.FieldNames;
//save the source file to MemoryStream
pdfForm.Save(msOut);
//create a Formeditor - bind pdf to memorystream
Aspose.Pdf.Facades.FormEditor editor = new Aspose.Pdf.Facades.FormEditor();
editor.BindPdf(msOut);
//if read only i.e; for viewing purposes only then remove Buttons off of the pdf form
if (makeReadOnly) { //make pdf read only
foreach (string field in fields) {
if ((field.Contains(“Button”)) || (field.Contains(“Submit”))) { //remove submit button
editor.RemoveField(field);
}
}
editor.Save(msOut);
pdfForm.Save(msOut);
//Document pdfdoc = new Document(msOut);
} else { // loop thru all the fields, find push button and set url
foreach (string field in fields) {
if (pdfForm.GetFieldType(field) == Aspose.Pdf.Facades.FieldType.PushButton) { //find out if FieldType is PushButton
editor.SetSubmitUrl(field, GetFullApplicationPath() + “/IconPDF.aspx” + extraSubmitURLParms); //if so, save the name of the submit field
}
}
editor.Save(msOut);
}
editor.Close();
return msOut;
//}
}