Get Table Width In PDF File

In this tutorial, we are going to learn how to get the width of a table in PDF file using Aspose.PDF for .NET. We will explain the source code in C# step by step. At the end of this tutorial, you will know how to get the width of a table in a PDF document. Let’s start!

Step 1: Setting up the environment

First, make sure you’ve set up your C# development environment with Aspose.PDF for .NET. Add the reference to the library and import the necessary namespaces.

Step 2: Creating a New Document and Page

We create a new PDF document and add a page in this document.

Document doc = new Document();
Page page = doc.Pages.Add();

Step 3: Initializing a new table

We initialize a new table and set the column fit to “AutoFitToContent”.

Table table = new Table
{
ColumnAdjustment = ColumnAdjustment.AutoFitToContent
};

Step 4: Add row and cells in table

We add a row in the table and add cells in that row.

Row row = table.Rows.Add();
Cell cell = row.Cells.Add("Text of cell 1");
cell = row.Cells.Add("Text from cell 2");

Step 5: Get table width

We use the “GetWidth()” method to get the width of the table.

Console.WriteLine(table.GetWidth());

Example source code for get Table Width using Aspose.PDF for .NET

// Create a new document
Document doc = new Document();
// Add page in document
Page page = doc.Pages.Add();
// Initialize new table
Table table = new Table
{
	ColumnAdjustment = ColumnAdjustment.AutoFitToContent
};
// Add row in table 
Row row = table.Rows.Add();
// Add cell in table
Cell cell = row.Cells.Add("Cell 1 text");
cell = row.Cells.Add("Cell 2 text");
// Get table width
Console.WriteLine(table.GetWidth());

System.Console.WriteLine("Extracted table width succesfully!");

Conclusion

In this tutorial, we learned how to get the width of a table in a PDF document using Aspose.PDF for .NET. You can use this step-by-step guide to get table widths in your own C# projects.

FAQ’s for get table width in PDF file

Q: Can I modify the column adjustment of the table to a fixed width instead of AutoFitToContent?

A: Yes, you can adjust the column width to a fixed value by setting the ColumnAdjustment property to ColumnAdjustment.FixedColumnWidth. After setting this property, you can specify the desired width for each column using the ColumnWidths property of the table.

Q: What if the table spans across multiple pages? Will the GetWidth() method still provide accurate results?

A: The GetWidth() method calculates the width of the table based on its content within the current page. If the table spans across multiple pages, you may need to iterate through each page and sum up the widths of the table on each page to get the overall width of the complete table.

Q: Can I get the individual column widths of the table using Aspose.PDF for .NET?

A: Yes, you can retrieve the individual column widths of the table using the ColumnWidths property. It returns a string that represents the width of each column separated by spaces. You can then parse this string to get the width of each column.

Q: Is it possible to get the height of the table using Aspose.PDF for .NET?

A: Yes, you can get the height of the table using the GetHeight() method of the table. This method returns the total height of the table based on its content and layout.

Q: Can I adjust the table width based on specific content in each cell?

A: Yes, you can adjust the table width based on specific content in each cell by setting the ColumnAdjustment property to ColumnAdjustment.AutoFitToContent. Aspose.PDF for .NET will automatically adjust the column widths to fit the content in each cell.