Hi comunidadmexi...,
Check this example. Now please take its reference and correct your code.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
Namespaces
C#
using System.IO;
using System.Configuration;
using System.Data.SqlClient;
using iText.Kernel.Colors;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
using Table = iText.Layout.Element.Table;
using iText.IO.Font.Constants;
using iText.Kernel.Font;
using iText.Layout.Borders;
VB.Net
Imports System.IO
Imports System.Configuration
Imports System.Data.SqlClient
Imports iText.Kernel.Colors
Imports iText.Kernel.Geom
Imports iText.Kernel.Pdf
Imports iText.Layout
Imports iText.Layout.Element
Imports iText.Layout.Properties
Imports Table = iText.Layout.Element.Table
Imports iText.IO.Font.Constants
Imports iText.Kernel.Font
Imports iText.Layout.Borders
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
string dest = Server.MapPath("~/Files/table.pdf");
GeneratePdf(dest);
}
private void GeneratePdf(string dest)
{
FileInfo file = new FileInfo(dest);
file.Directory.Create();
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
Document document = new Document(pdfDoc, new PageSize(595, 842));
document.SetMargins(0, 0, 0, 0);
float[] columnWidthsman = { 5, 5, 5, 5 };
Table tableman = new Table(UnitValue.CreatePercentArray(columnWidthsman));
PdfFont fman = PdfFontFactory.CreateFont(StandardFonts.HELVETICA);
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "SELECT TOP 10 ProductName 'fld3', QuantityPerUnit 'fld4', UnitPrice 'fld1'," +
" UnitsInStock 'fld2' FROM products ORDER BY fld3";
SqlConnection con = new SqlConnection(conString);
SqlCommand cmd = new SqlCommand(query);
cmd.Connection = con;
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
Cell cellman = new Cell(1, 4)
.Add(new Paragraph("Header"))
.SetFont(fman)
.SetFontSize(13)
.SetFontColor(DeviceGray.WHITE)
.SetBackgroundColor(DeviceGray.BLACK)
.SetTextAlignment(TextAlignment.CENTER)
.SetBorder(new SolidBorder(ColorConstants.GRAY, 2));
// Add Header cell.
tableman.AddHeaderCell(cellman);
Cell cellman1 = new Cell(1, 1)
.Add(new Paragraph("cell1"))
.SetFont(fman)
.SetFontSize(13)
.SetFontColor(DeviceGray.BLACK)
.SetBackgroundColor(new DeviceGray(0.75f))
.SetTextAlignment(TextAlignment.CENTER)
.SetBorder(new SolidBorder(ColorConstants.GRAY, 2));
// Add cell 1.
tableman.AddHeaderCell(cellman1);
Cell cellman2 = new Cell(1, 1)
.Add(new Paragraph("cell2"))
.SetFont(fman)
.SetFontSize(13)
.SetFontColor(DeviceGray.BLACK)
.SetBackgroundColor(new DeviceGray(0.75f))
.SetTextAlignment(TextAlignment.CENTER)
.SetBorder(new SolidBorder(ColorConstants.GRAY, 2));
// Add cell 2.
tableman.AddHeaderCell(cellman2);
Cell cellman3 = new Cell(1, 1)
.Add(new Paragraph("cell3"))
.SetFont(fman)
.SetFontSize(13)
.SetFontColor(DeviceGray.BLACK)
.SetBackgroundColor(new DeviceGray(0.75f))
.SetTextAlignment(TextAlignment.CENTER)
.SetBorder(new SolidBorder(ColorConstants.GRAY, 2));
// Add cell 3.
tableman.AddHeaderCell(cellman3);
Cell cellman4 = new Cell(1, 1)
.Add(new Paragraph("cell4"))
.SetFont(fman)
.SetFontSize(13)
.SetFontColor(DeviceGray.BLACK)
.SetBackgroundColor(new DeviceGray(0.75f))
.SetTextAlignment(TextAlignment.CENTER)
.SetBorder(new SolidBorder(ColorConstants.GRAY, 2));
// Add cell 4.
tableman.AddHeaderCell(cellman4);
while (reader.Read())
{
tableman.AddCell(new Cell().SetTextAlignment(TextAlignment.CENTER).
Add(new Paragraph(Convert.ToInt32(reader["fld1"]).ToString("N0"))));
tableman.AddCell(new Cell().SetTextAlignment(TextAlignment.CENTER).
Add(new Paragraph(Convert.ToInt32(reader["fld2"]).ToString("N0"))));
tableman.AddCell(new Cell().SetTextAlignment(TextAlignment.LEFT).
Add(new Paragraph(reader["fld3"].ToString().Trim())));
tableman.AddCell(new Cell().SetTextAlignment(TextAlignment.LEFT).
Add(new Paragraph(reader["fld4"].ToString().Trim())));
}
document.Add(tableman);
document.Close();
con.Close();
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim dest As String = Server.MapPath("~/Files/table.pdf")
GeneratePdf(dest)
End Sub
Private Sub GeneratePdf(ByVal dest As String)
Dim file As FileInfo = New FileInfo(dest)
file.Directory.Create()
Dim pdfDoc As PdfDocument = New PdfDocument(New PdfWriter(dest))
Dim document As Document = New Document(pdfDoc, New PageSize(595, 842))
document.SetMargins(0, 0, 0, 0)
Dim columnWidthsman As Single() = {5, 5, 5, 5}
Dim tableman As Table = New Table(UnitValue.CreatePercentArray(columnWidthsman))
Dim fman As PdfFont = PdfFontFactory.CreateFont(StandardFonts.HELVETICA)
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim query As String = "SELECT TOP 10 ProductName 'fld3', QuantityPerUnit 'fld4', UnitPrice 'fld1'," &
" UnitsInStock 'fld2' FROM products ORDER BY fld3"
Dim con As SqlConnection = New SqlConnection(conString)
Dim cmd As SqlCommand = New SqlCommand(query)
cmd.Connection = con
con.Open()
Dim reader As SqlDataReader = cmd.ExecuteReader()
Dim cellman As Cell = New Cell(1, 4) _
.Add(New Paragraph("Header")) _
.SetFont(fman).SetFontSize(13) _
.SetFontColor(DeviceGray.WHITE) _
.SetBackgroundColor(DeviceGray.BLACK) _
.SetTextAlignment(TextAlignment.CENTER) _
.SetBorder(New SolidBorder(ColorConstants.GRAY, 2))
tableman.AddHeaderCell(cellman)
Dim cellman1 As Cell = New Cell(1, 1) _
.Add(New Paragraph("cell1")) _
.SetFont(fman) _
.SetFontSize(13) _
.SetFontColor(DeviceGray.BLACK) _
.SetBackgroundColor(New DeviceGray(0.75F)) _
.SetTextAlignment(TextAlignment.CENTER) _
.SetBorder(New SolidBorder(ColorConstants.GRAY, 2))
tableman.AddHeaderCell(cellman1)
Dim cellman2 As Cell = New Cell(1, 1) _
.Add(New Paragraph("cell2")) _
.SetFont(fman) _
.SetFontSize(13) _
.SetFontColor(DeviceGray.BLACK) _
.SetBackgroundColor(New DeviceGray(0.75F)) _
.SetTextAlignment(TextAlignment.CENTER) _
.SetBorder(New SolidBorder(ColorConstants.GRAY, 2))
tableman.AddHeaderCell(cellman2)
Dim cellman3 As Cell = New Cell(1, 1) _
.Add(New Paragraph("cell3")) _
.SetFont(fman).SetFontSize(13) _
.SetFontColor(DeviceGray.BLACK) _
.SetBackgroundColor(New DeviceGray(0.75F)) _
.SetTextAlignment(TextAlignment.CENTER) _
.SetBorder(New SolidBorder(ColorConstants.GRAY, 2))
tableman.AddHeaderCell(cellman3)
Dim cellman4 As Cell = New Cell(1, 1) _
.Add(New Paragraph("cell4")) _
.SetFont(fman).SetFontSize(13) _
.SetFontColor(DeviceGray.BLACK) _
.SetBackgroundColor(New DeviceGray(0.75F)) _
.SetTextAlignment(TextAlignment.CENTER) _
.SetBorder(New SolidBorder(ColorConstants.GRAY, 2))
tableman.AddHeaderCell(cellman4)
While reader.Read()
tableman.AddCell(New Cell().SetTextAlignment(TextAlignment.CENTER) _
.Add(New Paragraph(Convert.ToInt32(reader("fld1")).ToString("N0"))))
tableman.AddCell(New Cell().SetTextAlignment(TextAlignment.CENTER) _
.Add(New Paragraph(Convert.ToInt32(reader("fld2")).ToString("N0"))))
tableman.AddCell(New Cell().SetTextAlignment(TextAlignment.LEFT) _
.Add(New Paragraph(reader("fld3").ToString().Trim())))
tableman.AddCell(New Cell().SetTextAlignment(TextAlignment.LEFT) _
.Add(New Paragraph(reader("fld4").ToString().Trim())))
End While
document.Add(tableman)
document.Close()
con.Close()
End Sub
Generated PDF