We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

How to Autofit to Contents in a Word table?

Is there a way to autofit to contents all of the table columns in a Word document created with DocIO?


3 Replies

PR Poorani Rajendran Syncfusion Team October 18, 2019 04:26 AM

Hi Bert,

Thank you for contacting Syncfusion support.

To meet your requirement, we suggest you use IsAutoResized API to auto resize of table to automatically resize all cell width based on its content. Please use the below highlighted code snippet in your sample application.

 
//Creates an instance of WordDocument class (Empty Word Document)
WordDocument document = new WordDocument();
//Opens an existing Word document into DocIO instance
document.Open("Table.docx", FormatType.Docx);
//Accesses the instance of the first section in the Word document
WSection section = document.Sections[0];
//Accesses the instance of the first table in the section
WTable table = section.Tables[0] as WTable;
//Specifies the auto resize of table to automatically resize all cell width based on its content
table.TableFormat.IsAutoResized = true;
//Saves and closes the document instance
document.Save("TableFormatting.docx", FormatType.Docx);
document.Close();
 

Please refer the below UG documentation link to know more about working with table and apply formatting to table in Word document using DocIO:
https://help.syncfusion.com/file-formats/docio/working-with-tables#apply-formatting-to-table-row-and-cell

Please let us know if you have any other questions.

Regards,
Poorani Rajendran




NI Nick May 7, 2022 05:05 AM

I am copying the above example (or at least I think I am!):

            var table = section.AddTable();

            table.TableFormat.IsAutoResized = true;

I am then saving as a PDF:

           var render = new DocIORenderer();

           var pdfDocument = render.ConvertToPDF(document);

I am using the latest versions of the Nuget packages:


I am seeing two issues.  Firstly the auto size doesn't seem to be working.  Secondly, the table format doesn't seem to be respecting the right margin (the right margin is actually set to the same size as the left):


I am probably doing something wrong and would welcome any pointers.

Thanks,

Nick



AN Anto Nihil Sahaya Raj Syncfusion Team May 9, 2022 03:46 AM

Hi Nick,

We have tried to reproduce the reported problem (auto size doesn't work and table format doesn't seem to be respecting the right margin using DocIO) using the given details, but it works properly at our end. We suspect that the reported problem might be due to the contents inside the input Word document and code snippets which used at your end. So, to analyze further on the reported problem with your requirement, could you please provide us the following things from your end,
  1. Input Word document.
  2. Complete code snippets of adding table and its format in the Word document.
  3. Output PDF document generated at your end.

Note: If you have any confidential data in your input Word document, please replace with some dummy data and provide us the same. We just need your document to recreate the problem you face.

Based on the above details, we will analyze further at our end and provide you the appropriate solution at the earliest.

Regards,
Anto Nihil S


Loader.
Live Chat Icon For mobile
Up arrow icon