FlattenAllFields gives me Object reference not set to an instance of an object error
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 123: }
Line 124: editor.Save(msOut);
Line 125: pdfForm.FlattenAllFields();
Line 126: pdfForm.Save(msOut);
Line 127: //MemoryStream msOut2 = new MemoryStream();
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
Aspose.Pdf.InteractiveFeatures.Annotations.Annotation.( , ) +764
Aspose.Pdf.Page.(Boolean ) +320
Aspose.Pdf.Document.() +159
Aspose.Pdf.Document.Flatten() +77
Aspose.Pdf.Facades.Form.FlattenAllFields() +63
ez_filing.PDFProcessing.MergePDFAndXML(String sourceFileName, String xml, Boolean makeReadOnly, String extraSubmitURLParms) in C:\Users\jvakharia\Documents\Visual Studio 2010\Projects\EZFiling\EZ-Filing\ez-filing\PDFProcessing.cs:125
ez_filing.PDFProcessing.MergePDFAndXML(String sourceFileName, String xml, Boolean makeReadOnly) in C:\Users\jvakharia\Documents\Visual Studio 2010\Projects\EZFiling\EZ-Filing\ez-filing\PDFProcessing.cs:19
ez_filing.ViewFilingPreview.Page_Load(Object sender, EventArgs e) in C:\Users\jvakharia\Documents\Visual Studio 2010\Projects\EZFiling\EZ-Filing\ez-filing\ViewFilingPreview.aspx.cs:20
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
System.Web.UI.Control.OnLoad(EventArgs e) +91
System.Web.UI.Control.LoadRecursive() +74
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272
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();
//xDocument.Load(RdrObjXDoc);
//xDocument.Save(msXML);
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);
//editor.Document.Flatten();
editor.Save(msOut);
//pdfForm.FlattenAllFields();
pdfForm.Save(msOut);
//Document pdfdoc = new Document(msOut);
//pdfdoc.Flatten();
//pdfdoc.Save(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;
//}
}