Gestionar tablas en documentos PDF a través de C#

Cómo trabajar con tablas en PDF usando la biblioteca C#

La acción más popular con las tablas - C#

Cómo gestionar tablas en documentos PDF mediante la biblioteca .NET

Para trabajar con table, 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#


    // Load source PDF document
    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";