Hello,
as you can probably guess i'm using MigraDoc to render some pdf's. Basicly i'm trying to render some (~300 - 500) reports that contains couple of tables each. Every table can have from 1 to let's say 5 rows (in avarage), besides tables there is of course some text.
My problem is that when i'm trying to render over 100 reports in one pdf document the stack overflow exception occures. I tried to find some reletive thread but I've ended up with finding only this one
click. There is not answer but I assume that someone struggled with similar problem.
Ok, let's get to the point. The exception is thrown when method
RenderDocument() is called. Last line in the snipped of code
Code:
Document document = CreateDocument(caseRepository.GetCasesReportInfo(clientId, caseStages));
document.UseCmykColor = true;
PdfDocumentRenderer renderer = new PdfDocumentRenderer(true, PdfSharp.Pdf.PdfFontEmbedding.Always);
renderer.Document = document;
renderer.RenderDocument();
It is thrown in random places, but mainly when enumerating something corelated with tables. Last time it stopped at this point (line 447 in PdfReferenceTable.cs)
Code:
if (pdfObject is PdfDictionary)
I don't know if it will help someone with judgment about my issue but anyway i will enclose this information. The call stack is full of this kind of entries.
Code:
PdfSharp.DLL!PdfSharp.Pdf.PdfReferenceTable.TransitiveClosureImplementation(System.Collections.Generic.Dictionary<PdfSharp.Pdf.PdfItem,object> objects = Count = Cannot evaluate expression because the current thread is in a stack overflow state., PdfSharp.Pdf.PdfObject pdfObject = (pairs=Cannot evaluate expression because the current thread is in a stack overflow state.), ref int depth = 29217) Line 502 + 0x11 bytes C#
Document is created pretty much like this
Code:
Document document = new Document();
document.Info.Title = string.Format("Raport sprawy");
document.Info.Author = "System zarządzania sprawami kancelarii Rhetor";
Section section = document.AddSection();
section.PageSetup.OddAndEvenPagesHeaderFooter = true;
section.PageSetup.StartingNumber = 1;
HeaderFooter header = section.Headers.Primary;
header.AddParagraph(string.Format("{0}: {1}", "Data wygenerowania raportu",
DateTime.Now.ToString("dd-MM-yyyy")));
// Create a paragraph with centered page number. See definition of style "Footer".
Paragraph paragraph = new Paragraph();
paragraph.AddTab();
paragraph.AddPageField();
// Add paragraph to footer for odd pages.
section.Footers.Primary.Add(paragraph);
// Add clone of paragraph to footer for odd pages. Cloning is necessary because an object must
// not belong to more than one other object. If you forget cloning an exception is thrown.
section.Footers.EvenPage.Add(paragraph.Clone());
foreach (CasesReportGeneral cr in casesReports.general)
{
section.AddPageBreak();
// Name & Lastname
paragraph = section.AddParagraph(string.Format("Imię i nazwisko: {0} {1}", cr.debtorName,
cr.debtorLastname));
paragraph.Format.Font.Size = 12;
paragraph.Format.Font.Bold = false;
paragraph.Format.SpaceAfter = 15;
paragraph.Format.OutlineLevel = OutlineLevel.Level1;
// Company
paragraph = section.AddParagraph(string.Format("Nazwa firma: {0}", cr.debtorCompanyName));
paragraph.Format.Font.Size = 12;
paragraph.Format.Font.Bold = false;
paragraph.Format.SpaceAfter = 15;
paragraph.Format.OutlineLevel = OutlineLevel.Level1;
// SAP
paragraph = section.AddParagraph(string.Format("SAP: {0}", cr.debtorSap));
paragraph.Format.Font.Size = 12;
paragraph.Format.Font.Bold = false;
paragraph.Format.SpaceAfter = 15;
paragraph.Format.OutlineLevel = OutlineLevel.Level1;
// Stage
paragraph.AddTab();
paragraph.AddTab();
paragraph.AddText(string.Format("Etap: {0}", cr.caseStage.GetDescriptionAttributeValue()));
// There are some comments in this type of proceeding
if (cr.casesComments.Any(cc => cc.type == CommentTypes.Friendly) && ProceedingIsOk(cr, CommentTypes.Friendly))
{
paragraph = section.AddParagraph(CommentTypes.Friendly.GetDescriptionAttributeValue(), "Heading1");
Table table = CreateCommentTable(cr.casesComments.Where(c => c.type == CommentTypes.Friendly).ToArray());
document.LastSection.Add(table);
document.LastSection.AddParagraph();
}
}
// Many times looped
private Table CreateCommentTable(CasesReportComments[] comments)
{
Table table = new Table();
Column column = new Column();
table.Borders.Width = 0.75;
column = table.AddColumn(Unit.FromCentimeter(1)); // Lp
column.Format.Alignment = ParagraphAlignment.Center;
column = table.AddColumn(Unit.FromCentimeter(10)); // Content
column.Format.Alignment = ParagraphAlignment.Center;
column = table.AddColumn(Unit.FromCentimeter(5)); // Date
column.Format.Alignment = ParagraphAlignment.Center;
Row row = table.AddRow();
Cell cell = row.Cells[0];
cell.AddParagraph("Lp.");
cell = row.Cells[1];
cell.AddParagraph("Komentarz");
cell = row.Cells[2];
cell.AddParagraph("Data");
int lp = 1;
bool addRow = true;
foreach (CasesReportComments crc in comments)
{
//if (isClient)
// if (!crm.hidden)
// addRow = false;
if (addRow)
{
row = table.AddRow();
row.Shading.Color = Colors.PaleGoldenrod;
cell = row.Cells[0];
cell.AddParagraph(lp.ToString());
cell = row.Cells[1];
cell.AddParagraph(crc.content);
cell = row.Cells[2];
cell.AddParagraph(crc.date.ToString("dd-MM-yyyy"));
lp++;
}
}
return table;
}
I'll provide any other info if you need one. Thanks in advance for any help.