How to insert table between text paragraphs
hello,
I need to insert table to empty line in between text in section. but i dont know how to do it correctly.
This code add table to end of file,
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.
SIGN IN To post a reply.
1 Reply
SY
Sethumanikkam Yogendran
Syncfusion Team
January 31, 2017 06:26 AM UTC
Hi Customer,
Thank you for contacting Syncfusion support.
You can use any of the following way to achieve your requirement.
1. Find and Replace:
You can insert placeholder for example “{empty line}” in input Word document and using Find and Replace can replace that placeholder text with table.
Please find the code snippets which illustrates the same, and let us know if this helps you.
Thank you for contacting Syncfusion support.
You can use any of the following way to achieve your requirement.
1. Find and Replace:
You can insert placeholder for example “{empty line}” in input Word document and using Find and Replace can replace that placeholder text with table.
Please find the code snippets which illustrates the same, and let us know if this helps you.
|
// 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(); |
Please refer the UG link to know more about working with find and replace using DocIO.
2. Iterating through Word document elements:
Iterating through Word document elements also can remove the empty line and insert table into same position of that empty line in Word document.
Please find the code snippets which illustrates the same, and let us know if this helps you.
|
// 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(); |
Please refer the UG link to know more about working with iteration through Word document elements using DocIO.
Note: Please find the template Word documents from Template.docx and TemplateWithEmptyLine.docx.
If we misunderstand your requirement then, kindly update us requirement with clear description along with Input document used (if any) and screenshot/output Word document of the expected result which will helpful to provide you the appropriate solution at the earliest.
Please let us know if you have any other questions.
Regards,
Sethumanikkam.Y
SIGN IN To post a reply.
- 1 Reply
- 2 Participants
-
DM Dmytro
- Jan 30, 2017 10:41 AM UTC
- Jan 31, 2017 06:26 AM UTC