Style Table Element

Introduction

In this article, we will dive into how to create and style a table element using Aspose.PDF for .NET. You’ll learn how to structure a table, apply custom styles, and validate the PDF/UA compliance of your document. By the end of this tutorial, you’ll be able to create professional-looking tables in your PDFs with ease!

Prerequisites

Before jumping into the tutorial, you’ll need to ensure you have the following:

  1. Visual Studio or a similar IDE installed on your machine.
  2. .NET Framework or .NET Core SDK for running the application.
  3. Aspose.PDF for .NET library downloaded and referenced in your project. You can grab the latest version from here.
  4. A valid Aspose license or a temporary license to unlock the full functionality of the library.

Import Packages

To start, import the necessary namespaces into your project:

using Aspose.Pdf.LogicalStructure;
using Aspose.Pdf.Tagged;
using Aspose.Pdf.Text;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

These namespaces cover core PDF operations, tagged content, tables, and text formatting.

Now let’s break down the process of creating and styling a table in Aspose.PDF. We’ll go through each section in detail so you can follow along.

Step 1: Create a New PDF Document and Setup Tagged Content

In this first step, we’ll create a blank PDF document and set up its tagged content.

// The path to the documents directory.
string dataDir = "YOUR DOCUMENT DIRECTORY";

// Create a new PDF document
Document document = new Document();

// Setup tagged content
ITaggedContent taggedContent = document.TaggedContent;
taggedContent.SetTitle("Example table style");
taggedContent.SetLanguage("en-US");

We start by creating a new Document object, representing our PDF. The TaggedContent object is used to manage the document’s structure, ensuring compliance with accessibility standards. We set the title and language of the document for proper tagging.

Step 2: Define the Root Element

Next, we’ll create the root structure element, which acts as the container for all the content in our PDF.

// Get the root structure element
StructureElement rootElement = taggedContent.RootElement;

The RootElement serves as the base container for all structured elements, including our table. It helps maintain the document’s structural hierarchy, which is important for both organization and accessibility.

Step 3: Create and Style the Table Element

Now that the root element is set up, we’ll create a TableElement and apply styles like background color, borders, and alignment.

// Create table structure element
TableElement tableElement = taggedContent.CreateTableElement();
rootElement.AppendChild(tableElement);

// Style the table
tableElement.BackgroundColor = Color.Beige;
tableElement.Border = new BorderInfo(BorderSide.All, 0.80F, Color.Gray);
tableElement.Alignment = HorizontalAlignment.Center;
tableElement.Broken = TableBroken.Vertical;
tableElement.ColumnAdjustment = ColumnAdjustment.AutoFitToWindow;

We create a TableElement, which defines our table structure. The BackgroundColor, Border, and Alignment properties allow us to customize the appearance of the table. The Broken property ensures that if the table breaks across pages, it breaks vertically.

Step 4: Set Table Dimensions and Cell Styles

In this step, we’ll define the number of columns, cell padding, and other important table properties.

tableElement.ColumnWidths = "80 80 80 80 80";
tableElement.DefaultCellBorder = new BorderInfo(BorderSide.All, 0.50F, Color.DarkBlue);
tableElement.DefaultCellPadding = new MarginInfo(16.0, 2.0, 8.0, 2.0);
tableElement.DefaultCellTextState.ForegroundColor = Color.DarkCyan;
tableElement.DefaultCellTextState.FontSize = 8F;

We specify the column widths to ensure each column in the table is uniformly spaced. The DefaultCellBorder, DefaultCellPadding, and DefaultCellTextState define the default styles for the cells, including borders, padding, text color, and font size.

Step 5: Add Repeating Rows and Custom Styles

We can also define styles for repeating rows and other specific table elements like headers and footers.

tableElement.RepeatingRowsCount = 3;
TextState rowStyle = new TextState();
rowStyle.BackgroundColor = Color.LightCoral;
tableElement.RepeatingRowsStyle = rowStyle;

The RepeatingRowsCount ensures that the first three rows repeat if the table spans multiple pages. We set the RepeatingRowsStyle to apply a custom background color to these rows.

Step 6: Add Table Head, Body, and Foot Elements

Now, let’s create the table header, body, and footer sections and populate them with content.

TableTHeadElement tableTHeadElement = tableElement.CreateTHead();
TableTBodyElement tableTBodyElement = tableElement.CreateTBody();
TableTFootElement tableTFootElement = tableElement.CreateTFoot();

// Create header row
TableTRElement headTrElement = tableTHeadElement.CreateTR();
headTrElement.AlternativeText = "Head Row";
for (int colIndex = 0; colIndex < 5; colIndex++)
{
    TableTHElement thElement = headTrElement.CreateTH();
    thElement.SetText($"Head {colIndex}");
}

// Populate the table body
for (int rowIndex = 0; rowIndex < 10; rowIndex++)
{
    TableTRElement trElement = tableTBodyElement.CreateTR();
    for (int colIndex = 0; colIndex < 5; colIndex++)
    {
        TableTDElement tdElement = trElement.CreateTD();
        tdElement.SetText($"Cell [{rowIndex}, {colIndex}]");
    }
}

The table is divided into three parts: the head, body, and foot. We first create the header row using TableTHElement and add column headings. Then, we populate the body of the table with TableTDElement, filling each cell with a label that includes its position.

Step 7: Save the Document

Finally, we save the PDF document to the specified directory.

// Save the tagged PDF document
document.Save(dataDir + "StyleTableElement.pdf");

This step finalizes the document creation process by saving the PDF file with the styled table.

Step 8: Validate PDF/UA Compliance

After saving the document, it’s essential to ensure that it complies with PDF/UA (Universal Accessibility) standards.

// Check PDF/UA compliance
document = new Document(dataDir + "StyleTableElement.pdf");
bool isPdfUaCompliance = document.Validate(dataDir + "StyleTableElement.xml", PdfFormat.PDF_UA_1);
Console.WriteLine($"PDF/UA compliance: {isPdfUaCompliance}");

Here, we reload the document and validate it against PDF/UA standards. Compliance ensures that your PDF meets accessibility requirements, making it suitable for a wide range of users.

Conclusion

With Aspose.PDF for .NET, creating and styling tables in your PDF documents is simple and intuitive. By following the steps outlined in this tutorial, you can build tables with customized styles and ensure your PDFs meet accessibility standards. Whether you’re generating reports or creating structured documents, tables are a powerful tool for presenting data clearly.

FAQ’s

Can I add images inside table cells?

Yes, you can insert images into table cells using the Image element.

How do I adjust column widths dynamically?

You can set the ColumnAdjustment property to AutoFitToWindow to adjust column widths automatically based on content.

Is PDF/UA compliance mandatory for all documents?

While not mandatory, it is recommended for documents that require high accessibility standards.

Can I apply different styles to specific rows?

Yes, you can customize individual rows or cells by adjusting their TextState or BackgroundColor.

What’s the benefit of using tagged content?

Tagged content improves document accessibility and helps ensure compliance with standards like PDF/UA.