PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 7:47 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Tue May 24, 2016 1:26 pm 
Offline

Joined: Tue May 24, 2016 12:20 pm
Posts: 3
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


Top
 Profile  
Reply with quote  
PostPosted: Tue May 24, 2016 2:27 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Tue May 24, 2016 2:30 pm 
Offline

Joined: Tue May 24, 2016 12:20 pm
Posts: 3
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.


Top
 Profile  
Reply with quote  
PostPosted: Tue May 24, 2016 7:53 pm 
Offline

Joined: Tue May 24, 2016 12:20 pm
Posts: 3
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?


Top
 Profile  
Reply with quote  
PostPosted: Wed May 25, 2016 8:22 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed May 25, 2016 2:50 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC


Who is online

Users browsing this forum: Bing [Bot] and 139 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group