We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Upgraded and now getting an error

So I’m looking for help. Just upgraded to the latest version and now I’m getting an error when I go to save my PDF.


Aspose.Pdf.Document form = this.affordableFormService.GetForm(accountID, pdfName, certificationID, System.Web.HttpContext.Current.Server.MapPath("~/Content/forms/"), complianceVerification, new ResManSimpleInjector(accountID));

if (form != null)
{
string contentType = “application/pdf”;
MemoryStream _ms = new MemoryStream();
form.Save(_ms, Aspose.Pdf.SaveFormat.Pdf); <- here is where error is caused.
byte[] bytes = _ms.ToArray();
_ms.Flush();
_ms.Close();
return File(bytes, contentType);
}


The error is

An exception of type ‘System.ArgumentNullException’ occurred in mscorlib.dll but was not handled in user code

Additional information: Value cannot be null.

at System.Collections.Generic.Dictionary2.FindEntry(TKey key)</div><div> at System.Collections.Generic.Dictionary2.ContainsKey(TKey key)
at . (String )
at . ​ (String )
at Aspose.Pdf.XFormCollection. (String )
at Aspose.Pdf.Artifact. ()
at Aspose.Pdf.Artifact…ctor(ArtifactCollection , , List1 , )</div><div> at Aspose.Pdf.HeaderArtifact..ctor(ArtifactCollection , , List1 , )
at Aspose.Pdf.ArtifactCollection. ( , List1 , )</div><div> at Aspose.Pdf.ArtifactCollection. (Matrix , OperatorCollection , Resources , List1 )
at Aspose.Pdf.ArtifactCollection. ()
at Aspose.Pdf.ArtifactCollection.Add(Artifact artifact)
at Aspose.Pdf.Page. (Page )
at Aspose.Pdf.Page. (Page )
at Aspose.Pdf.Page. (Page )
at Aspose.Pdf.Page. ()
at Aspose.Pdf.Document.ProcessParagraphs()
at Aspose.Pdf.Document.Save(Stream output)
at Aspose.Pdf.Document.Save(Stream outputStream, SaveOptions options)
at Aspose.Pdf.Document.Save(Stream outputStream, SaveFormat format)
at ResMan.Web.Controllers.CertificationsController.PrintForm(Guid certificationID, String pdfName) in c:\Workspaces\2017-03\ResMan.Web\Controllers\CertificationsController.cs:line 923
at ResMan.Web.Controllers.CertificationsController.SelectFormForPrint(FormSelectPrintViewModel model) in c:\Workspaces\2017-03\ResMan.Web\Controllers\CertificationsController.cs:line 909
at lambda_method(Closure , ControllerBase , Object[] )
at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary2 parameters)</div><div> at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary2 parameters)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass42.b__41()
at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass81.<BeginSynchronous>b__7(IAsyncResult _)</div><div> at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult1.End()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<>c__DisplayClass39.b__33()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass4f.b__49()


I’ve seem to narrowed it down to this block of the PDF.

foreach (WidgetAnnotation ann in page.Annotations)
{
Aspose.Pdf.Drawing.Graph graph3 = new Aspose.Pdf.Drawing.Graph(100, 400);
// Add graph object to paragraphs collection of page instance
page.Paragraphs.Add(graph3);
// Create Rectangle instance
Aspose.Pdf.Drawing.Circle rect3 = new Aspose.Pdf.Drawing.Circle((float)ann.Rect.LLX - 82, (float)ann.Rect.LLY - 315, (float)ann.Rect.Height / 2);
// Add rectangle object to shapes collection of Graph object
graph3.Shapes.Add(rect3);

}

Because if I skip this block it works fine. But when this section is included on the PDF generation I get the error when I save.

Hi There,


Thanks for contacting support.

I have tried to process a PDF document for testing purpose using the code snippet which you have shared. Though I was unable to observe any issue. We will really appreciate if you please share a sample project along with input file so that we can try to reproduce the issue in our environment and get back to you with some definite updates accordingly.

We are sorry for the inconvenience.

Best Regards,

attached is the doc.



public ActionResult Index()
{
var pathToFile = System.Web.HttpContext.Current.Server.MapPath("~/Docs/") + “Texas Tenant Income Certification.pdf”;
Aspose.Pdf.Document crpForm = new Aspose.Pdf.Document(pathToFile);
Aspose.Pdf.Document document = new Aspose.Pdf.Document();

foreach (Aspose.Pdf.Page p in crpForm.Pages)
{
Aspose.Pdf.Page page = document.Pages.Add§;
// loop over each annotation and fill form out
foreach (WidgetAnnotation ann in page.Annotations)
{
var title = ann.FullName;
var indexOfNum = title.IndexOf(".");
if (indexOfNum < 0)
{
indexOfNum = title.IndexOf("#");
}
var num = 0;
if (indexOfNum >= 0)
{
num = Convert.ToInt32(title.Substring(indexOfNum + 1));
title = title.Substring(0, indexOfNum);
}
switch (title)
{
case “NAStudent”:
Aspose.Pdf.Drawing.Graph graph3 = new Aspose.Pdf.Drawing.Graph(100, 400);
// Add graph object to paragraphs collection of page instance
page.Paragraphs.Add(graph3);
// Create Rectangle instance
Aspose.Pdf.Drawing.Circle rect3 = new Aspose.Pdf.Drawing.Circle((float)ann.Rect.LLX - 82, (float)ann.Rect.LLY - 315, (float)ann.Rect.Height / 2);
// Add rectangle object to shapes collection of Graph object
graph3.Shapes.Add(rect3);
break;
}
var s = “”;
TextBuilder builder = new TextBuilder(page);
TextParagraph paragraph = new TextParagraph();
// specify the location to add TextParagraph
paragraph.Rectangle = ann.Rect;
paragraph.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center;
// specify word wraping mode
paragraph.FormattingOptions.WrapMode = TextFormattingOptions.WordWrapMode.ByWords;
// create text fragment
TextFragment fragment1 = new TextFragment(s);
fragment1.TextState.Font = FontRepository.FindFont(“Helvetica”);
fragment1.TextState.FontSize = 8;
fragment1.TextState.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center;
// add fragment to paragraph
paragraph.AppendLine(fragment1);
// add paragraph
builder.AppendParagraph(paragraph);
}
}

// clear annotations
foreach (Aspose.Pdf.Page p in document.Pages)
{
p.Annotations.Delete();
}
Aspose.Pdf.Document form = document;

string contentType = “application/pdf”;
MemoryStream _ms = new MemoryStream();
form.Save(_ms, Aspose.Pdf.SaveFormat.Pdf);
byte[] bytes = _ms.ToArray();
_ms.Flush();
_ms.Close();
return File(bytes, contentType);
}


When I run this code I get the error.

Hi There,


Thanks for sharing document and sample project. I tried the whole scenario at my end and was able to reproduce the issue which you have reported. There I have logged the issue as PDFNET-42341 in our issue tracking system. We will further look into the details and keep you updated on the status. Please be patient and spare us a little time.

We are sorry for the inconvenience.


Best Regards,

So I need to be able to do this within 3 weeks. In case this isn’t resolved by then, is there a work around or something to be able to accomplish the goal of circling something on a PDF?

Hi There,


Thanks for your patience. I have tested the whole scenario again with incremental approach and observed that the issue was occurring because of adding Graph class on the same page in inner foreach iteration. I have moved the code of adding Graph to the page level and it generated the output with circles around the text.

Please check the following modified code snippet to generate PDF with the circles around text. I have also highlighted the part in the code snippet for adding graph inside the page and then add different shapes (i.e circles) into the Shape collection of the Graph.

Aspose.Pdf.Document crpForm = new Aspose.Pdf.Document(dataDir + “Texas+Tenant+Income+Certification.pdf”);<o:p></o:p>

Aspose.Pdf.Document document = new Aspose.Pdf.Document();

foreach (Aspose.Pdf.Page p in crpForm.Pages)

{

Aspose.Pdf.Page page = document.Pages.Add(p);

Aspose.Pdf.Drawing.Graph graph3 = new Aspose.Pdf.Drawing.Graph(100, 400);

// Add graph object to paragraphs collection of page instance

page.Paragraphs.Add(graph3);

// loop over each annotation and fill form out

foreach (WidgetAnnotation ann in page.Annotations)

{

var title = ann.FullName;

var indexOfNum = title.IndexOf(".");

if (indexOfNum < 0)

{

indexOfNum = title.IndexOf("#");

}

var num = 0;

if (indexOfNum >= 0)

{

num = Convert.ToInt32(title.Substring(indexOfNum + 1));

title = title.Substring(0, indexOfNum);

}

switch (title)

{

case "NAStudent":

// Create Rectangle instance

Aspose.Pdf.Drawing.Circle rect3 = new Aspose.Pdf.Drawing.Circle((float)ann.Rect.LLX - 82, (float)ann.Rect.LLY - 315, (float)ann.Rect.Height / 2);

// Add rectangle object to shapes collection of Graph object

graph3.Shapes.Add(rect3);

break;

}

var s = "";

TextBuilder builder = new TextBuilder(page);

TextParagraph paragraph = new TextParagraph();

// specify the location to add TextParagraph

paragraph.Rectangle = ann.Rect;

paragraph.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center;

// specify word wraping mode

paragraph.FormattingOptions.WrapMode = TextFormattingOptions.WordWrapMode.ByWords;

// create text fragment

TextFragment fragment1 = new TextFragment(s);

fragment1.TextState.Font = FontRepository.FindFont("Helvetica");

fragment1.TextState.FontSize = 8;

fragment1.TextState.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center;

// add fragment to paragraph

paragraph.AppendLine(fragment1);

// add paragraph

builder.AppendParagraph(paragraph);

}

}

// clear annotations

foreach (Aspose.Pdf.Page p in document.Pages)

{

p.Annotations.Delete();

}

Aspose.Pdf.Document form = document;

string contentType = "application/pdf";

MemoryStream _ms = new MemoryStream();

form.Save(_ms, Aspose.Pdf.SaveFormat.Pdf);

byte[] bytes = _ms.ToArray();

_ms.Flush();

_ms.Close();

return File(bytes, contentType);


I have also attached the output file generated by above code for your reference. In can if you need any further assistance please feel free to contact us.


Best Regards,