Rendre un tableau à partir de la source de données
Aspose.PDF vous permet de créer le tableau avec DataSource à partir de DataSet, Data Table, tableaux et objets IEnumerable en utilisant la classe PdfLightTable.
La classe Table est utilisée pour traiter les tableaux. Cette classe nous donne la possibilité de créer des tableaux et de les placer dans le document, en utilisant Rows et Cells . Donc, pour créer le tableau, vous devez ajouter le nombre requis de lignes et les remplir avec le nombre approprié de cellules.
L’exemple suivant crée le tableau 4x10.
.NET Core 3.1
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddTable ()
{
// The path to the documents directory
var dataDir = RunExamples . GetDataDir_AsposePdf_WorkingDocuments ();
// Create PDF document
using ( var document = new Aspose . Pdf . Document ())
{
// Add page
var page = document . Pages . Add ();
var table = new Aspose . Pdf . Table
{
// Set column auto 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 ),
};
for ( var rowCount = 0 ; rowCount < 10 ; rowCount ++)
{
// Add row to table
var row = table . Rows . Add ();
// Add table cells
for ( int i = 0 ; i < 4 ; i ++)
{
row . Cells . Add ( $"Cell ({i + 1}, {rowCount + 1})" );
}
}
// Add table object to first page of input document
page . Paragraphs . Add ( table );
// Save PDF document
document . Save ( dataDir + "AddTable_out.pdf" );
}
}
.NET 8
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddTable ()
{
// The path to the documents directory
var dataDir = RunExamples . GetDataDir_AsposePdf_WorkingDocuments ();
// Create PDF document
using var document = new Aspose . Pdf . Document ();
// Add page
var page = document . Pages . Add ();
var table = new Aspose . Pdf . Table
{
// Set column auto 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 ),
};
for ( var rowCount = 0 ; rowCount < 10 ; rowCount ++)
{
// Add row to table
var row = table . Rows . Add ();
// Add table cells
for ( int i = 0 ; i < 4 ; i ++)
{
row . Cells . Add ( $"Cell ({i + 1}, {rowCount + 1})" );
}
}
// Add table object to first page of input document
page . Paragraphs . Add ( table );
// Save PDF document
document . Save ( dataDir + "AddTable_out.pdf" );
}
Lors de l’initialisation de l’objet Table, les paramètres de base minimaux ont été utilisés :
ColumnWidths - largeur des colonnes (par défaut).
DefaultCellPadding - les champs par défaut pour la cellule du tableau.
Border - attributs du cadre du tableau (style, épaisseur, couleur).
DefaultCellBorder - attributs du cadre de la cellule (style, épaisseur, couleur).
Exportation de données à partir d’un tableau d’objets
La classe Table fournit des méthodes pour interagir avec les sources de données ADO.NET - ImportDataTable et ImportDataView . La première méthode importe des données à partir du DataTable, la seconde à partir du DataView.
En supposant que ces objets ne sont pas très pratiques pour travailler dans le modèle MVC, nous nous limiterons à un bref exemple. Dans cet exemple (ligne 50), la méthode ImportDataTable est appelée et reçoit comme paramètres une instance de DataTable et des paramètres supplémentaires comme le drapeau d’en-tête et la position initiale (lignes/colonnes) pour la sortie des données.
.NET Core 3.1
// 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"
};
}
}
}
.NET 8
// 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 ) }
};
}