Hi jlahrachdev,
Calculate the width as per the columns coumn.
Refer below sample.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
<asp:Button ID="btnReport" runat="server" Text="Generate Report" OnClick="GenerateReport" />
Namespaces
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Collections.Generic
Code
Private Function GetData(query As String) As DataTable
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim cmd As New SqlCommand(query)
Using con As New SqlConnection(conString)
Using sda As New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As New DataTable()
sda.Fill(dt)
Return dt
End Using
End Using
End Using
End Function
Protected Sub GenerateReport(ByVal sender As Object, ByVal e As EventArgs)
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Dim pdfDoc As iTextSharp.text.Document = New iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 0, 0, 0, 0)
Dim bf As iTextSharp.text.pdf.BaseFont = iTextSharp.text.pdf.BaseFont.CreateFont(Environment.GetEnvironmentVariable("windir") + "\fonts\calibrib.ttf", iTextSharp.text.pdf.BaseFont.IDENTITY_H, True)
Dim font As New iTextSharp.text.Font(bf, 11, iTextSharp.text.Font.NORMAL)
Dim font1 As New iTextSharp.text.Font(bf, 13, iTextSharp.text.Font.NORMAL)
Try
Dim writer As iTextSharp.text.pdf.PdfWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
pdfDoc.Open()
Dim PdfTable As iTextSharp.text.pdf.PdfPTable = New iTextSharp.text.pdf.PdfPTable(1)
PdfTable.SpacingBefore = 10.0F
Dim imageURL As String = Server.MapPath("~/Images/northwindlogo.gif")
Dim jpg As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(imageURL)
jpg.ScaleToFit(50.0F, 50.0F)
'jpg.SpacingBefore = 10.0F
jpg.SpacingAfter = 1.0F
Dim PdfPCellh As iTextSharp.text.pdf.PdfPCell = New iTextSharp.text.pdf.PdfPCell(New iTextSharp.text.Jpeg(jpg))
PdfPCellh.Border = iTextSharp.text.Rectangle.NO_BORDER
'PdfPCellh.HorizontalAlignment = iText.Layout.Properties.HorizontalAlignment.CENTER
PdfTable.AddCell(PdfPCellh)
pdfDoc.Add(PdfTable)
Dim dt1 As DataTable = GetData("SELECT TOP 2 EmployeeId, FirstName, LastName, BirthDate, HireDate, Address, City, Country FROM Employees")
Dim columnNames = New List(Of String)
For Each column As DataColumn In dt1.Columns
columnNames.Add(column.ColumnName)
Next
con.Close()
Dim PdfTable2 As iTextSharp.text.pdf.PdfPTable = New iTextSharp.text.pdf.PdfPTable(dt1.Columns.Count)
PdfTable2.SpacingBefore = 10.0F
PdfTable2.WidthPercentage = 90
PdfTable2.SetWidths(GetHeaderWidths(font, columnNames.ToArray()))
For columns As Integer = 0 To dt1.Columns.Count - 1
Dim Cell As iTextSharp.text.pdf.PdfPCell = Nothing
Cell = New iTextSharp.text.pdf.PdfPCell(New iTextSharp.text.Phrase(dt1.Columns(columns).ColumnName, font))
Cell.RunDirection = iTextSharp.text.pdf.PdfWriter.RUN_DIRECTION_RTL
'Cell.HorizontalAlignment = iText.Layout.Properties.HorizontalAlignment.LEFT
Cell.BackgroundColor = New iTextSharp.text.pdf.CMYKColor(0, 20, 60, 0)
PdfTable2.AddCell(Cell)
Next
For row As Integer = 0 To dt1.Rows.Count - 1
For Columns As Integer = 0 To dt1.Columns.Count - 1
Dim Cell As iTextSharp.text.pdf.PdfPCell = Nothing
Cell = New iTextSharp.text.pdf.PdfPCell(New iTextSharp.text.Phrase(dt1.Rows(row)(Columns).ToString(), font))
Cell.RunDirection = iTextSharp.text.pdf.PdfWriter.RUN_DIRECTION_RTL
Cell.HorizontalAlignment = HorizontalAlign.Left
PdfTable2.AddCell(Cell)
Next
Next
pdfDoc.Add(PdfTable2)
Dim PdfTable3 As iTextSharp.text.pdf.PdfPTable = New iTextSharp.text.pdf.PdfPTable(1)
PdfTable3.SpacingBefore = 10.0F
Dim imageURL2 As String = Server.MapPath("~/Images/northwindlogo.gif")
Dim jpg2 As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(imageURL2)
jpg2.ScaleToFit(50.0F, 50.0F)
jpg.SpacingBefore = 10.0F
jpg2.SpacingAfter = 1.0F
Dim PdfPCellf As iTextSharp.text.pdf.PdfPCell = New iTextSharp.text.pdf.PdfPCell(New iTextSharp.text.Jpeg(jpg2))
PdfPCellf.Border = iTextSharp.text.Rectangle.NO_BORDER
PdfPCellf.HorizontalAlignment = HorizontalAlign.Center
PdfTable3.AddCell(PdfPCellf)
pdfDoc.Add(PdfTable3)
pdfDoc.Close()
Response.ContentType = "application/pdf"
Response.AddHeader("content-disposition", "attachment; filename=myfile.pdf")
Response.ContentEncoding = Encoding.UTF8
System.Web.HttpContext.Current.Response.Write(pdfDoc)
Response.Flush()
Response.End()
Catch de As iTextSharp.text.DocumentException
Catch ioEx As System.IO.IOException
Catch ex As Exception
End Try
End Using
End Sub
Public Function GetHeaderWidths(ByVal font As Font, headers As String()) As Integer()
Dim total = 0
Dim columns = headers.Length
Dim widths = New Integer(columns - 1) {}
For i = 0 To columns - 1
Dim w = font.GetCalculatedBaseFont(True).GetWidth(headers(i))
total += w
widths(i) = w
Next
Dim result = New Integer(columns - 1) {}
For i = 0 To columns - 1
result(i) = CInt(widths(i)) / total * 100
Next
Return result
End Function
Screenshot