Agregar tablas en un documento PDF a través de C#

Cómo añadir una tabla en un PDF con la biblioteca C#

Cómo añadir tablas en un documento PDF con la biblioteca .NET

Para agregar una tabla, usaremos la API Aspose.PDF for .NET, que es una API de manipulación de documentos rica en funciones, potente y fácil de usar para la plataforma net. Abra el administrador de paquetes NuGet, busque Aspose.pdf e instálelo. También puede usar el siguiente comando desde la consola de Package Manager.

Package Manager Console

PM > Install-Package Aspose.PDF

Agregar tabla al PDF mediante C#


Necesita Aspose.PDF for .NET para probar el código en su entorno.

  1. Cargue el PDF con una instancia de Document.
  2. Acceda a la página a través de su índice.
  3. Crear objeto Table.
  4. Establece la mesa (por ejemplo, establece los bordes).
  5. Rellenar tabla.
  6. Añada la tabla a una página.
  7. Guarde el archivo.

Añadir tabla en PDF: C#


Aspose.Pdf.Document doc = new Aspose.Pdf.Document(dataDir+ "AddTable.pdf");
// 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 = 1; row_count < 10; row_count++)
{
    // Add row to table
    Aspose.Pdf.Row row = table.Rows.Add();
    // Add table cells
    row.Cells.Add("Column (" + row_count + ", 1)");
    row.Cells.Add("Column (" + row_count + ", 2)");
    row.Cells.Add("Column (" + row_count + ", 3)");
}
// Add table object to first page of input document
doc.Pages[1].Paragraphs.Add(table);
dataDir = dataDir + "document_with_table_out.pdf";
// Save updated document containing table object
doc.Save(dataDir);