Adicionando tabelas em um documento PDF via C#

Como adicionar tabela em PDF usando C# Biblioteca

Como adicionar tabelas em um documento PDF usando a biblioteca .NET

Para adicionar tabela, usaremos a API Aspose.PDF for .NET, que é uma API de manipulação de documentos rica em recursos, poderosa e fácil de usar para a plataforma net. Abra o gerenciador de pacotes NuGet, procure por Aspose.pdf e instale. Você também pode usar o seguinte comando no Console do Gerenciador de Pacotes.

Package Manager Console

PM > Install-Package Aspose.PDF

Adicionar tabela ao PDF via C#


Você precisa Aspose.PDF for .NET testar o código em seu ambiente.

  1. Carregue o PDF com uma instância do Document.
  2. Acesse a página por meio de seu índice.
  3. Objeto Create Table.
  4. Definir a configuração da tabela (por exemplo, definir as bordas).
  5. Preencher tabela.
  6. Adicione a tabela a uma página.
  7. Salve o arquivo.

Adicionar tabela em 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);