This project has moved and is read-only. For the latest updates, please go here.

Change table with another one?

May 22, 2014 at 2:01 PM
Here is my problem,

I have a table in the 3th page of Word document and I need to modify it. Sometimes table will be expand and sometimes it will be get shortened. Is there any way I can do it?

What I mean is, lets say I have 1x3 table and I need to make it 6x3 and fill it with some data. How can I do it?
May 27, 2014 at 8:18 AM
Edited May 27, 2014 at 8:18 AM
Maybe better place Token instead of your table, and then replace it with table generated from your code :)

I saw function to replace tokens add by other user (dont remember name), and used it for tables:
public void ReplaceTextWithTable(string tokenName, TableTypeEnum tableTypeEnum)
        {
            int count = 1;
            var countReplace = 0;
            
            foreach (var paragraph in document.Paragraphs)
            {
                var valuesIndex = paragraph.FindAll(tokenName);
                if (valuesIndex.Count > 0)
                {
                    valuesIndex.Reverse();

                    foreach (var valueIndex in valuesIndex)
                    {
                        paragraph.InsertTableAfterSelf(CreateTable());
                        paragraph.RemoveText(valueIndex, tokenName.Length);
                        if (countReplace == count)
                        {
                            return;
                        }
                    }
                }
            }
        }


protected Novacode.Table CreateTable()
        {
            Novacode.Table table = document.AddTable(2, 3);

            table.Design = TableDesign.LightGridAccent3;
            table.Alignment = Alignment.left;
            table.AutoFit = AutoFit.Window;
            
            table.Rows[0].Cells[0].Paragraphs[0].Append(GetLocalResourceByKey("TableLp")).FontSize(15);
            table.Rows[0].Cells[1].Paragraphs[0].Append(GetLocalResourceByKey("TableUserName")).FontSize(15);
            table.Rows[0].Cells[2].Paragraphs[0].Append(GetLocalResourceByKey("TableUserPosition")).FontSize(15);

            table.Rows[1].Cells[0].Paragraphs[0].Append("1");
            table.Rows[1].Cells[1].Paragraphs[0].Append("Johny Bravo");
            table.Rows[1].Cells[2].Paragraphs[0].Append("Specialist");
            
            return table;
        }