Products Technologies Demo Docs Blog Support Company

Document Editor: Formatting Table Cells Using JavaScript

This article explains how to format table cells in the Document Editor using JavaScript. It shows how to change the background color and frames of a table cell.

Document Editor: Formatting Table Cells Using JavaScript

The JavaScript API is a set of functions that allow you to interact with the document. You can use it to manipulate any part of the document, including paragraphs, images and even tables. It is very easy to add a new table to the document using the JavaScript API. Here is an example:

TXTextControl.tables.add(5, 5, 10, function(e) {
  if (e === true) { // if added
    TXTextControl.tables.getItem(function(table) {
      table.cells.forEach(function(cell) {

        cell.setText("Cell Text");

      });
    }, null, 10);
  }
})

This code creates a new table with 5 rows and 5 columns. The table is inserted at the current input position:

Tables in TX Text Control

Setting the TableCellFormat

To format the borders of the table, you must first get the TableCellFormat to apply new formatting to it. The following code loops through all tables to apply a visible border to all cells.

TXTextControl.tables.forEach(table => {
    table.cells.forEach(cell => {
        cell.getCellFormat(format => {
            format.leftBorder.setWidth(30);
            format.topBorder.setWidth(30);
            format.bottomBorder.setWidth(30);
            format.rightBorder.setWidth(30);
        });
    });
});

The border width is set to 30 Twips and the default color is black.

Tables in TX Text Control

Set Format to Current Cell

To set the format of a single table cell, you can use the following code:

TXTextControl.tables.getItem(tableAtInputPos => {
    tableAtInputPos.cells.getItemAtInputPosition(cellAtInputPos => {
        cellAtInputPos.getCellFormat(format => {
            format.setBackColor("#ea1183");
        });
    });
});

The code above sets the background color of the cell at the current input position to magenta.

Tables in TX Text Control

Conclusion

This article showed how to create and format tables using the JavaScript API of TX Text Control. The API provides a wide range of functions to manipulate tables and cells in a document. The code snippets above can be used as a starting point to create more complex table structures.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

ASP.NET

Integrate document processing into your applications to create documents such as PDFs and MS Word documents, including client-side document editing, viewing, and electronic signatures.

ASP.NET Core
Angular
Blazor
JavaScript
React
  • Angular
  • Blazor
  • React
  • JavaScript
  • ASP.NET MVC, ASP.NET Core, and WebForms

Learn more Trial token Download trial

Related Posts

ASP.NETASP.NET CorePDF

Why Table Control in Templates is Important for Professional PDF Creation in C#

Controlling how tables behave at page breaks is an important factor in creating professional-looking documents. This article discusses the importance of table control in templates for PDF generation.


ASP.NETWindows FormsASP.NET Core

Splitting Tables at Bookmark Positions and Cloning Table Headers

This article shows how to split tables at bookmark positions and how to clone table headers in TX Text Control .NET for Windows Forms and TX Text Control .NET Server.


ASP.NETWindows FormsExcel

Loading and Processing Excel XLSX Spreadsheet Tables into TX Text Control…

TX Text Control provides a powerful API to load and process Excel spreadsheet tables in .NET applications. This article shows how to load an Excel file and process the tables using TX Text Control…


AngularASP.NETASP.NET Core

Creating Advanced Tables in PDF and DOCX Documents with C#

This article shows how to create advanced tables in PDF and DOCX documents using the TX Text Control .NET for ASP.NET Server component. This article shows how to create tables from scratch,…


ASP.NETWindows FormsTab Stops

Text to Table and Table to Text in TX Text Control and C#

TX Text Control provides powerful table features and also full access to text formatting which can be used to create tables from text and vice versa. This article shows how to convert text to…