FlattenAllFields gives me Object reference not set to an instance of an object error

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;
//}
}

Hi Jinisha,

Thanks for using our products.

Please share the PDF document causing this problem so that we can test the scenario at our end. We are sorry for this inconvenience.

Just to give you more info...earlier we used Aspose.Pdf.Kit and with Aspose.Pdf.Kit everything worked fine in .Net 3.5 version of the project using Aspose.Pdf.Kit.dll.

Since Aspose.Pdf.Kit is old, I changed it with Aspose.Pdf.Facades.Form version 8.1.0.0 Using that version the fill part is working fine but preview is not working because of FlattenAllFields line. For the pdf I sent you...I got the following error "Some fields are malformed, and may not work as expected. Please contact the author for a fix". For others, the pdf is pulling fine...its showing up as flattened however the filled data didnt' display for some reasons so I seemed like it removed the filled values completely.

Since I was having issues in .Net 3.5...I converted the app to .Net framework 4.0. removed Aspose.pdf.Kit.dll and added Aspose.Pdf 8.1.0.0 version via References. BTW, I had the same problem with 8.0.0.0 version of Aspose.Pdf too. If I comment the FlattenAllFields line...it pulls the filled pdf correctly...the form fields are not flattened and the values are showing up fine. As soon as I add that line back in. I get an error "Object reference not set to an instance of an object".

Please advise!

Thanks!

Jinisha

BTW, here is the View portion of the code

public partial class ViewFilingPreview : System.Web.UI.Page {

protected void Page_Load(object sender, EventArgs e) {
string pdfFile = Util.GetTempUserUploadDirectory();
if (!pdfFile.EndsWith("\")) pdfFile += “\”;
string ext = Request.QueryString[“Ext”];
if (String.IsNullOrEmpty(ext)) ext = “PDF”;
ext = “.” + ext;
pdfFile += Request.QueryString[“file”] + ext;
byte[] b;
// make it a read-only file for viewing.
using (MemoryStream msviewpdf = PDFProcessing.MergePDFAndXML(pdfFile, string.Empty, true)) {
b = new byte[msviewpdf.Length];
msviewpdf.Position = 0;
msviewpdf.Read(b, 0, (int)msviewpdf.Length);
msviewpdf.Close();
}
System.Web.HttpContext.Current.Response.Charset = null;
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.ContentType = “application/pdf”;
System.Web.HttpContext.Current.Response.AddHeader(“Content-Type”, “application/pdf”);
System.Web.HttpContext.Current.Response.AddHeader(“Content-Disposition”, “inline;filename=” + System.IO.Path.GetFileName(pdfFile));
System.Web.HttpContext.Current.Response.BinaryWrite(b);
Response.End();
}
}

Hi Jinisha,

Thanks for sharing the resource file.

I have tested the scenario where I have simply called FlattenAllFields() method using Aspose.Pdf for .NET in Visual Studio 2010 application where I have set the target platform of application as .NET Framework 4.0 and I am unable to notice any problem. I have used the following code snippet to perform the operation. I think the problem occurs once form fields are filled with data. Can you please share other resource files (which you are using to fill data inside form) so that we can again test the scenario at our end. We are sorry for this inconvenience.

[C#]

//create a pdf form…bind pdf using
source filename

Aspose.Pdf.Facades.Form pdfForm = new Aspose.Pdf.Facades.Form();

// bind the source PDF file

pdfForm.BindPdf("c:/pdftest/DE -101 (I) Intestate(no Will)Application.pdf");

// flatten form fields

pdfForm.FlattenAllFields();

// save update PDF file

pdfForm.Save(“c:/pdftest/FlattenField.pdf”);<!–[if gte mso 9]>
<w:WordDocument>
<w:View>Normal</w:View>
<w:Zoom>0</w:Zoom>
<w:TrackMoves/>
<w:TrackFormatting/>
<w:PunctuationKerning/>
<w:ValidateAgainstSchemas/>
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
<w:IgnoreMixedContent>false</w:IgnoreMixedContent>
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:DoNotPromoteQF/>
<w:LidThemeOther>EN-US</w:LidThemeOther>
<w:LidThemeAsian>X-NONE</w:LidThemeAsian>
<w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript>
<w:Compatibility>
<w:BreakWrappedTables/>
<w:SnapToGridInCell/>
<w:WrapTextWithPunct/>
<w:UseAsianBreakRules/>
<w:DontGrowAutofit/>
<w:SplitPgBreakAndParaMark/>
<w:EnableOpenTypeKerning/>
<w:DontFlipMirrorIndents/>
<w:OverrideTableStyleHps/>
</w:Compatibility>
<m:mathPr>
<m:mathFont m:val=“Cambria Math”/>
<m:brkBin m:val=“before”/>
<m:brkBinSub m:val="–"/>
<m:smallFrac m:val=“off”/>
<m:dispDef/>
<m:lMargin m:val=“0”/>
<m:rMargin m:val=“0”/>
<m:defJc m:val=“centerGroup”/>
<m:wrapIndent m:val=“1440”/>
<m:intLim m:val=“subSup”/>
<m:naryLim m:val=“undOvr”/>
</m:mathPr></w:WordDocument>
<![endif]–><!–[if gte mso 10]>

/* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin-top:0in; mso-para-margin-right:0in; mso-para-margin-bottom:10.0pt; mso-para-margin-left:0in; line-height:115%; mso-pagination:widow-orphan; font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin;}

<![endif]–>

I have emailed you the code but haven’t heard back from you.

Hi Jinisha,


I got the email in which you have shared the DE -101 (I) Intestate(no Will)Application.pdf file and as specified in my earlier post, I am unable to replicate the issue with my sample code. Can you please again try sending the code/project. We are sorry for this delay and inconvenience.