We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

"Collection was modified" exception

Hi all,
I'm having an issue with Essential PDF for ASP.net, version 9.304.0.61
In my project, I have a class that generate a big PDF reading some data from entity framework, that sometimes return this exception:

System.InvalidOperationException: Collection was modified; enumeration operation may not execute.     at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)     at System.Collections.Generic.Dictionary`2.Enumerator.MoveNext()     at Syncfusion.Pdf.Graphics.Fonts.UnicodeTrueTypeFont.GetDescendantWidth()     at Syncfusion.Pdf.Graphics.Fonts.UnicodeTrueTypeFont.SetSymbols(String text)     at Syncfusion.Pdf.Graphics.PdfGraphics.ConvertToUnicode(String text, PdfTrueTypeFont ttfFont)     at Syncfusion.Pdf.Graphics.PdfGraphics.DrawUnicodeLine(LineInfo lineInfo, RectangleF layoutRectangle, PdfFont font, PdfStringFormat format)     at Syncfusion.Pdf.Graphics.PdfGraphics.DrawLayoutResult(PdfStringLayoutResult result, PdfFont font, PdfStringFormat format, RectangleF layoutRectangle)     at Syncfusion.Pdf.Graphics.PdfGraphics.DrawStringLayoutResult(PdfStringLayoutResult result, PdfFont font, PdfPen pen, PdfBrush brush, RectangleF layoutRectangle, PdfStringFormat format)     at Syncfusion.Pdf.Graphics.PdfGraphics.DrawString(String s, PdfFont font, PdfPen pen, PdfBrush brush, RectangleF layoutRectangle, PdfStringFormat format)     at ABB.Soc.Manager.PdfExtensions.AddFieldTabbed(PdfPage page, String label, String value, Int32 columns, SizeF size, PointF& currentPoint) in c:\Projects\it-tfsapp1\SOC2\SOC2-Dev\ABB.Soc.Manager\PdfHelper.cs:line 1375     at ABB.Soc.Manager.PdfHelper.SocTableToPdfDoc(User user, Int32 tableRevisionId, PdfDocument& doc) in c:\Projects\it-tfsapp1\SOC2\SOC2-Dev\ABB.Soc.Manager\PdfHelper.cs:line 450  

The method that create the PDF is launched on a separated task through Task.Factory.StartNew(...), and generate exception only when launched more then one time, where one of the task (seams a random one, not first or last) finish succesfully and all other have the exception.

This is the method that generate the error:

        public static void AddFieldTabbed(this PdfPage page, string label, string value, int columns, SizeF size, ref PointF currentPoint)
        {
            try
            {
                page.Graphics.DrawString(label, PdfStyles.LabelHeaderFont, PdfStyles.StandardBrush, currentPoint);
                float w = PdfStyles.LabelHeaderFont.MeasureString(label).Width;
                currentPoint.X += w;
                page.Graphics.DrawString(value, PdfStyles.StandardFont, PdfStyles.StandardBrush, currentPoint);
                currentPoint.X -= w;

                currentPoint.X += size.Width / columns;
                if (currentPoint.X >= size.Width)
                {
                    currentPoint.X = 0;
                    currentPoint.Y += PdfStyles.StandardFont.Height + (PdfStyles.Margin * 2);
                }
            }
            catch (Exception ex)
            {
                throw new Soc2Exception("Unexpected error on module Soc2Manager.", System.Reflection.MethodBase.GetCurrentMethod(), ErrorType.Error, ex);
            }
        }

That is just a my extension used for simplicity.
PdfStyles is static class (does never change), LabelHeaderFont is a PdfTrueTypeFont.

I'm spending hour about this without success, have someone experience with similar issue or now what's wrong with this?


14 Replies

PH Praveenkumar H Syncfusion Team November 11, 2013 12:15 PM UTC

Hi Diego,

 

Thank you for using Syncfusion Products.

 

When we create the pdf document in multithreading we need to make disable the following property to avoid reported exception.

 

PdfDocument.EnableCache = false;

 

The sample project is attached for your reference.

 

Pdf creation with MultiTask.zip

 

Please let us know if you need further assistance.

 

With Regards,

Praveen



DB Diego Beneggi November 22, 2013 05:17 PM UTC

Hi, 
thanks for the support, this work as expected, but now I have a new issue.
In a PDF creation that use html converter, the html render is done from html converter with compatibility view, that create issues on my page.
I have installed IE10, and using the meta tag
<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />
in my page, but printing the user agent on a span I have the correct one from browser

Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C; .NET4.0E; MS-RTC LM 8; EIE10;ENUSWOL)

and this one on PDF, that seams indicate a compatibility view forced on IE10

Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/6.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C; .NET4.0E; MS-RTC LM 8; EIE10;ENUSWOL)

there is a way to force using of lastest rendering engine from htmlconverter?



PH Praveenkumar H Syncfusion Team November 27, 2013 01:21 PM UTC

Hi Diego

Thank you for using syncfusion products,

To convert HTML to PDF we prefer you to use MSHTML(IE rendering machine).
Please find the online sample link below.
Meanwhile could you please let us know why your prefer Gecko than MSHTML.

Please let us know if you need further assistance.

With Regards,
Praveen


DB Diego Beneggi December 2, 2013 12:10 PM UTC

Hi,
i'm rewriting my anwser because was disappeared.
I Don't want to use Geko engine MSHTML is ok.
My problem is different, ans is that even if I have instaleld IE 10 on the server, when a PDF is created, is opend with IE7 emulator, that caus many issue with my scripts, i only whant to have IE 7 witouth compatibility, and know how to force syncfusuion html converter to do not use compatibility or emulation mode.


PH Praveenkumar H Syncfusion Team December 9, 2013 05:01 AM UTC

Hi Deigo,

Thank you for your update,

Html to pdf converter makes use of IE rendering engine(MSHTML), we are using mshtml.dll to convert the html to pdf. The output pdf document will look as it is viewed in the IE browser.

Pleas let us know if you need further information.

With Regards,
Praveen


DB Diego Beneggi December 9, 2013 11:05 AM UTC

Hi again,
in my case the render is not the same viewed directly in IE (on same machine, so sure same version), and the cause is that wehn i directly open the page, i have IE10, when pdf is printed, use IE10 with IE7 compatibility.
I've created a sample project to bettere describe the problem, there is a label that print the user agent, and you will see that is different when create PDF.
Hope someone can help me


WebApplication1_d06b90e8.zip


PH Praveenkumar H Syncfusion Team December 12, 2013 04:38 AM UTC

Hi Diego,

Thank you for creating the new incident for the query , Please follow the incident 116810 for further updates.

Please let us know if you need further assistance,

With Regards,
Praveen


ML Mikael Larsson October 12, 2015 02:01 PM UTC

Hi,

We are considering purchasing a license but we need to know that this works in a multi-threaded scenario.

i have tried setting 
PdfDocument.EnableCache = false;
As instructed in this thread but i still get an issue when doing a save
We are using the pdf library in a ASP.NET environment, .NET 4.5, syncfusion: 13.3450.0.7.
It only happens when there is concurrent requests.

Is there any documentation this EnableCache setting?

The exception we see:
   at System.Collections.Generic.Dictionary`2.Enumerator.MoveNext()
   at Syncfusion.Pdf.Graphics.Fonts.UnicodeTrueTypeFont.GetDescendantWidth()
   at Syncfusion.Pdf.Graphics.Fonts.UnicodeTrueTypeFont.DescendantFontBeginSave(Object sender, SavePdfPrimitiveEventArgs ars)
   at Syncfusion.Pdf.Primitives.PdfDictionary.OnBeginSave(SavePdfPrimitiveEventArgs args)
   at Syncfusion.Pdf.Primitives.PdfDictionary.Save(IPdfWriter writer, Boolean bRaiseEvent)
   at Syncfusion.Pdf.Primitives.PdfDictionary.Save(IPdfWriter writer)
   at Syncfusion.Pdf.IO.PdfArchiveStream.SaveObject(IPdfPrimitive obj, PdfReference reference)
   at Syncfusion.Pdf.IO.PdfCrossTable.DoArchiveObject(IPdfPrimitive obj, PdfReference reference, PdfWriter writer)
   at Syncfusion.Pdf.IO.PdfCrossTable.SaveIndirectObject(IPdfPrimitive obj, PdfWriter writer)
   at Syncfusion.Pdf.IO.PdfCrossTable.SaveObjects(PdfWriter writer)
   at Syncfusion.Pdf.IO.PdfCrossTable.Save(PdfWriter writer)
   at Syncfusion.Pdf.PdfDocument.Save(Stream stream)
   at Syncfusion.Pdf.PdfDocumentBase.Save(String filename)


KK Karthik Krishnaraj Syncfusion Team October 13, 2015 06:19 AM UTC

Hi Deigo,
I am afraid that I couldn’t able to reproduce the issue, I have attached a simple sample for your reference in the below link. Can you please share us the sample for which the issue reproduces so that it will be helpful for us to proceed further.
Sample Link:
http://www.syncfusion.com/downloads/support/forum/114218/ze/PdfFormASP353201096
Thanks,
Karthik.


ML Mikael Larsson October 13, 2015 02:18 PM UTC

Hi,

I have attached a sample to reproduce the issue:

also a screen of the issue.

Best Regards.

Attachment: Capture_904c123d.zip


ML Mikael Larsson October 13, 2015 02:19 PM UTC

Apparently only one file could be attatched.

Attachment: ConsoleApplication6_cbe02ac8.zip


KK Karthik Krishnaraj Syncfusion Team October 14, 2015 07:32 AM UTC

Hi Deigo

We were able to reproduce the problem and have logged defect report regarding this. A support incident to track the status of this defect has been created under your account. Please log on to our support website to check for further updates

https://www.syncfusion.com/account/login?ReturnUrl=%2fsupport%2fdirecttrac%2fincidents

Please let me know if you have any questions.


Thanks,

Karthik



ML Mikael Larsson October 14, 2015 08:56 AM UTC

Hi Thanks for looking at this.

My Name is not Diego or Deigo, and there was no incident created in my account.

Best Regards,
Mikael Larsson, (not Diego or Deigo or any other name, just plain Mikael is fine :))


KK Karthik Krishnaraj Syncfusion Team October 15, 2015 06:05 AM UTC

Hi Mikael,
Sorry for the inconvenience caused.
Could please check your account for the incident now?
Regards,
Karthik



Loader.
Live Chat Icon For mobile
Up arrow icon