// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddTable()
{
// Create PDF document
using (var document = new Aspose.Pdf.Document
{
PageInfo = new Aspose.Pdf.PageInfo { Margin = new Aspose.Pdf.MarginInfo(28, 28, 28, 42) }
})
{
var table = new Aspose.Pdf.Table
{
// Set column widths of the table
ColumnWidths = "25% 25% 25% 25%",
// Set cell padding
DefaultCellPadding = new Aspose.Pdf.MarginInfo(10, 5, 10, 5), // Left Bottom Right Top
// Set the table border color as Green
Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, .5f, Aspose.Pdf.Color.Green),
// Set the border for table cells as Black
DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, .2f, Aspose.Pdf.Color.Green),
};
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("config.json", false)
.Build();
var connectionString = configuration.GetSection("connectionString").Value;
if (string.IsNullOrEmpty(connectionString))
{
throw new ArgumentException("No connection string in config.json");
}
var resultTable = new DataTable();
using (var conn = new SqlConnection(connectionString))
{
const string sql = "SELECT * FROM Tennats";
using (var cmd = new SqlCommand(sql, conn))
{
using (var adapter = new SqlDataAdapter(cmd))
{
adapter.Fill(resultTable);
}
}
}
table.ImportDataTable(resultTable, true, 1, 1);
// Add table object to first page of input document
document.Pages[1].Paragraphs.Add(table);
using (var streamOut = new MemoryStream())
{
// Save PDF document
document.Save(streamOut);
return new FileContentResult(streamOut.ToArray(), "application/pdf")
{
FileDownloadName = "demotable2.pdf"
};
}
}
}
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddTable()
{
// Create PDF document
using var document = new Aspose.Pdf.Document
{
PageInfo = new Aspose.Pdf.PageInfo { Margin = new Aspose.Pdf.MarginInfo(28, 28, 28, 42) }
};
var table = new Aspose.Pdf.Table
{
// Set column widths of the table
ColumnWidths = "25% 25% 25% 25%",
// Set cell padding
DefaultCellPadding = new Aspose.Pdf.MarginInfo(10, 5, 10, 5), // Left Bottom Right Top
// Set the table border color as Green
Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, .5f, Aspose.Pdf.Color.Green),
// Set the border for table cells as Black
DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, .2f, Aspose.Pdf.Color.Green),
};
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("config.json", false)
.Build();
var connectionString = configuration.GetSection("connectionString").Value;
if (string.IsNullOrEmpty(connectionString))
{
throw new ArgumentException("No connection string in config.json");
}
var resultTable = new DataTable();
using var conn = new SqlConnection(connectionString);
const string sql = "SELECT * FROM Tennats";
using var cmd = new SqlCommand(sql, conn);
using var adapter = new SqlDataAdapter(cmd);
adapter.Fill(resultTable);
table.ImportDataTable(resultTable, true, 1, 1);
// Add table object to first page of input document
document.Pages[1].Paragraphs.Add(table);
using var streamOut = new MemoryStream();
// Save PDF document
document.Save(streamOut);
return new FileContentResult(streamOut.ToArray(), "application/pdf")
{
PageInfo = new Aspose.Pdf.PageInfo { Margin = new Aspose.Pdf.MarginInfo(28, 28, 28, 42) }
};
}