ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Generate Table and set cell color using iTextSharp in ASP.Net<p>Hi <a class="username" rel="sureshMGR"> sureshMGR</a>,</p> <p>Check this example. Now please take its reference and correct your code.</p> <p><strong><span style="text-decoration: underline;">Namespaces</span></strong></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using iTextSharp.text; using iTextSharp.text.pdf;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports iTextSharp.text Imports iTextSharp.text.pdf</pre> <p><strong><span style="text-decoration: underline;">Code</span></strong></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { Document document = new Document(PageSize.A4, 88f, 88f, 10f, 10f); using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream()) { PdfWriter writer = PdfWriter.GetInstance(document, memoryStream); PdfPCell cell = null; PdfPTable table = null; document.Open(); table = new PdfPTable(3); table.TotalWidth = 400f; table.LockedWidth = true; table.SetWidths(new float[] { 4f, 4f, 4f }); table.HorizontalAlignment = 0; table.SpacingBefore = 20f; table.SpacingAfter = 30f; cell = new PdfPCell(new Phrase(&#34;Header spanning 3 columns&#34;)); cell.Colspan = 3; cell.BackgroundColor = new BaseColor(System.Drawing.Color.Red); cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right table.AddCell(cell); cell = new PdfPCell(new Phrase(&#34;Col 1 Row 1&#34;)); cell.BackgroundColor = new BaseColor(System.Drawing.Color.Yellow); table.AddCell(cell); cell = new PdfPCell(new Phrase(&#34;Col 2 Row 1&#34;)); cell.BackgroundColor = new BaseColor(System.Drawing.Color.Gray); table.AddCell(cell); cell = new PdfPCell(new Phrase(&#34;Col 3 Row 1&#34;)); cell.BackgroundColor = new BaseColor(System.Drawing.Color.LimeGreen); table.AddCell(cell); cell = new PdfPCell(new Phrase(&#34;Col 1 Row 2&#34;)); cell.BackgroundColor = new BaseColor(System.Drawing.Color.Yellow); table.AddCell(cell); cell = new PdfPCell(new Phrase(&#34;Col 2 Row 2&#34;)); cell.BackgroundColor = new BaseColor(System.Drawing.Color.Gray); table.AddCell(cell); cell = new PdfPCell(new Phrase(&#34;Col 3 Row 2&#34;)); cell.BackgroundColor = new BaseColor(System.Drawing.Color.LimeGreen); table.AddCell(cell); document.Add(table); document.Close(); byte[] bytes = memoryStream.ToArray(); memoryStream.Close(); Response.Clear(); Response.ContentType = &#34;application/pdf&#34;; Response.AddHeader(&#34;Content-Disposition&#34;, &#34;attachment; filename=Test.pdf&#34;); Response.ContentType = &#34;application/pdf&#34;; Response.Buffer = true; Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.BinaryWrite(bytes); Response.End(); Response.Close(); } }</pre> <p><strong><span style="text-decoration: underline;">VB.</span><span style="text-decoration: underline;">Net</span></strong></p> <pre class="brush: vb">Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load Dim document As Document = New Document(PageSize.A4, 88.0F, 88.0F, 10.0F, 10.0F) Using memoryStream As System.IO.MemoryStream = New System.IO.MemoryStream() Dim writer As PdfWriter = PdfWriter.GetInstance(document, memoryStream) Dim cell As PdfPCell = Nothing Dim table As PdfPTable = Nothing document.Open() table = New PdfPTable(3) table.TotalWidth = 400.0F table.LockedWidth = True table.SetWidths(New Single() {4.0F, 4.0F, 4.0F}) table.HorizontalAlignment = 0 table.SpacingBefore = 20.0F table.SpacingAfter = 30.0F cell = New PdfPCell(New Phrase(&#34;Header spanning 3 columns&#34;)) cell.Colspan = 3 cell.BackgroundColor = New BaseColor(System.Drawing.Color.Red) cell.HorizontalAlignment = 1 table.AddCell(cell) cell = New PdfPCell(New Phrase(&#34;Col 1 Row 1&#34;)) cell.BackgroundColor = New BaseColor(System.Drawing.Color.Yellow) table.AddCell(cell) cell = New PdfPCell(New Phrase(&#34;Col 2 Row 1&#34;)) cell.BackgroundColor = New BaseColor(System.Drawing.Color.Gray) table.AddCell(cell) cell = New PdfPCell(New Phrase(&#34;Col 3 Row 1&#34;)) cell.BackgroundColor = New BaseColor(System.Drawing.Color.LimeGreen) table.AddCell(cell) cell = New PdfPCell(New Phrase(&#34;Col 1 Row 2&#34;)) cell.BackgroundColor = New BaseColor(System.Drawing.Color.Yellow) table.AddCell(cell) cell = New PdfPCell(New Phrase(&#34;Col 2 Row 2&#34;)) cell.BackgroundColor = New BaseColor(System.Drawing.Color.Gray) table.AddCell(cell) cell = New PdfPCell(New Phrase(&#34;Col 3 Row 2&#34;)) cell.BackgroundColor = New BaseColor(System.Drawing.Color.LimeGreen) table.AddCell(cell) document.Add(table) document.Close() Dim bytes As Byte() = memoryStream.ToArray() memoryStream.Close() Response.Clear() Response.ContentType = &#34;application/pdf&#34; Response.AddHeader(&#34;Content-Disposition&#34;, &#34;attachment; filename=Test.pdf&#34;) Response.ContentType = &#34;application/pdf&#34; Response.Buffer = True Response.Cache.SetCacheability(HttpCacheability.NoCache) Response.BinaryWrite(bytes) Response.End() Response.Close() End Using End Sub</pre> <p><strong><span style="text-decoration: underline;">Screenshot</span></strong></p> <p><strong><span style="text-decoration: underline;"><img src="https://i.imgur.com/gjZpXeI.jpg" alt="" width="472" height="147" /></span></strong></p>https://www.aspforums.net:443/Threads/143352/Generate-Table-and-set-cell-color-using-iTextSharp-in-ASPNet/https://www.aspforums.net:443/Threads/143352/Generate-Table-and-set-cell-color-using-iTextSharp-in-ASPNet/Thu, 19 Jul 2018 05:07:42 GMT