表格前添加文字内容

您好:
我在读取文档内容是,希望在文档开头增加文字。但是文档内容只是一个表格,当我在文档最前面增加文字时,文字被写在了表格的第一个单元格里(附件中的input.doc)。我希望写在整个表格的最前面(上面)(附件中的output.doc),怎么实现。我使用的方法:
Document doc = new Document(path);
DocumentBuilder doc_db = new DocumentBuilder(doc);
doc_db.MoveToDocumentStart();
doc_db.Write(“添加文字”);
doc_db.MoveToDocumentEnd();
doc.Save(path);
谢谢!
Test.zip (15.9 KB)

@dhzhao2016,

请尝试使用以下代码

Document doc = new Document(MyDir + @"test\input.doc");

Paragraph para = new Paragraph(doc);
para.AppendChild(new Run(doc, "hello World"));

Table tab = doc.FirstSection.Body.Tables[0];
tab.ParentNode.InsertBefore(para, tab);          

doc.Save(MyDir + @"test\18.4.doc")

您好,感谢您提供的方法。如果文档中只有表格的话,这个方法是可以的。
如果我要批量处理多个Word文件,有的文件是以文本开头的;有的文件是以表格开头的,我该如何判断该用DocumentBuilder.MoveToDocumentStart();还是使用InsertBefore(您提供的)方法?

@dhzhao2016,

以下代码适用于所有情况:

Document doc = new Document(MyDir + @"test\input.doc");

Paragraph para = new Paragraph(doc);
para.AppendChild(new Run(doc, "hello World"));

doc.FirstSection.Body.InsertBefore(para, doc.FirstSection.Body.FirstChild);          

doc.Save(MyDir + @"test\18.4.doc");