Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Windows. Forms;
Using DocumentFormat. OpenXml. Packaging;
Using DocumentFormat. OpenXml. Wordprocessing;
Using System. IO;
Namespace ConsoleApplication15
{
Class Program
{
[STAThread]
Static void Main (string [] args)
{
OpenFileDialog ofd = new OpenFileDialog ();
Ofd. Multiselect = false;
Ofd. Filter = "Word Document | *. docx ";
Ofd. ShowDialog ();
String usepath = AppDomain. CurrentDomain. SetupInformation. ApplicationBase + "test.docx ";
File. Copy (ofd. FileName, usepath, true );
Using (WordprocessingDocument wpd = WordprocessingDocument. Open (usepath, true ))
{
MainDocumentPart mdp = wpd. MainDocumentPart;
Document dc = mdp. Document;
SdtBlock target = null;
Foreach (SdtBlock sb in dc. Descendants <SdtBlock> ())
{
Tag tg = sb. Descendants <Tag> (). Where (T => T. Val = "TestField"). FirstOrDefault ();
If (tg! = Null)
{
Target = sb;
Break;
}
}
If (target! = Null)
{
SdtContentBlock scb = target. SdtContentBlock;
Table tb = new Table ();
TableGrid tg = new TableGrid ();
GridColumn gc = new GridColumn () {Width = "2840 "};
GridColumn gc1 = new GridColumn () {Width = "2840 "};
Tg. Append (gc );
Tg. Append (gc1 );
Tb. Append (tg );
TableRow tr = new TableRow ();
TableCell tc = new TableCell ();
Paragraph p = new Paragraph ();
TableCell tc1 = new TableCell ();
Paragraph p1 = new Paragraph ();
Tc. Append (p );
Tc1.Append (p1 );
Tr. Append (tc );
Tr. Append (tc1 );
Tb. Append (tr );
Scb. Append (tb );
}
Dc. Save ();
}
}
}
}
From TX_OfficeDev's column