Text Alignment For Table Row Content

In this tutorial, we will guide you step by step to align the contents of a row in a table of a PDF document using Aspose.PDF for .NET. We’ll explain the provided C# source code and show you how to implement it.

Step 1: Creating the PDF document

First, we’ll create the PDF document:

var dataDir = "YOUR DOCUMENTS DIRECTORY";
Aspose.Pdf.Document doc = new Aspose.Pdf.Document();

Step 2: Table initialization

Next, we will initialize the table:

Aspose.Pdf.Table table = new Aspose.Pdf.Table();

Step 3: Setting the table border color

We will configure the table border color:

table.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, .5f, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray));

Step 4: Configuring the table cell border

We are going to configure the table cell border:

table.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, .5f, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray));

Step 5: Loop to add 10 rows to the table

We will now use a loop to add 10 rows to the table:

for (int row_count = 0; row_count < 10; row_count++)
{
     Aspose.Pdf.Row row = table.Rows.Add();
     row.VerticalAlignment = VerticalAlignment.Center;

     row.Cells.Add("Column("+row_count+",1)"+DateTime.Now.Ticks);
     row.Cells.Add("Column("+row_count+",2)");
     row.Cells.Add("Column("+row_count+",3)");
}

Step 6: Configuring the vertical line alignment

We are going to configure the vertical alignment of the rows of the table:

row.VerticalAlignment = VerticalAlignment.Center;

Step 7: Adding content to row cells

We are going to add content to the row cells:

row.Cells.Add("Column("+row_count+",1)"+DateTime.Now.Ticks);
row.Cells.Add("Column("+row_count+",2)");
row.Cells.Add("Column("+row_count+",3)");

Step 8: Adding the table to the document page

Now let’s add the table to the document page:

Page tocPage = doc.Pages.Add();
tocPage.Paragraphs.Add(table);

Step 9: Saving the PDF document

Finally, we will save the PDF document:

doc.Save(dataDir + "43620_ByWords_out.pdf");

Example source code for Text Alignment For Table Row Content using Aspose.PDF for .NET

var dataDir = "YOUR DOCUMENT DIRECTORY";

// Create PDF document
Aspose.Pdf.Document doc = new Aspose.Pdf.Document();
// Initializes a new instance of the Table
Aspose.Pdf.Table table = new Aspose.Pdf.Table();
// Set the table border color as LightGray
table.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, .5f, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray));
// set the border for table cells
table.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, .5f, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray));
// create a loop to add 10 rows
for (int row_count = 0; row_count < 10; row_count++)
{
	// add row to table
	Aspose.Pdf.Row row = table.Rows.Add();
	row.VerticalAlignment = VerticalAlignment.Center;

	row.Cells.Add("Column (" + row_count + ", 1)" + DateTime.Now.Ticks);
	row.Cells.Add("Column (" + row_count + ", 2)");
	row.Cells.Add("Column (" + row_count + ", 3)");
}
Page tocPage = doc.Pages.Add();
// Add table object to first page of input document
tocPage.Paragraphs.Add(table);
// Save updated document containing table object
doc.Save(dataDir + "43620_ByWords_out.pdf");

Conclusion

Congratulation ! You have now learned how to align the contents of a row in a table in a PDF document using Aspose.PDF for .NET. This step-by-step guide showed you how to create a document, initialize a table, configure border and alignment, add content, and save the PDF document. Now you can apply this knowledge to your own projects.

FAQ’s

Q: How can I align the contents of the table cells horizontally?

A: You can align the contents of the table cells horizontally by setting the HorizontalAlign property of the cell’s TextState object. For example, to center-align the text, use cell.TextState.HorizontalAlignment = HorizontalAlignment.Center. You can also set it to HorizontalAlignment.Left or HorizontalAlignment.Right for left and right alignment, respectively.

Q: Can I apply different border styles and colors to individual cells within the table?

A: Yes, you can apply different border styles and colors to individual cells within the table. To customize the border for a specific cell, set the cell.Border property to a new BorderInfo object with the desired settings, such as border sides, width, and color.

Q: How can I adjust the vertical alignment of the table content within the cells?

A: You can adjust the vertical alignment of the table content within the cells by setting the VerticalAlignment property of the row to VerticalAlignment.Center, VerticalAlignment.Top, or VerticalAlignment.Bottom. This property controls the vertical alignment of all cells in that row.

Q: Is it possible to add more columns or rows to the table dynamically?

A: Yes, you can add more columns and rows to the table dynamically by using the table.Rows.Add() method to add new rows and the row.Cells.Add() method to add new cells to the rows. You can do this inside loops or based on your specific requirements.

Q: How can I set a background color for specific cells or the entire table?

A: To set a background color for specific cells or the entire table, use the BackgroundColor property of the Cell or Table object. For example, to set the background color of a cell, use cell.BackgroundColor = Aspose.Pdf.Color.LightBlue.