Style Table Cell

Welcome to this detailed tutorial on formatting table cells using Aspose.PDF for .NET. In this guide, we will explain in detail each step of the provided C# source code to help you understand how to style table cells. Make sure you’ve installed Aspose.PDF for .NET and set up your development environment before you begin.

Step 1: Setting up the environment

Before you begin, make sure you’ve configured your development environment to use Aspose.PDF for .NET. This includes installing the Aspose.PDF library and configuring your project to reference it.

Step 2: Creating a document

In this step, we will create a new document object Aspose.PDF.

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

// Document creation
Document document = new Document();
ITaggedContent taggedContent = document.TaggedContent;
taggedContent.SetTitle("Example of table cell formatting");
taggedContent.SetLanguage("fr-FR");

We have created a new document and set the document title and language.

Step 3: Obtaining the root structure element

In this step we will get the root structure element for our document.

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

We got the root structure element which will serve as a container for the array elements.

Step 4: Creating the array structure element

Now let’s create a new table structure element for our document.

// Create the array structure element
TableElement tableElement = taggedContent.CreateTableElement();
rootElement.AppendChild(tableElement);
TableTHeadElement tableTHeadElement = tableElement.CreateTHead();
TableTBodyElement tableTBodyElement = tableElement.CreateTBody();
TableTFootElement tableTFootElement = tableElement.CreateTFoot();

We have created a new array structure element and added it to the root structure element. We also created the table header, body, and footer elements.

Step 5: Adding table headers

In this step we will add the table headers to our table.

// Number of rows and columns in the table
int rowCount = 4;
int colCount = 4;

int rowIndex;
int colIndex;

// Create the table header row
TableTRElement headTrElement = tableTHeadElement.CreateTR();
headTrElement.AlternativeText = "Header Row";

for (colIndex = 0; colIndex < colCount; colIndex++)
{
     TableTHElement theElement = headTrElement.CreateTH();
     theElement.SetText(string.Format("Header {0}", colIndex));
     theElement.BackgroundColor = Color.GreenYellow;
     theElement.Border = new BorderInfo(BorderSide.All, 4.0F, Color.Gray);
     theElement. IsNoBorder = true;
     theElement.Margin = new MarginInfo(16.0, 2.0, 8.0, 2.0);
     theElement.Alignment = HorizontalAlignment.Right;
}

We created a header row for our table and added header cells with formatting properties such as background color, borders, margins, and alignment.

Step 6: Adding the table body rows

Now let’s add the table body rows to our table.

for (rowIndex = 0; rowIndex < rowCount; rowIndex++)
{
     TableTRElement trElement = tableTBodyElement.CreateTR();
     trElement.AlternativeText = string.Format("Row {0}", rowIndex);

     for (colIndex = 0; colIndex < colCount; colIndex++)
     {
         int colSpan = 1;
         int rowSpan = 1;

         if (colIndex == 1 && rowIndex == 1)
         {
             colSpan = 2;
             rowSpan = 2;
         }
         else if (colIndex == 2 && (rowIndex == 1 || rowIndex == 2))
         {
             keep on going;
         }
         else if (rowIndex == 2 && (colIndex == 1 || colIndex == 2))
         {
             keep on going;
         }

         TableTDElement tdelement = trElement.CreateTD();
         tdElement.SetText(string.Format("Cell [{0}, {1}]", rowIndex, colIndex));
         tdElement.BackgroundColor = Color.Yellow;
         tdElement.Border = new BorderInfo(BorderSide.All, 4.0F, Color.Gray);
         tdElement.IsNoBorder = false;
         tdElement.Margin = new MarginInfo(8.0, 2.0, 8.0, 2.0);
         tdElement.Alignment = HorizontalAlignment.Center;

         TextState cellTextState = new TextState();
         cellTextState.ForegroundColor = Color.DarkBlue;
         cellTextState.FontSize = 7.5F;
         cellTextState.FontStyle = FontStyles.Bold;
         cellTextState.Font = FontRepository.FindFont("Arial");

         tdElement. DefaultCellTextState = cellTextState;
         tdElement.IsWordWrapped = true;
         tdElement.VerticalAlignment = VerticalAlignment.Center;
         tdElement.ColSpan = colSpan;
         tdElement. RowSpan = rowSpan;
     }
}

We added rows to the body of the table using loops to iterate over each table cell. We set formatting properties for each cell, such as background color, borders, margins, text alignment, etc.

Finally, we’ll add the table footer to our table.

TableTRElement footTrElement = tableTFootElement.CreateTR();
footTrElement.AlternativeText = "Footline";

for (colIndex = 0; colIndex < colCount; colIndex++)
{
     TableTDElement tdElement = footTrElement.CreateTD();
     tdElement.SetText(string.Format("Foot {0}", colIndex));
}

We created a footer for our table and added footer cells with text.

Step 8: Saving the tagged PDF document

Now that we’ve created our document with the styled table, we’ll save it as a tagged PDF document.

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

We saved the tagged PDF document in the specified directory.

Step 9: PDF/UA compliance validation

Next, we will validate the PDF/UA conformity of our document.

// PDF/UA compliance check
document = new Document(dataDir + "StyleTableCell.pdf");
bool isPdfUaCompliance = document.Validate(dataDir + "StyleTableCell.xml", PdfFormat.PDF_UA_1);
Console.WriteLine(string.Format("PDF/UA Compliance: {0}", isPdfUaCompliance));

We uploaded the tagged PDF document and validated its PDF/UA compliance by generating an XML report.

Sample source code for Style Table Cell using Aspose.PDF for .NET


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

// Create document
Document document = new Document();
ITaggedContent taggedContent = document.TaggedContent;
taggedContent.SetTitle("Example table cell style");
taggedContent.SetLanguage("en-US");

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

// Create table structure element
TableElement tableElement = taggedContent.CreateTableElement();
rootElement.AppendChild(tableElement);
TableTHeadElement tableTHeadElement = tableElement.CreateTHead();
TableTBodyElement tableTBodyElement = tableElement.CreateTBody();
TableTFootElement tableTFootElement = tableElement.CreateTFoot();
int rowCount = 4;
int colCount = 4;
int rowIndex;
int colIndex;
TableTRElement headTrElement = tableTHeadElement.CreateTR();
headTrElement.AlternativeText = "Head Row";
for (colIndex = 0; colIndex < colCount; colIndex++)
{
	TableTHElement thElement = headTrElement.CreateTH();
	thElement.SetText(String.Format("Head {0}", colIndex));
	thElement.BackgroundColor = Color.GreenYellow;
	thElement.Border = new BorderInfo(BorderSide.All, 4.0F, Color.Gray);
	thElement.IsNoBorder = true;
	thElement.Margin = new MarginInfo(16.0, 2.0, 8.0, 2.0);
	thElement.Alignment = HorizontalAlignment.Right;
}
for (rowIndex = 0; rowIndex < rowCount; rowIndex++)
{
	TableTRElement trElement = tableTBodyElement.CreateTR();
	trElement.AlternativeText = String.Format("Row {0}", rowIndex);
	for (colIndex = 0; colIndex < colCount; colIndex++)
	{
		int colSpan = 1;
		int rowSpan = 1;
		if (colIndex == 1 && rowIndex == 1)
		{
			colSpan = 2;
			rowSpan = 2;
		}
		else if (colIndex == 2 && (rowIndex == 1 || rowIndex == 2))
		{
			continue;
		}
		else if (rowIndex == 2 && (colIndex == 1 || colIndex == 2))
		{
			continue;
		}
		TableTDElement tdElement = trElement.CreateTD();
		tdElement.SetText(String.Format("Cell [{0}, {1}]", rowIndex, colIndex));
		tdElement.BackgroundColor = Color.Yellow;
		tdElement.Border = new BorderInfo(BorderSide.All, 4.0F, Color.Gray);
		tdElement.IsNoBorder = false;
		tdElement.Margin = new MarginInfo(8.0, 2.0, 8.0, 2.0);
		tdElement.Alignment = HorizontalAlignment.Center;
		TextState cellTextState = new TextState();
		cellTextState.ForegroundColor = Color.DarkBlue;
		cellTextState.FontSize = 7.5F;
		cellTextState.FontStyle = FontStyles.Bold;
		cellTextState.Font = FontRepository.FindFont("Arial");
		tdElement.DefaultCellTextState = cellTextState;
		tdElement.IsWordWrapped = true;
		tdElement.VerticalAlignment = VerticalAlignment.Center;
		tdElement.ColSpan = colSpan;
		tdElement.RowSpan = rowSpan;
	}
}
TableTRElement footTrElement = tableTFootElement.CreateTR();
footTrElement.AlternativeText = "Foot Row";
for (colIndex = 0; colIndex < colCount; colIndex++)
{
	TableTDElement tdElement = footTrElement.CreateTD();
	tdElement.SetText(String.Format("Foot {0}", colIndex));
}

// Save Tagged Pdf Document
document.Save(dataDir + "StyleTableCell.pdf");

// Checking PDF/UA compliance
document = new Document(dataDir + "StyleTableCell.pdf");
bool isPdfUaCompliance = document.Validate(dataDir + "StyleTableCell.xml", PdfFormat.PDF_UA_1);
Console.WriteLine(String.Format("PDF/UA compliance: {0}", isPdfUaCompliance));

Conclusion

In this tutorial, we learned how to style table cells using Aspose.PDF for .NET. We’ve seen how to create a document, add a table with headers, body rows, and a footer, and customize cell styles. Finally, we saved the tagged PDF document and validated its PDF/UA compliance. You can now use Aspose.PDF for .NET to create and style tables in your .NET applications.

FAQ’s

Q: What is the purpose of this tutorial on formatting table cells using Aspose.PDF for .NET?

A: This tutorial aims to provide a comprehensive guide on how to style table cells in a PDF document using the Aspose.PDF library for .NET. It covers step-by-step instructions and C# source code examples to help you understand and implement table cell formatting.

Q: What are the prerequisites for following this tutorial?

A: Before you begin, ensure that you have installed Aspose.PDF for .NET and have set up your development environment. This includes configuring your project to reference the Aspose.PDF library.

Q: How do I create a new PDF document using Aspose.PDF for .NET?

A: To create a new PDF document, you need to instantiate a Document object from the Aspose.PDF library. The provided C# source code demonstrates how to create a document and set its title and language.

Q: What is the significance of the root structure element in a PDF document?

A: The root structure element serves as a container for other structure elements, helping organize and categorize the content of the PDF document. It plays a crucial role in establishing the logical structure of the document.

Q: How can I create a table structure element and customize its appearance using Aspose.PDF for .NET?

A: You can create a table structure element using the CreateTableElement() method. The provided source code demonstrates how to customize the appearance of the table, including its header, body, and footer, by setting properties such as background color, borders, margins, and alignment.

Q: Can I add multiple rows and columns to the table body and customize their formatting?

A: Yes, the tutorial demonstrates how to add multiple rows and columns to the table body using loops. It also provides examples of customizing cell formatting, such as background color, borders, text alignment, font style, and more.

Q: What is the purpose of validating PDF/UA compliance, and how can I perform this validation?

A: Validating PDF/UA compliance ensures that the PDF document adheres to accessibility standards, making it more accessible to users with disabilities. The tutorial shows how to validate PDF/UA conformity using the Validate() method and generate an XML report.

Q: How can I apply these concepts to my own .NET applications?

A: You can use the provided C# source code examples as a guide to implementing table cell formatting in your own .NET applications. Customize the code as needed to suit your requirements and integrate it into your projects.

A: When styling table cells, consider the needs of your audience, including accessibility requirements. Use contrasting colors, appropriate fonts, and clear cell alignment to enhance readability. Additionally, validate PDF/UA compliance to ensure accessibility standards are met.

Q: What other features of Aspose.PDF for .NET can I explore for PDF document manipulation?

A: Aspose.PDF for .NET offers a wide range of features for PDF document manipulation, including text extraction, image insertion, form field management, digital signatures, and more. Explore the official documentation and resources to learn about additional functionalities.