Table conditional styles unexpected behavior

Hi,
I’ve noticed that conditional table padding styles (checked for FirstRow in particular) have a somewhat unexpected interaction with inline (as in - defined for specific instance of a table) styles in imported tables.

I’m adding a table style (named Circle Table in my specific case) to a document created in aspose. This style defines padding for entire table, and padding specifically for first row. I then import into this document two tables from another (created with word) document. Both of the imported tables use a style under the same name (Circle Table), but one of the tables had its paddings changed directly (for both first row and whole table). I perform the import with the option “UseDestinationStyles”. In the resulting document, the table that had no direct formatting fully takes on the properties of the Circle Table style from the created document, while in the one that had spacings applied directly table body keeps the direct styles and in heading they are overriden by the Circle Table styles from the target document. While I do expect the attributes from Circle Table from source doc to be overriden with the ones in target doc, I would expect the direct formatting to be kept.

When I used a document prepared with Word Interop that contained Circle Style as the target document instead, direct formatting was kept, so I think this is the expected behavior.

I’m using code as follows to replicate this behavior:

internal class Program
{
    static void Main(string[] args)
    {
        ApplyLicense();
        //load reference and source, create Aspose document
        var docFromOldEngine = new Document(@"OldEmpty.docx");
        var sourceDocument = new Document(@"src.docx");
        var madeWithAspose = new Document();
        //Add Circle Table style to the empty Aspose document
        var style = (TableStyle)madeWithAspose.Styles.Add(StyleType.Table, "Circle Table");
        style.TopPadding = 50;
        style.Shading.BackgroundPatternColor = Color.Aqua;
        style.ConditionalStyles.FirstRow.BottomPadding = 50;
        style.ConditionalStyles.FirstRow.Shading.BackgroundPatternColor = Color.Blue;
        //Save Empty Aspose document
        madeWithAspose.Save(@"AsposeEmpty.docx");
        //import tables to both documents
        MergeTablesIntoDocument(sourceDocument, docFromOldEngine);
        MergeTablesIntoDocument(sourceDocument, madeWithAspose);
        //save both
        madeWithAspose.Save(@"AsposeWithTables.docx");
        docFromOldEngine.Save(@"OldWithTables.docx");
    }

    private static void MergeTablesIntoDocument(Document sourceDocument, Document targetDocument)
    {
        var importer = new NodeImporter(sourceDocument,
                                        targetDocument,
                                        ImportFormatMode.UseDestinationStyles);
        var builder = new DocumentBuilder(targetDocument);
        builder.Writeln("spacing paragraph");
        foreach (var table in sourceDocument.FirstSection.Body.Tables)
        {
            var imported = importer.ImportNode(table, true);
            targetDocument.FirstSection.Body.InsertBefore(imported,
                                                            targetDocument.FirstSection.Body.LastParagraph);
            builder.Writeln("spacing paragraph");
        }
    }

    private static void ApplyLicense()
    {
        var wordsLicense = new License();
        wordsLicense.SetLicense(@"path to licence");
    }
}

I’m also attaching a zip with a sample solution and both src.docx and OldEmpty.docx. I think the difference comes from how the first row is created in Aspose, as the same odd interaction (first row direct formatting padding overridden, body direct formatting padding retained) happens when the tables are copied from src.docx into AsposeEmpty.docx manually.

Could you advise whether it is possible to apply first row style in a way that allows the direct formatting of paddings to be retained in Aspose?

Thanks
CellMarginsDirectFormatting.zip (34.1 KB)

@acturisaspose

It seems you’re experiencing unexpected behavior with table styles and direct formatting in Aspose.Words. When you apply a table style that includes padding settings, those paddings are typically applied to the cells based on the style. However, if direct formatting has been applied to specific cells, it can lead to conflicts.

To retain the direct formatting of paddings while also applying the table style, you can use the ExpandTableStylesToDirectFormatting method. This method ensures that the padding settings from the style are applied directly to the cells without overriding any existing direct formatting.

Here’s a brief example of how to implement this:

// Create a document and a DocumentBuilder
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Create a custom table style with padding
Style style = doc.Styles.Add(StyleType.Table, "MyTableStyle");
TableStyle tableStyle = (TableStyle)style;
tableStyle.LeftPadding = 10;
tableStyle.RightPadding = 10;
tableStyle.TopPadding = 5;
tableStyle.BottomPadding = 5;

// Create a table and apply the style
Table table = builder.StartTable();
builder.InsertCell();
builder.Write("Cell 1");
builder.InsertCell();
builder.Write("Cell 2");
builder.EndRow();
builder.InsertCell();
builder.Write("Cell 3");
builder.InsertCell();
builder.Write("Cell 4");
builder.EndTable();

table.Style = doc.Styles["MyTableStyle"];

// Expand the table styles to direct formatting
doc.ExpandTableStylesToDirectFormatting();

// Save the document
doc.Save("TableWithDirectFormatting.docx");

This approach should help you maintain the direct formatting of paddings while applying the desired table style. If you continue to face issues, consider reviewing how the first row is created in your Aspose document, as this might affect the behavior you’re observing.

@acturisaspose
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): WORDSNET-28674

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.