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 Mar 01, 2022 5:50 pm

I have a trouble about the replace text, I already understand that how to replaced the text in docx but my challenge is (for example), I have the existing docx file and that file already contain the blank table 4 column the first column have the text "XXX" in that and another column are blank. I have to replace the XXX with "AAA" and then the next column insert "BBB" and next column insert "CCC" and "DDD" in the next column.
For manually I press TAB key to move the cursor to next column but I don't know how to do it with spire.doc, Please advice Thank you.

naywaen99
 
Posts: 2
Joined: Sun Feb 27, 2022 2:23 am

Wed Mar 02, 2022 1:56 am

Hello,

Thanks for your inquiry.
Please refer to the following code to achieve your needs. Here I also attached my test file for your reference. If there is any question, just feel free to contact us.
Code: Select all
Document document = new Document();
document.LoadFromFile("input.docx");
//Get the first section
Section section = document.Sections[0];
//Get the first table in the section
Table table = section.Tables[0] as Table;
//Replace the text of table
table.Replace("xxx", "AAA",false,true);
string[] newText = { "BBB", "CCC", "DDD" };
for (int r =0; r < table.Rows.Count; r++)
{
    for (int c = 1; c < table.Rows[r].Cells.Count; c++)
    {
        TableCell cell = table.Rows[r].Cells[c];
        IParagraph paragraph = cell.FirstParagraph;
        // Insert text
        paragraph.AppendText(newText[c-1]);
    }
}
document.SaveToFile("result.docx", FileFormat.Docx2010);

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1643
Joined: Wed Apr 07, 2021 2:50 am

Wed Mar 02, 2022 3:52 am

Thank you Very much, But I may be a bit confuse about communicate because the real doc isn't simple as I tell TT-TT.
I attached the docx file and You can see the table below the document, I design the table as attached file and when I press TAB the keyboard until endofline the next tab will be newline automatically, but when I try to contain whole data in one string array string[] newText = { "BBB", "CCC", "DDD" }; It's doesn't work. Could you please suggest me how to manage this point.
one is the table format is complicate and one is the newText array can be contain the whole (all row) data and append text in one time. Thank you

naywaen99
 
Posts: 2
Joined: Sun Feb 27, 2022 2:23 am

Wed Mar 02, 2022 8:34 am

Hello,

Thanks for your feedback.
After investigation, I found that the table where the text "XXX" is located can be obtained under the Section of the document. But the table to be filled with data is in a TextBox. Please use the attached code to populate the table with data.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1643
Joined: Wed Apr 07, 2021 2:50 am

Return to Spire.Doc