DocX clone table and insert at index

ⅰ亾dé卋堺 提交于 2019-11-30 15:16:32

Here is how I do it, I use a tag that I insert and replace with table:

// Add a Table to this document.
var table = document.AddTable(2, 3);

// Specify some properties for this Table.
table.Alignment = Alignment.center;

// Add content to this Table.
table.Rows[0].Cells[0].Paragraphs.First().Append("A");
table.Rows[0].Cells[1].Paragraphs.First().Append("B");
table.Rows[0].Cells[2].Paragraphs.First().Append("C");
table.Rows[1].Cells[0].Paragraphs.First().Append("D");
table.Rows[1].Cells[1].Paragraphs.First().Append("E");
table.Rows[1].Cells[2].Paragraphs.First().Append("F");

// Insert table at index where tag #TABLE# is in document.
document.InsertTable(table));
foreach (var paragraph in document.Paragraphs)
{
    paragraph.FindAll("#TABLE#").ForEach(index => paragraph.InsertTableAfterSelf((table)));
}

//Remove tag
document.ReplaceText("#TABLE#", ""); 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!