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.Export ASP.Net DataList with Image to PDF using C# and VB.Net<p>Hi <a class="username" rel="salini"> salini</a>,</p> <p>Refer below sample.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;asp:DataList ID=&#34;DataList1&#34; RepeatColumns=&#34;2&#34; runat=&#34;server&#34;&gt; &lt;ItemTemplate&gt; &lt;table&gt; &lt;tr&gt; &lt;td&gt;&lt;div&gt;&lt;asp:Image ID=&#34;imgPicture&#34; runat=&#34;server&#34; CssClass=&#34;image&#34; ImageUrl=&#39;&lt;%# Eval(&#34;Path&#34;, GetUrl(&#34;{0}&#34;)) %&gt;&#39;&gt;&lt;/asp:Image&gt;&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td align=&#34;left&#34;&gt;Name&lt;/td&gt; &lt;td&gt;:&lt;/td&gt; &lt;td&gt;&lt;asp:Label ID=&#34;Label14&#34; Text=&#39;&lt;%# DataBinder.Eval(Container.DataItem, &#34;Name&#34;)%&gt;&#39; runat=&#34;server&#34;&gt;&lt;/asp:Label&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td align=&#34;left&#34;&gt;Id&lt;/td&gt; &lt;td&gt;:&lt;/td&gt; &lt;td colspan=&#34;6&#34; align=&#34;left&#34;&gt;&lt;asp:Label ID=&#34;Label7&#34; Text=&#39;&lt;%# DataBinder.Eval(Container.DataItem, &#34;Id&#34;)%&gt;&#39; runat=&#34;server&#34;&gt;&lt;/asp:Label&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/ItemTemplate&gt; &lt;/asp:DataList&gt; &lt;asp:Button Text=&#34;ExportToPdf&#34; runat=&#34;server&#34; OnClick=&#34;PDF_Export&#34; /&gt;</pre> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Data; using System.IO; using System.Data.SqlClient; using System.Configuration; using iTextSharp.text; using iTextSharp.text.pdf; using iTextSharp.text.html.simpleparser;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.IO Imports iTextSharp.text Imports iTextSharp.text.html.simpleparser Imports iTextSharp.text.pdf Imports System.Data.SqlClient Imports System.Data</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString)) { using (SqlCommand cmd = new SqlCommand(&#34;select TOP 2 Id, Name, Path from tblFilespath order by ID&#34;, con)) { using (SqlDataAdapter da = new SqlDataAdapter(cmd)) { DataTable dt = new DataTable(); da.Fill(dt); DataList1.DataSource = dt; DataList1.DataBind(); } } } } } protected string GetUrl(string imagepath) { string[] splits = Request.Url.AbsoluteUri.Split(&#39;/&#39;); if (splits.Length &gt;= 2) { string url = splits[0] + &#34;//&#34;; for (int i = 2; i &lt; splits.Length - 1; i++) { url += splits[i]; url += &#34;/&#34;; } return url + imagepath; } return imagepath; } protected void PDF_Export(object sender, EventArgs e) { Response.ContentType = &#34;application/pdf&#34;; Response.AddHeader(&#34;content-disposition&#34;, &#34;attachment;filename=GridViewExport.pdf&#34;); Response.Cache.SetCacheability(HttpCacheability.NoCache); StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); DataList1.RenderControl(hw); StringReader sr = new StringReader(sw.ToString()); Document pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f); HTMLWorker htmlparser = new HTMLWorker(pdfDoc); PdfWriter.GetInstance(pdfDoc, Response.OutputStream); pdfDoc.Open(); htmlparser.Parse(sr); pdfDoc.Close(); Response.Write(pdfDoc); Response.End(); }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load If Not Me.IsPostBack Then Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString) Using cmd As SqlCommand = New SqlCommand(&#34;select TOP 2 Id, Name, Path from tblFilespath order by ID&#34;, con) Using da As SqlDataAdapter = New SqlDataAdapter(cmd) Dim dt As DataTable = New DataTable() da.Fill(dt) DataList1.DataSource = dt DataList1.DataBind() End Using End Using End Using End If End Sub Protected Function GetUrl(ByVal imagepath As String) As String Dim splits As String() = Request.Url.AbsoluteUri.Split(&#34;/&#34;c) If splits.Length &gt;= 2 Then Dim url As String = splits(0) &amp; &#34;//&#34; For i As Integer = 2 To splits.Length - 1 - 1 url += splits(i) url += &#34;/&#34; Next Return url &amp; imagepath End If Return imagepath End Function Protected Sub PDF_Export(ByVal sender As Object, ByVal e As EventArgs) Response.ContentType = &#34;application/pdf&#34; Response.AddHeader(&#34;content-disposition&#34;, &#34;attachment;filename=GridViewExport.pdf&#34;) Response.Cache.SetCacheability(HttpCacheability.NoCache) Dim sw As StringWriter = New StringWriter() Dim hw As HtmlTextWriter = New HtmlTextWriter(sw) DataList1.RenderControl(hw) Dim sr As StringReader = New StringReader(sw.ToString()) Dim pdfDoc As Document = New Document(PageSize.A2, 7.0F, 7.0F, 7.0F, 0.0F) Dim htmlparser As HTMLWorker = New HTMLWorker(pdfDoc) PdfWriter.GetInstance(pdfDoc, Response.OutputStream) pdfDoc.Open() htmlparser.Parse(sr) pdfDoc.Close() Response.Write(pdfDoc) Response.End() End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://imgur.com/U7D5oHI.gif" alt="" width="464" height="338" /></p>https://www.aspforums.net:443/Threads/512984/Export-ASPNet-DataList-with-Image-to-PDF-using-C-and-VBNet/https://www.aspforums.net:443/Threads/512984/Export-ASPNet-DataList-with-Image-to-PDF-using-C-and-VBNet/Mon, 11 Mar 2019 05:39:09 GMT