Free Support Forum - aspose.com

Modify existing table with document builder and font formatting

Hi,
I have a docx document with only one table with one row and one cell that is used as master. This cell contains a letter with user font formatting (font,size,color,ecc.). The need is to change the table by adding cells and rows, keeping the font formatting on new cell.
The problem is that the new cell is not formatted.
How can I clone the formatting form the first cell on the other new cells ?
Thanks, Ivano

Dim doc As New Document(“D:\temp\Nota.docx”)
Dim tbl As Table = doc.FirstSection.Body.Tables(0)
Dim docBuilder As New DocumentBuilder(doc)
'Master Font
Dim masterFont = tbl.FirstRow.FirstCell.FirstParagraph.Runs(0).Font
’Builder Font
Dim currentFont = docBuilder.Font
’Setting font for the documentbuilder form now to the end
currentFont.Name=mastetFont.Name
currentFont.Size=masterFont.Size

’Adding a new cell with the same font formatting as the first one
Dim c As Cell
c = New Cell(doc)
c.AppendChild(New Paragraph(doc))
tbl.FirstRow.Cells.Add©
docBuilder.MoveToCell(0, 0, 1, 0)
docBuilder.Write(“Second Cell”)

doc.Save(“D:\temp\New.docx”, SaveFormat.Docx)

Hi

Thanks for your request. Please try using the following code:

Document doc = new Document("C:\\Temp\\in.docx");

Cell cell = new Cell(doc);

// Create paragraph and run, which will represent text of the cell.

Paragraph paragraph = new Paragraph(doc);

Run run = new Run(doc, "SecondCell");

// Set formatting

run.Font.Name = doc.FirstSection.Body.Tables[0].Rows[0].Cells[0].FirstParagraph.Runs[0].Font.Name;

run.Font.Size = doc.FirstSection.Body.Tables[0].Rows[0].Cells[0].FirstParagraph.Runs[0].Font.Size;

run.Font.Color = doc.FirstSection.Body.Tables[0].Rows[0].Cells[0].FirstParagraph.Runs[0].Font.Color;

// Add Run to Paragraph

paragraph.AppendChild(run);

// Add Paragraph to cell

cell.AppendChild(paragraph);

doc.FirstSection.Body.Tables[0].Rows[0].AppendChild(cell);

doc.Save("C:\\Temp\\out.docx");

Hope this helps.

Best regards,