PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Fri Nov 22, 2019 11:34 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Tue Nov 05, 2019 6:12 pm 
Offline

Joined: Tue Aug 06, 2019 10:45 am
Posts: 20
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?


Top
 Profile  
Reply with quote  
PostPosted: Tue Nov 05, 2019 6:40 pm 
Offline

Joined: Tue Aug 06, 2019 10:45 am
Posts: 20
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.


Top
 Profile  
Reply with quote  
PostPosted: Wed Nov 06, 2019 9:54 am 
Offline

Joined: Tue Aug 06, 2019 10:45 am
Posts: 20
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
}


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 123 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:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group