Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Tue Aug 05, 2014 11:24 am

Hello,

I am trying to do a simple text replacement with table. The example shown for adding table will always add the table at the end of document. However, we need to add the table in the middle of document where a 'key text' is found. Is there a method similar to 'document.Replace' where I can replace a text with a table?

There is an example on 'Replace Text in Word with HTML' in VB. Is it possible for you to provide an example to 'Replace Text in Word with Table' in C#?


Regards,
Heng Kok

hengkokatmuu
 
Posts: 2
Joined: Tue Aug 05, 2014 9:09 am

Wed Aug 06, 2014 6:40 am

Dear Heng Kok,

Thanks for your inquiry.
For your requirement, please refer to the following code.
Code: Select all
Document doc = new Document();
doc.LoadFromFile(@"..\..\test.docx");
Section section = doc.Sections[0];
//”Burning.liu” as a “key text”
TextSelection selection = doc.FindString("Burning.liu", true, true);
TextRange range = selection.GetAsOneRange();
Paragraph paragraph = range.OwnerParagraph;
Body body = paragraph.OwnerTextBody;
int index = body.ChildObjects.IndexOf(paragraph);
Table table = section.AddTable(true);
table.ResetCells(3, 3);
body.ChildObjects.Remove(paragraph);
body.ChildObjects.Insert(index, table);
doc.SaveToFile("result.doc", FileFormat.Doc);
System.Diagnostics.Process.Start("result.doc");


Please feel free to contact us if you have any problems.

Best regards,
Burning
E-iceblue support team
Best Regards,
Burning
E-iceblue Support Team
User avatar

burning.liu
 
Posts: 406
Joined: Mon Aug 04, 2014 6:47 am

Wed Aug 06, 2014 7:52 am

burning.liu wrote:Dear Heng Kok,

Thanks for your inquiry.
For your requirement, please refer to the following code.
Code: Select all
Document doc = new Document();
doc.LoadFromFile(@"..\..\test.docx");
Section section = doc.Sections[0];
//”Burning.liu” as a “key text”
TextSelection selection = doc.FindString("Burning.liu", true, true);
TextRange range = selection.GetAsOneRange();
Paragraph paragraph = range.OwnerParagraph;
Body body = paragraph.OwnerTextBody;
int index = body.ChildObjects.IndexOf(paragraph);
Table table = section.AddTable(true);
table.ResetCells(3, 3);
body.ChildObjects.Remove(paragraph);
body.ChildObjects.Insert(index, table);
doc.SaveToFile("result.doc", FileFormat.Doc);
System.Diagnostics.Process.Start("result.doc");


Please feel free to contact us if you have any problems.

Best regards,
Burning
E-iceblue support team


Hi Burning.liu,

This is a perfect example. Thanks for your quick response. I am really impress with your turnaround time. Kudos!

Regards,
Heng Kok

hengkokatmuu
 
Posts: 2
Joined: Tue Aug 05, 2014 9:09 am

Wed Aug 06, 2014 8:18 am

You're welcome.

Please feel free to contact us if you have any problems.

Best regards,
Burning
E-iceblue support team
Best Regards,
Burning
E-iceblue Support Team
User avatar

burning.liu
 
Posts: 406
Joined: Mon Aug 04, 2014 6:47 am

Return to Spire.Doc