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

Inserting Paragraphs and Tables

Nov 29, 2016 at 12:24 PM
I import tables and paragraphs into a exciting docx documents
In basic I like to replace keyword like %table% with a table.
I was running in to errors.
By digging around I found some issues in the code
in the file container.cs around line 436
was : index[0] += p.startindex
should be index [i] += p.startindex

I modify the function GetFirstParagraphEffectedByInsert(...) in Helperfunction.cs
I leave the original with //

internal static Paragraph GetFirstParagraphEffectedByInsert(DocX document, int index)
    {
        // This document contains no Paragraphs and insertion is at index 0

        if (document.Paragraphs.Count() == 0 && index == 0)
            return null;

        //if (document.paragraphLookup.Keys.Count() == 0 && index == 0)
        //    return null;


        foreach (Paragraph p in document.Paragraphs)
        {
            if (p.endIndex >= index)
                return p;
        }


        //foreach (int paragraphEndIndex in document.paragraphLookup.Keys)
        //{
        //    if (paragraphEndIndex >= index)
        //        return document.paragraphLookup[paragraphEndIndex];
        //}

        throw new ArgumentOutOfRangeException();
    }
The insert table works now ( I insert the same table on different places )
But now I find out that the place of the insert is not always correct
It seems the the startindex of a paragraph is not always correct.
I think it should not be possible that the endindex of a paragraph should NOT be greater than the startindex of the the next paragraph. but this is the case.
the result is that the function GetFirstParagraphEffectedByInsert can give a wrong paragraph back.
I try to find out how this index is working but but until now it is a mystery.
so the big question for me is now : How is the startindex and endindex in a paragraph calculated and where?

I like to help make the code better. I don't know if this is the right place or forum.
So any comment is welcome.

ton