I have a word document in which there is table added. I want to access that document and add new blank row to the table which is already in it. I referred this reference link and created below code:
static void Main(string[] args)
{
string filePath = "C:\\TestDoc1.docx";
byte[] byteArray = File.ReadAllBytes(filePath);
using (MemoryStream stream = new MemoryStream())
{
stream.Write(byteArray, 0, (int)byteArray.Length);
using (WordprocessingDocument doc = WordprocessingDocument.Open(stream, true))
{
Body bod = doc.MainDocumentPart.Document.Body;
foreach (Table t in bod.Descendants<Table>().Where(tbl => tbl.GetFirstChild<TableRow>().Descendants<TableCell>().Count() == 4))
{
// Create a row.
TableRow tr = new TableRow();
t.Append(tr);
}
}
// Save the file with the new name
File.WriteAllBytes("C:\\TestDoc2.docx", stream.ToArray());
}
}
However, code does not throw any error. But when I open TestDoc2.docx I am getting the below error:
What am I missing?