Hi
I wounder how can i add a table or a text on a specific location into the pdf document
any smaple code?
Regards
To add a table or text at a specific location in a PDF document using Aspose.PDF, you can follow these steps. Below is a sample code snippet in C# that demonstrates how to create a table and position it on a specific page of a PDF document.
Sample Code in C#
using System;
using Aspose.Pdf;
namespace AsposePdfExample
{
class Program
{
static void Main(string[] args)
{
// Create a new PDF document
Document pdfDocument = new Document();
// Add a page to the document
Page page = pdfDocument.Pages.Add();
// Create a table
Table table = new Table();
// Set the table border
table.Border = new BorderInfo(BorderSide.All, 0.5f, Color.LightGray);
// Set the default cell border
table.DefaultCellBorder = new BorderInfo(BorderSide.All, 0.5f, Color.LightGray);
// Add rows and cells to the table
for (int rowCount = 1; rowCount <= 3; rowCount++)
{
Row row = table.Rows.Add();
row.Cells.Add("Column " + rowCount + ", Cell 1");
row.Cells.Add("Column " + rowCount + ", Cell 2");
}
// Set the position of the table
table.ColumnWidths = "100 100"; // Set column widths
table.Margin.Top = 100; // Set top margin
table.Margin.Left = 50; // Set left margin
// Add the table to the page
page.Paragraphs.Add(table);
// Save the document
pdfDocument.Save("output.pdf");
Console.WriteLine("Table added successfully to the PDF document.");
}
}
}
Explanation
- Create a Document: Start by creating a new PDF document and adding a page to it.
- Create a Table: Instantiate a
Table
object and set its border properties. - Add Rows and Cells: Populate the table with rows and cells.
- Set Position: Adjust the margins of the table to position it at a specific location on the page.
- Add to Page: Finally, add the table to the page’s paragraphs and save the document.
This code will create a PDF file named output.pdf
with a table positioned according to the specified margins. You can adjust the Margin.Top
and Margin.Left
properties to change the table’s position as needed.
For more detailed information on working with tables in Aspose.PDF, you can refer to the official documentation here and here.
Sources
[1]: Create or Add Table In PDF using C# - Aspose Documentation
[2]: Manipulate Tables in existing PDF|Aspose.PDF for .NET