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.

Fri Jul 19, 2024 9:32 am

Screenshot_1.png

I insert a row and fill text into cells, but it break format. What should I do to keep the format?

toanhv_123
 
Posts: 2
Joined: Fri Jul 19, 2024 9:24 am

Mon Jul 22, 2024 8:04 am

Hello,

Thank you for your inquiry.
To maintain the same paragraph style for cells, you can first copy the row and then modify the cell content. You can refer to the following sample code:

Code: Select all
            //Create a document
            Document document = new Document();
            //Load the file
            document.LoadFromFile(@"test.docx");
            //Get table   
            Spire.Doc.Section section = document.Sections[0];
            Spire.Doc.Table table = section.Tables[0] as Spire.Doc.Table;
            Spire.Doc.TableRow tableRowOne = table.Rows[0];
            //Copy row
            Spire.Doc.TableRow copyRow = tableRowOne.Clone();
            //Insert new row
            table.Rows.Insert(1, copyRow);
            //Change the data
            for (int i = 0; i < copyRow.Cells.Count; i++)
            {
                Spire.Doc.TableCell tc = copyRow.Cells[i];
                //Delete other paragraphs and keep only one
               for (int p= tc.Paragraphs.Count-1; p>=0 ; p--)
                {
                    if(p!=0)
                    {
                        tc.Paragraphs.RemoveAt(p);
                    }
                    else
                    {
                        //Directly modify to ensure the same paragraph style
                        Spire.Doc.Documents.Paragraph paragraph = tc.Paragraphs[p];
                        paragraph.Text = "Added";
                    }                   
                }         
            }
            document.SaveToFile(@"out.docx", FileFormat.Docx);

If you have any further questions, please feel free to contact.

Sincerely,
Amin
E-iceblue support team
User avatar

Amin.Gan
 
Posts: 231
Joined: Mon Jul 15, 2024 5:40 am

Tue Jul 23, 2024 3:00 am

Oh, my problem is solved. Thank you very much :D :D :D

toanhv_123
 
Posts: 2
Joined: Fri Jul 19, 2024 9:24 am

Tue Jul 23, 2024 7:57 am

Hi,

Thank you for your feedback.
If you encounter any issue with Spire in the future, just feel free to contact us at any time.

Sincerely,
Amin
E-iceblue support team
User avatar

Amin.Gan
 
Posts: 231
Joined: Mon Jul 15, 2024 5:40 am

Return to Spire.Doc