Copy Table Style, Clear Cell Formatting & Apply Built-In Table Style to Specified Table using C# .NET API

Now,I have “old.docx” and “new.docx”:
1.var oldDoc = new Document("old.docx"); var newDoc = new Document("new.docx") { AttachedTemplate = "old.docx", AutomaticallyUpdateSyles = true };
2. Then,I want to apply the old.docx’s specified table style to the new.docx’s tables,the code might be this:
var oldTable = oldDoc.GetChildNodes(NodeType.Table, true).OfType<Table>().FirstOrDefault(x => x.ParentNode.NodeType != NodeType.HeaderFooter && x.Rows.Count > 0); var newtables = newDoc.GetChildNodes(NodeType.Table, true).OfType<Table>().ToList(); foreach (var table in newtables ) { table.Style = oldTable.Style ; }
The whole process looks something like this, but the newtables’style haven’t change.
How to solve this problem?

@Anghost,

To ensure a timely and accurate response, please ZIP and attach the following resources here for testing:

  • Your simplified input Word documents (old.docx and new.docx)
  • Aspose.Words for .NET 19.11 generated output DOCX file showing the undesired behavior
  • Your expected DOCX file showing the desired output. You can create this document by using MS Word.
  • Please also create a standalone simple console application (source code without compilation errors) that helps us to reproduce your current problem on our end and attach it here for testing. Please do not include Aspose.Words DLL files in it to reduce the file size.

As soon as you get these pieces of information ready, we will start investigation into your scenario and provide you more information. Thanks for your cooperation.

ApplyTableStyle.zip (5.3 MB)

@Anghost,

We tested the scenario and have managed to reproduce the same problem on our end. For the sake of any correction, we have logged this problem in our issue tracking system. The ID of this issue is WORDSNET-19644. We will further look into the details of this problem and will keep you updated on the status of correction. We apologize for your inconvenience.

@Anghost,

Regarding WORDSNET-19644, it is to update you that we have completed the analysis of this issue and the root cause has been identified. In the meantime, while you are waiting for a fix, please use the following code as a workaround:

Document oldDoc = new Document("E:\\Temp\\ApplyTableStyle\\old.docx");
Document newDoc = new Document("E:\\Temp\\ApplyTableStyle\\new.docx");

Style oldStyle = oldDoc.Styles["Table Grid"];

newDoc.Styles["Table Grid"].Remove();
Style newStyle = newDoc.Styles.AddCopy(oldStyle);

Table targetTable = newDoc.FirstSection.Body.Tables[0];
targetTable.Style = newStyle;

// Reset direct formatting.
foreach (Row row in targetTable.Rows)
    foreach (Cell cell in row.Cells)
        cell.CellFormat.ClearFormatting();

// Also it is better to copy conditional formatting settings which specifies 
// the components of the conditional formatting of the referenced table style
// which shall be applied to the current table. It may affect the table appearance.
targetTable.StyleOptions = oldDoc.FirstSection.Body.Tables[0].StyleOptions;

newDoc.Save("E:\\Temp\\ApplyTableStyle\\19.12-workaround.docx");

Hope, this helps.