Apply Custom Table style

Hi, 

I am trying to apply a custom table style to a word document following the documentation. 


It's looks like that the method table.ApplyStyle accepts only a builtin tablestyle but not the name of the custom table style. What could be a work around?

Regards

Franz 



3 Replies

SR Sindhu Ramesh Syncfusion Team February 27, 2025 01:03 PM UTC

Hi Franz,
Based on the details provided, we understand that your requirement is to apply a custom style to the table. You can achieve this using the ApplyStyle() method, which has an overload that accepts a style name as a string. This method searches for a style by the specified name and applies it to the table.

Kindly refer the below UG and GitHub Sample:
Custom table style Documentation
Apply-custom-table-style-Sample

Regards,
Sindhu Ramesh.



FB Franz Bichlmaier February 27, 2025 01:49 PM UTC

Thank you for the reply. 


I did try to use ApplyStyle(). Unfortunately, this Method does not accept a string (only builtinStyles)


Here is my code: 


 IWTable table = section.AddTable();

 table.ResetCells(11,2);

 table.TableFormat.Borders.BorderType = BorderStyle.None;

 cellWidths = new float[] { 250f, 200f };

 SetCellWidth(table, cellWidths);

 table.ApplyStyle("CustomStyle");  (ErrorMessage: conversion from string to builtinTableStyle not possible)


Regards


Franz



SR Sindhu Ramesh Syncfusion Team February 28, 2025 09:59 AM UTC

Franz,
You are encountering this issue because you are trying to apply a custom style in the IWTable interface. As mentioned in the user guide, instead of using IWTable, use the WTable class, to achieve it. Kindly refer to the modified code:

WordDocument document = new WordDocument();

WSection section = (WSection)document.AddSection();

WTable table = (WTable)section.AddTable();

table.ResetCells(11, 2);

table.TableFormat.Borders.BorderType = BorderStyle.None;

cellWidths = new float[] { 250f, 200f };

SetCellWidth(table, cellWidths);

table.ApplyStyle("CustomStyle");



Loader.
Up arrow icon