This project has moved. For the latest updates, please go here.

InsertTable with parameter index fails

Mar 28, 2011 at 9:32 AM
Edited Mar 28, 2011 at 9:38 AM

I try to insert a table at a certain place in an existing document with the function document.InsertTable(index, table);

This function does a call to the function GetFirstParagraphEffectedByInsert from the static class helperfunctions. This function makes use of the document.paragraphlookup dictionary.

Unfortunately this dictionary is only filled when paragraphs are created and not when using an existing document.

 

Is there a better way to insert a table at a certain position?

 

Ps. I tried to insert a paragraph at a certain index, same result. Every function for inserting elements at a certain position (index) goes wrong.

Jun 6, 2011 at 3:14 AM

Any Fix for this. All inserts seem to fail!

Jul 27, 2011 at 2:16 PM

I am using either InsertTableAfterSelf() or InsertTableBeforeSelf().  I put an empty table in my document template to use as a tag, then I get its index and insert my actual table either before or after it, then remove the empty table. This has worked fine for me.

// Empty table
Table t = document.Tables[4];
// New table
Table callList = document.AddTable(10, 9);
Table newTable = t.InsertTableAfterSelf(callList);
t.Remove();
Feb 23, 2012 at 11:01 AM

Is there a real fix for this? I have the index where I want to insert, but get the out of range exception. I don't have the InsertTableAfterSelf function.

I saw this similar post: http://docx.codeplex.com/workitem/10062

But it does not solve my issue.

Help?

-M

May 25, 2012 at 5:06 PM
Dumeetje wrote:

try to insert a table at a certain place in an existing document with the function document.InsertTable(index, table);

This function does a call to the function GetFirstParagraphEffectedByInsert from the static class helperfunctions. This function makes use of the document.paragraphlookup dictionary.

Unfortunately this dictionary is only filled when paragraphs are created and not when using an existing document.

Hi all,

for bypass this problem, you can also use following function (edited from an earlier DocX version):

        static internal void RebuildParagraphs(DocX document)
        {
            document.paragraphLookup.Clear();

            // Get the runs in this paragraph
            IEnumerable<XElement> paras = document.mainDoc.Descendants(XName.Get("p", "http://schemas.openxmlformats.org/wordprocessingml/2006/main"));

            int startIndex = 0;

            // Loop through each run in this paragraph
            foreach (XElement par in paras)
            {
                Paragraph xp = new Paragraph(document, par, startIndex);

               // Only add runs which contain text
                if (Paragraph.GetElementTextLength(par) > 0)
                {
                    document.paragraphLookup.Add(xp.endIndex, xp);
                    startIndex = xp.endIndex;
                }
            }
        }

Hope this helps.

Best Regards

ZioZione

 

 

 

 

Developer
May 27, 2012 at 6:38 PM

@ziozione does this problem still exists with newest sources that it needs your workaround?

Feb 21, 2013 at 10:38 PM
Still seems to be a problem. Probably need to reconsider paragraphLookup.
Does anyone know about the history of this collection. Is a throw back to an older version, or was it designed for this purpose due to a performance issue or similar?
It seems to cause some quite serious problems in a few places. Some of the incoming patches/git commits regarding lists etc are also having problems with this.

Options appear to be:
  1. Track down everywhere the paragraphs are modified and make sure paragraphLookup is getting updated (hard?). A quick skim around suggests only some of the InsertParagraph overloads support it, I'd guess all the other candidates might take a while to track down
  2. Rebuilding it when it's needed (getter?) as per ZioZione's post. This is fairly quick and easy and would do for a starting point (and is better than crashing, so we should could put it into trunk?), but I don't know enough about this to understand the performance implications.
  3. Rewrite GetFirstParagraphEffectedByInsert - this seems to be the only function using it.
Any thoughts?