Search code examples
ms-wordopenxmldocxxceed

OpenXML / Xceed insert Table without empty lines


I have an existing.docx document with some text. All I want is to insert programmatically a table to a specific place. So my idea was to add a Keyword where the table should be inserted. There are no empty lines, before and after the keyword. After the insertion of the table I add \n, before and after the table, for an empty line but somehow the Xceed library adds three after the table and two before the table.

Here is how I'm doing it:

using (DocX document = DocX.Load(@"C:\Users\rk\Desktop\test.docx"))
            {
                var IntTableSoftwareLocation = document.FindAll("Table").FirstOrDefault();


                document.ReplaceText("Table", "");

                var tableSoftware = document.InsertTable(IntTableSoftwareLocation, 3, 5);

                tableSoftware.InsertParagraphBeforeSelf("\n");
                tableSoftware.InsertParagraphAfterSelf("\n");

                tableSoftware.SetBorder(TableBorderType.InsideH, new Border());
                tableSoftware.SetBorder(TableBorderType.InsideV, new Border());
                tableSoftware.SetBorder(TableBorderType.Left, new Border());
                tableSoftware.SetBorder(TableBorderType.Bottom, new Border());
                tableSoftware.SetBorder(TableBorderType.Top, new Border());
                tableSoftware.SetBorder(TableBorderType.Right, new Border());

                //Header
                tableSoftware.Rows[0].Cells[0].Paragraphs[0].Append("Col1").Bold().Font("Arial").FontSize(11d);
                tableSoftware.Rows[0].Cells[1].Paragraphs[0].Append("Col2").Bold().Font("Arial").FontSize(11d);
                tableSoftware.Rows[0].Cells[2].Paragraphs[0].Append("Col3").Bold().Font("Arial").FontSize(11d);
                tableSoftware.Rows[0].Cells[3].Paragraphs[0].Append("Col4.").Bold().Font("Arial").FontSize(11d);
                tableSoftware.Rows[0].Cells[4].Paragraphs[0].Append("Col5").Bold().Font("Arial").FontSize(11d);

                //Content
                string TextToInsert = "Some Text";

                //Column width
                for (int i = 0; i < tableSoftware.RowCount; i++)
                {
                    for (int x = 0; x < tableSoftware.ColumnCount; x++)
                    {
                        #region set column width
                        if (x == 0)
                        {
                            tableSoftware.Rows[i].Cells[x].Width = 28.3; // 1cm
                        }
                        else if (x == 1)
                        {
                            tableSoftware.Rows[i].Cells[x].Width = 318;
                        }
                        else if (x == 2)
                        {
                            tableSoftware.Rows[i].Cells[x].Width = 50;
                        }
                        else if (x == 3)
                        {
                            tableSoftware.Rows[i].Cells[x].Width = 28.3;
                        }
                        else if (x == 4)
                        {
                            tableSoftware.Rows[i].Cells[x].Width = 64;
                        }
                        #endregion
                    }
                }

                tableSoftware.Rows[2].Cells[1].Paragraphs[0].Append(TextToInsert + "\n").FontSize(11d).Bold().Font("Arial");

                tableSoftware.Rows[2].Cells[2].Paragraphs[0].Append("User").Font("Arial").Alignment = Alignment.center;
                tableSoftware.Rows[2].Cells[2].VerticalAlignment = VerticalAlignment.Center;

                tableSoftware.Rows[2].Cells[3].Paragraphs[0].Append("1").Font("Arial").Alignment = Alignment.center;
                tableSoftware.Rows[2].Cells[3].VerticalAlignment = VerticalAlignment.Center;

                tableSoftware.Rows[2].Cells[4].Paragraphs[0].Append("2.199,00 €").Font("Arial").Alignment = Alignment.right;
                tableSoftware.Rows[2].Cells[4].VerticalAlignment = VerticalAlignment.Center;

                document.Save();
            } 

And thats how my docx Document looks like:

laksjdf
Table
alskdfjs

Solution

  • Ok, this is how it should be done:

    //Find the Paragraph by keyword
    var paraTable = document.Paragraphs.FirstOrDefault(x => x.Text.Contains("Table"));
    
    // Remove the Keyword
    paraTable.RemoveText(0);
    
    //Insert the table into Paragraph
    var table = paraTable.InsertTableAfterSelf(3, 5);
    

    No strange empty lines anymore