Replace a text by a table

Hello,

I want to find a text and replace it by table but i don't know how to do it. i am new to the platform so any help will be appreciated.

For example:

In the doc there a word "table" want to change that by real table. 
I try  document.Replace("table", section, true, true, true);
In the section I add the table.



1 Reply

DB Dilli Babu Nandha Gopal Syncfusion Team January 7, 2019 05:07 PM UTC

Hi Saly, 
 
Thank you for your patience. 
 
Yes, it is possible to replace a text with a table in a Word document which is illustrated in the following code example. 

//Open a word Document instance 
WordDocument document = new WordDocument("Template.docx"); 
 
//Create a new table. 
WTable table = new WTable(document); 
 
//sets lineWidth and color 
 
table.TableFormat.Borders.LineWidth = 2f; 
 
table.TableFormat.Borders.Color = Color.Black; 
 
//Sets number of rows and columns. 
 
table.ResetCells(2, 2); 
 
 
//Selects the first row and appends text in each cell. 
 
WTableRow row0 = table.Rows[0]; 
 
row0.Cells[0].AddParagraph().AppendText("1"); 
 
row0.Cells[0].CellFormat.Borders.LineWidth = 2f; 
 
row0.Cells[0].CellFormat.Borders.Color = Color.Black;        
 
row0.Cells[1].AddParagraph().AppendText("2"); 
 
//Selects the second row and appends text in each cell. 
 
WTableRow row1 = table.Rows[1];        
 
row1.Cells[0].AddParagraph().AppendText("4"); 
 
row1.Cells[1].AddParagraph().AppendText("5"); 
 
row1.Cells[1].CellFormat.Borders.LineWidth = 2f; 
 
row1.Cells[1].CellFormat.Borders.Color = Color.Black; 
 
//Creates TextBodyPart and insert the table. 
 
TextBodyPart part = new TextBodyPart(document); 
 
part.BodyItems.Insert(0, table); 
 
// replaces the specified text with a part. 
document.Replace("ReplaceText", part, false, false); 
 
//Save document and dispose the Word document instance 

To know more about different replacing functionalities, kindly refer the following link. 

Please let us know if you have any questions. 

Regards, 
Dilli babu. 


Loader.
Up arrow icon