WordDocument document = new WordDocument();
IWSection section = document.Sections[0];//// section content:
// text// {empty line}// textIWTable tablez = section.AddTable();table = section.Tables[i] as WTable;Can you advice me something.
// Loads the template document.
WordDocument wordDocument = new WordDocument("Template.docx");
// Initializes a new instance of the WTable class with the specified WordDocument instance.
WTable wTable = new WTable(wordDocument);
// Resets the table with the specified number of rows and columns.
wTable.ResetCells(1,2);
// Add paragraph and text to the table Cell.
wTable.Rows[0].Cells[0].AddParagraph().AppendText("First Cell");
wTable.Rows[0].Cells[1].AddParagraph().AppendText("Last Cell");
// Initializes text body part
TextBodyPart textBodyPart = new TextBodyPart(wordDocument);
// Adds a table into Text Body Part
textBodyPart.BodyItems.Add(wTable);
// Replaces a text with the text body part
wordDocument.Replace("{empty line}", textBodyPart, true, true);
// Saves and closes the document
wordDocument.Save("Sample.docx");
wordDocument.Close(); |
// Loads the template document.
WordDocument wordDocument = new WordDocument("TemplateWithEmptyLine.docx");
// Initializes a new instance of the WTable class with the specified WordDocument instance.
WTable wTable = new WTable(wordDocument);
// Resets the table with the specified number of rows and columns.
wTable.ResetCells(1, 2);
// Add paragraph and text to the table Cell.
wTable.Rows[0].Cells[0].AddParagraph().AppendText("First Cell");
wTable.Rows[0].Cells[1].AddParagraph().AppendText("Last Cell");
// Accesses the Body of section where all the contents in document are apart.
WTextBody wTextBody = wordDocument.LastSection.Body;
int index = 0;
// Iterates through each of the child items of WTextBody and checks for empty line.
foreach (Entity entity in wTextBody.ChildEntities)
if (entity.EntityType == EntityType.Paragraph && (entity as WParagraph).Text == string.Empty
&& entity.PreviousSibling is WParagraph && (entity.PreviousSibling as WParagraph).Text == "Text")
{
index = wTextBody.ChildEntities.IndexOf(entity);
break;
}
// Removes in between paragraph which contains empty line from Word document.
wTextBody.ChildEntities.RemoveAt(index);
// Insert table into index of empty line.
wTextBody.ChildEntities.Insert(index, wTable);
//Saves and closes the document
wordDocument.Save("Sample.docx");
wordDocument.Close(); |