OpenXML / Xceed 插入 Table 没有空行

OpenXML / Xceed insert Table without empty lines

我有一份包含一些文字的 existing.docx 文件。我只想以编程方式将 table 插入到特定位置。所以我的想法是在应该插入 table 的地方添加一个关键字。关键字前后没有空行。 在插入 table 之后,我在 table 之前和之后添加 \n 作为一个空行,但是 Xceed 库以某种方式在 table 之后添加了三个,在 table 之前添加了两个=].

这是我的做法:

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();
            } 

这就是我的 docx 文档的样子:

laksjdf
Table
alskdfjs

好的,应该这样做:

//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);

没有奇怪的空行了