PDFsharp & MigraDoc Foundation
http://forum.pdfsharp.de/

KeyNotFoundException adding a table to a Section
http://forum.pdfsharp.de/viewtopic.php?f=2&t=3364
Page 1 of 1

Author:  slear [ Tue May 24, 2016 1:26 pm ]
Post subject:  KeyNotFoundException adding a table to a Section

This is started from nowhere today. I have made my first PDF with Migradoc without major issues and thus I was ready to widely employ the library for the other PDF required by my application following the same code pattern explained in your examples.

Now everytime I try to add a Table instance to the LastSection of a document I get this error at the PdfRenderer.RenderDocument call.

Code:
   
       private void AddData(Document document, int bookmarkCounter)
       {         
            Section section = document.AddSection();
            section.PageSetup.OddAndEvenPagesHeaderFooter = true;
            section.PageSetup.StartingNumber = 1;
            section.PageSetup.HeaderDistance = new Unit(1.0, UnitType.Centimeter);
            section.PageSetup.FooterDistance = new Unit(0.5, UnitType.Centimeter);
            HeaderFooter header = section.Headers.Primary;
            header.AddParagraph("\t.......");
            header = section.Headers.EvenPage;
            header.AddParagraph("...........");

            Paragraph paragraph = new Paragraph();
            paragraph.AddTab();
            paragraph.AddPageField();
            section.Footers.Primary.Add(paragraph);
            section.Footers.EvenPage.Add(paragraph.Clone());

             Paragraph paragraph = document.LastSection.AddParagraph("............", "Heading2");
             // Removed but doesn't make a difference
             // paragraph.AddBookmark("bkm" + bookmarkCounter.ToString("D3"));
             Table table = new Table();
             table.Borders.Width = 0.0;

              Column column = table.AddColumn(Unit.FromCentimeter(5));
              column.Format.Alignment = ParagraphAlignment.Left;
              table.AddColumn(Unit.FromCentimeter(12));
              AddRowToTable(table, "LABEL", "VALUE");
              document.LastSection.Add(table);
       }

        private void AddRowToTable(Table table, string textCol1, string textCol2)
        {
            Row row = table.AddRow();
            Cell cell = row.Cells[0];
            cell.AddParagraph(textCol1);
            cell = row.Cells[1];
            cell.AddParagraph(textCol2);
            return row;
        }


The following is the StackTrace at the exception
Code:
   in System.Collections.Generic.Dictionary`2.get_Item(TKey key)
   in MigraDoc.Rendering.TableRenderer.Format(Area area, FormatInfo previousFormatInfo)
   in MigraDoc.Rendering.TopDownFormatter.FormatOnAreas(XGraphics gfx, Boolean topLevel)
   in MigraDoc.Rendering.FormattedDocument.Format(XGraphics gfx)
   in MigraDoc.Rendering.DocumentRenderer.PrepareDocument()
   in MigraDoc.Rendering.PdfDocumentRenderer.PrepareDocumentRenderer(Boolean prepareCompletely)
   in MigraDoc.Rendering.PdfDocumentRenderer.PrepareRenderPages()
   in MigraDoc.Rendering.PdfDocumentRenderer.RenderDocument()
   in NE.CRParser.StampaFlussoCR.Esecuzione(Boolean anteprima, List`1 dati, Flussi flussi) in H:\_Sviluppo\Antiriciclaggio\CRParser\CRPDFWriter\StampaFlussoCR.cs:riga 47
   in NE.CRParser.PDFWriter.cmdPDF_Click(Object sender, EventArgs e) in H:\_Sviluppo\Antiriciclaggio\CRParser\CRPDFWriter\PDFWriter.cs:riga 117


Thanks for your attention
Steve

NB. I am using Migradoc 1.50.4000 and PDFSharp 1.50.4000

Author:  Thomas Hoevel [ Tue May 24, 2016 2:27 pm ]
Post subject:  Re: KeyNotFoundException adding a table to a Section

Hi!

I would use "Table table = document.LastSection.AddTable();" instead of "new Table()" and "Add(table)".
This may not make any difference though.

Please provide a solution that allows us to replicate the problem.
See also:
viewtopic.php?p=2094#p2094

I don't think your code snippet will compile, seeing "return row;" in a "void" method.

Author:  slear [ Tue May 24, 2016 2:30 pm ]
Post subject:  Re: KeyNotFoundException adding a table to a Section

Yes, sorry, it is a stripped down version where a Row is returned instead... I will try to manage a reproducible example of the code above. It is a bit difficult to extract just the Migradoc code.

Author:  slear [ Tue May 24, 2016 7:53 pm ]
Post subject:  Re: KeyNotFoundException adding a table to a Section

I have found the culprit.

I was setting the property Row.KeepWith to a value higher than the number of rows added to the table.
I mistakenly thought that number should include also the first row but that's not true.
So if my table has 5 rows and I want to keep all rows together on the same page I should set the property KeepWith on the first row to the value of 4.
Well the error message is not very explicative, I fail to see how this triggers a KeyNotFoundException, but All is well that ends well

However this presents a new problem for me because I have some tables where I don't know how many rows I have until the loop that dynamically add rows traverse all my data. Is there any setting at the table level?

Author:  Thomas Hoevel [ Wed May 25, 2016 8:22 am ]
Post subject:  Re: KeyNotFoundException adding a table to a Section

slear wrote:
However this presents a new problem for me because I have some tables where I don't know how many rows I have until the loop that dynamically add rows traverse all my data. Is there any setting at the table level?
You can always change the first row once the table is complete. Just set the KeepWith after adding the last row when you know the row count.

Author:  Thomas Hoevel [ Wed May 25, 2016 2:50 pm ]
Post subject:  Re: KeyNotFoundException adding a table to a Section

slear wrote:
Well the error message is not very explicative, I fail to see how this triggers a KeyNotFoundException, but All is well that ends well
The code probably tries to access the sixth row and this leads to an error. With an explicit check, the error message could be more informative.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/