Render table from the data source
Aspose.PDF allows you to create the table with DataSource from DataSet, Data Table, arrays and IEnumerable objects using PdfLightTable class
The Table class is used to process tables. This class gives us the ability to create tables and place them in the document, using Rows and Cells . So, to create the table, you need to add the required number of rows and fill them with the appropriate number of cells.
The following example creates the table 4x10.
.NET Core 3.1
Copy
private static void AddTable ( )
{
var dataDir = RunExamples . GetDataDir_AsposePdf_WorkingDocuments ();
using ( var document = new Aspose . Pdf . Document ())
{
var page = document . Pages . Add ();
var table = new Aspose . Pdf . Table
{
ColumnWidths = "25% 25% 25% 25%" ,
DefaultCellPadding = new Aspose . Pdf . MarginInfo ( 10 , 5 , 10 , 5 ),
Border = new Aspose . Pdf . BorderInfo ( Aspose . Pdf . BorderSide . All , . 5 f, Aspose . Pdf . Color . Green ),
DefaultCellBorder = new Aspose . Pdf . BorderInfo ( Aspose . Pdf . BorderSide . All , . 2 f, Aspose . Pdf . Color . Green ),
};
for ( var rowCount = 0 ; rowCount < 10 ; rowCount ++)
{
var row = table . Rows . Add ();
for ( int i = 0 ; i < 4 ; i ++)
{
row . Cells . Add ( $"Cell ({i + 1 } , {rowCount + 1 } )" );
}
}
page . Paragraphs . Add ( table );
document . Save ( dataDir + "AddTable_out.pdf" );
}
}
.NET 8
Copy
private static void AddTable ( )
{
var dataDir = RunExamples . GetDataDir_AsposePdf_WorkingDocuments ();
using var document = new Aspose . Pdf . Document ();
var page = document . Pages . Add ();
var table = new Aspose . Pdf . Table
{
ColumnWidths = "25% 25% 25% 25%" ,
DefaultCellPadding = new Aspose . Pdf . MarginInfo ( 10 , 5 , 10 , 5 ),
Border = new Aspose . Pdf . BorderInfo ( Aspose . Pdf . BorderSide . All , . 5 f, Aspose . Pdf . Color . Green ),
DefaultCellBorder = new Aspose . Pdf . BorderInfo ( Aspose . Pdf . BorderSide . All , . 2 f, Aspose . Pdf . Color . Green ),
};
for ( var rowCount = 0 ; rowCount < 10 ; rowCount ++)
{
var row = table . Rows . Add ();
for ( int i = 0 ; i < 4 ; i ++)
{
row . Cells . Add ( $"Cell ({i + 1 } , {rowCount + 1 } )" );
}
}
page . Paragraphs . Add ( table );
document . Save ( dataDir + "AddTable_out.pdf" );
}
When initializing the Table object, the minimal skin settings were used:
Exporting data from an array of objects
The Table class provides methods for interacting with ADO.NET data sources - ImportDataTable and ImportDataView . The first method imports data from the DataTable, the second from the DataView.
Premising that these objects are not very convenient for working in the MVC template, we will limit ourselves to a brief example. In this example (line 50), the ImportDataTable method is called and receives as parameters a DataTable instance and additional settings like the header flag and the initial position (rows/cols) for the data output.
.NET Core 3.1
Copy
private static void AddTable ( )
{
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
{
ColumnWidths = "25% 25% 25% 25%" ,
DefaultCellPadding = new Aspose . Pdf . MarginInfo ( 10 , 5 , 10 , 5 ),
Border = new Aspose . Pdf . BorderInfo ( Aspose . Pdf . BorderSide . All , . 5 f, Aspose . Pdf . Color . Green ),
DefaultCellBorder = new Aspose . Pdf . BorderInfo ( Aspose . Pdf . BorderSide . All , . 2 f, 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 );
document . Pages [ 1 ]. Paragraphs . Add ( table );
using ( var streamOut = new MemoryStream ())
{
document . Save ( streamOut );
return new FileContentResult ( streamOut . ToArray (), "application/pdf" )
{
FileDownloadName = "demotable2.pdf"
};
}
}
}
.NET 8
Copy
private static void AddTable ( )
{
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
{
ColumnWidths = "25% 25% 25% 25%" ,
DefaultCellPadding = new Aspose . Pdf . MarginInfo ( 10 , 5 , 10 , 5 ),
Border = new Aspose . Pdf . BorderInfo ( Aspose . Pdf . BorderSide . All , . 5 f, Aspose . Pdf . Color . Green ),
DefaultCellBorder = new Aspose . Pdf . BorderInfo ( Aspose . Pdf . BorderSide . All , . 2 f, 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 );
document . Pages [ 1 ]. Paragraphs . Add ( table );
using var streamOut = new MemoryStream ();
document . Save ( streamOut );
return new FileContentResult ( streamOut . ToArray (), "application/pdf" )
{
PageInfo = new Aspose . Pdf . PageInfo { Margin = new Aspose . Pdf . MarginInfo ( 28 , 28 , 28 , 42 ) }
};
}