PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Getting an exception that I can't figure out
https://forum.pdfsharp.net/viewtopic.php?f=2&t=4042
Page 1 of 1

Author:  IRlyDunno [ Tue Nov 05, 2019 6:12 pm ]
Post subject:  Getting an exception that I can't figure out

so, basically I'm getting this exception

Code:
    the given key was not present in the dictionary. 
   em System.Collections.Generic.Dictionary`2.get_Item(TKey key)
   em MigraDoc.DocumentObjectModel.Internals.Meta.GetValue(DocumentObject dom, String name, GV flags)
   em MigraDoc.DocumentObjectModel.DocumentObject.GetValue(String name, GV flags)
   em MigraDoc.DocumentObjectModel.DocumentObject.GetValue(String name)
   em MigraDoc.DocumentObjectModel.Visitors.PdfFlattenVisitor.GetParentFont(DocumentObject obj)
   em MigraDoc.DocumentObjectModel.Visitors.PdfFlattenVisitor.VisitFormattedText(FormattedText formattedText)
   em MigraDoc.DocumentObjectModel.FormattedText.MigraDoc.   DocumentObjectModel.Visitors.IVisitable.AcceptVisitor(DocumentObjectVisitor visitor, Boolean visitChildren)
   em MigraDoc.DocumentObjectModel.DocumentElements.MigraDoc.DocumentObjectModel.Visitors.IVisitable.AcceptVisitor(DocumentObjectVisitor visitor, Boolean visitChildren)
   em MigraDoc.DocumentObjectModel.Tables.Cell.     MigraDoc.DocumentObjectModel.Visitors.IVisitable.AcceptVisitor(DocumentObjectVisitor visitor, Boolean visitChildren)
   em MigraDoc.DocumentObjectModel.Tables.Row.      MigraDoc.DocumentObjectModel.Visitors.IVisitable.AcceptVisitor(DocumentObjectVisitor visitor, Boolean visitChildren)
   em MigraDoc.DocumentObjectModel.Tables.Rows.     MigraDoc.DocumentObjectModel.Visitors.IVisitable.AcceptVisitor(DocumentObjectVisitor visitor, Boolean visitChildren)
   em MigraDoc.DocumentObjectModel.Tables.Table.    MigraDoc.DocumentObjectModel.Visitors.IVisitable.AcceptVisitor(DocumentObjectVisitor visitor, Boolean visitChildren)
   em MigraDoc.DocumentObjectModel.DocumentElements.MigraDoc.DocumentObjectModel.Visitors.IVisitable.AcceptVisitor(DocumentObjectVisitor visitor, Boolean visitChildren)
   em MigraDoc.DocumentObjectModel.Section.         MigraDoc.DocumentObjectModel.Visitors.IVisitable.AcceptVisitor(DocumentObjectVisitor visitor, Boolean visitChildren)
   em MigraDoc.DocumentObjectModel.Sections.        MigraDoc.DocumentObjectModel.Visitors.IVisitable.AcceptVisitor(DocumentObjectVisitor visitor, Boolean visitChildren)
   em MigraDoc.DocumentObjectModel.Document.        MigraDoc.DocumentObjectModel.Visitors.IVisitable.AcceptVisitor(DocumentObjectVisitor visitor, Boolean visitChildren)
   em MigraDoc.DocumentObjectModel.Visitors.VisitorBase.Visit(DocumentObject documentObject)
   em MigraDoc.Rendering.DocumentRenderer.   PrepareDocument()
   em MigraDoc.Rendering.PdfDocumentRenderer.PrepareDocumentRendrer(Boolean prepareCompletely)
   em MigraDoc.Rendering.PdfDocumentRenderer.PrepareRenderPages()
   em MigraDoc.Rendering.PdfDocumentRenderer.RenderDocument()


But I can't figure out what's causing it!

Could you provide some info to some scenarios where this might happen?

Author:  IRlyDunno [ Tue Nov 05, 2019 6:40 pm ]
Post subject:  Re: Getting an exception that I can't figure out

I found out what it was, I was trying to clear the elements of a cell then adding a new element there, and with that. Looks like that is something migradoc internal code doesn't like very much.

the code that was giving the error was this:

Code:
    FormattedText c = new FormattedText ();
    c.AddText(FuncoesGlobais.ObterMensagens("CartaoRefeicao"));
    table_body.Rows[0].Cells[4].Elements.Clear();
    table_body.Rows[0].Cells[4].Elements.Add(c);


with more detail here, I need to change the string that is being used for a table header, so I thought I could clear the elements and add them again and it would be ok, but I guess I have to find another way to do this, if I do I'll post it here.

Author:  IRlyDunno [ Wed Nov 06, 2019 9:54 am ]
Post subject:  Re: Getting an exception that I can't figure out

Ok the solution I found was:

searching throught the cell elements getting all paragraphs,

searching through all paragraph elements and getting all formattedTexts

searching through all FormattedText elements and getting all Texts,

then searching each Text.Content and changing it when the value I want to change is found.

this method is not failt proof, but it will work for me since I know I only use it when I have 1 Paragraph with 1 FormattedText and 1 Text element inside the cell.


the code for anyone who wants it:
Code:
// I wrote this in notepad++, because the actual method has some sensitive data, but this should work
public bool ChangeTextValue(this MigraDoc.DocumentObjectModel.Tables.Cell cell, string oldValue, string newValue)
{
   for(int i = 0; i < cell.Elements.Count; ++i)
   {
      if(cell.Elements[i] is Paragraph)
      {
         var p = cell.Elements[i];
         for(int j = 0; j < p.Elements.Count; ++j)
         {
            if(p.Elements[j] is FormattedText)
            {
               var ft = p.Elements[j];
               for(int l = 0; l < ft.Elements.Count; ++l)
               {
                  if(ft.Elements[l] is Text && ((Text)ft.Elements[l]).Content.Contains(oldValue))
                  {
                     ((Text)ft.Elements[l]).Content = newValue;
                     return true; // found the value / updated it
                  }
                  
                     
               }
            }
         }
      }
   }
   
   return false; // didn't find the value / didn't update
}

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