DocX clone table and insert at index

后端 未结 1 458
离开以前
离开以前 2021-01-03 04:20

I am using C# to make a simple Windows app using Novacode to manipulate a Word document.

I have a source table in my Word document that I want to clone. I am able t

1条回答
  •  孤城傲影
    2021-01-03 04:27

    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#", ""); 
    

    0 讨论(0)
提交回复
热议问题