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 Web page and Image populated with base64String to PDF using iTextSharp in ASP.Net<p>Hi <a class="username" rel="milepredy"> milepredy</a>,</p> <blockquote><cite>milepredy says:</cite> <pre>iTextSharp.text.Image picture = iTextSharp.text.Image.GetInstance(imageURL);</pre> </blockquote> <p>&nbsp;You cant pass directly Image url (base64 string) to the GetInstance method.</p> <p>You need to remove <strong>data:Image/png;base64,</strong> from the string.</p> <p>Check this example. Now please take its reference and correct your code.</p> <p><strong><span style="text-decoration: underline;">HTML</span></strong></p> <pre class="brush: html">&lt;asp:Image ID=&#34;prvimage&#34; runat=&#34;server&#34; /&gt; &lt;br /&gt; &lt;table border=&#34;1&#34; width=&#34;100&#34;&gt; &lt;tr&gt; &lt;td&gt;Name&lt;/td&gt; &lt;td&gt;Age&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;John&lt;/td&gt; &lt;td&gt;11&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Sam&lt;/td&gt; &lt;td&gt;13&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Tony&lt;/td&gt; &lt;td&gt;12&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;br /&gt; &lt;asp:Button ID=&#34;btnExport&#34; runat=&#34;server&#34; Text=&#34;Export&#34; OnClick=&#34;btnExport_Click&#34; /&gt;</pre> <p><strong><span style="text-decoration: underline;">Namespace</span></strong></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.IO; using System.Web; using System.Web.UI; using iTextSharp.text; using iTextSharp.text.pdf; using iTextSharp.tool.xml;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.IO Imports System.Web Imports System.Web.UI Imports iTextSharp.text Imports iTextSharp.text.pdf Imports iTextSharp.tool.xml</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) { //&nbsp;Bind the image from database. string strBase64 = @"/9j/4AAQSkZJ...."; if (strBase64 != null) { prvimage.ImageUrl = "data:Image/png;base64," + strBase64; } else { Response.Write("Must upload an Image"); } } protected void btnExport_Click(object sender, EventArgs e) { using (StringWriter sw = new StringWriter()) { using (HtmlTextWriter hw = new HtmlTextWriter(sw)) { form1.RenderControl(hw); StringReader sr = new StringReader(sw.ToString()); Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f); PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream); pdfDoc.Open(); string imageURL = prvimage.ImageUrl; iTextSharp.text.Image picture = iTextSharp.text.Image.GetInstance(Convert.FromBase64String(prvimage.ImageUrl.Replace("data:Image/png;base64,", ""))); picture.ScaleToFit(105F, 120F); pdfDoc.Add(picture); XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr); pdfDoc.Close(); Response.ContentType = "application/pdf"; Response.AddHeader("content-disposition", "attachment;filename=DigiCV.pdf"); Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.Write(pdfDoc); Response.End(); } } } public override void VerifyRenderingInServerForm(Control control) { /* Verifies that the control is rendered */ }</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 ' Bind the image from database. Dim strBase64 As String = "/9j/4AAQSkZJ...." If strBase64 IsNot Nothing Then prvimage.ImageUrl = "data:Image/png;base64," &amp; strBase64 Else Response.Write("Must upload an Image") End If End Sub Protected Sub btnExport_Click(ByVal sender As Object, ByVal e As EventArgs) Using sw As StringWriter = New StringWriter() Using hw As HtmlTextWriter = New HtmlTextWriter(sw) form1.RenderControl(hw) Dim sr As StringReader = New StringReader(sw.ToString()) Dim pdfDoc As Document = New Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 0.0F) Dim writer As PdfWriter = PdfWriter.GetInstance(pdfDoc, Response.OutputStream) pdfDoc.Open() Dim imageURL As String = prvimage.ImageUrl Dim picture As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(Convert.FromBase64String(prvimage.ImageUrl.Replace("data:Image/png;base64,", ""))) picture.ScaleToFit(105.0F, 120.0F) pdfDoc.Add(picture) XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr) pdfDoc.Close() Response.ContentType = "application/pdf" Response.AddHeader("content-disposition", "attachment;filename=DigiCV.pdf") Response.Cache.SetCacheability(HttpCacheability.NoCache) Response.Write(pdfDoc) Response.End() End Using End Using End Sub Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control) End Sub</pre> <p><strong><span style="text-decoration: underline;">Screenshot</span></strong></p> <p><img src="https://i.imgur.com/gTktn3T.gif" alt="" width="296" height="255" /></p>https://www.aspforums.net:443/Threads/464111/Export-Web-page-and-Image-populated-with-base64String-to-PDF-using-iTextSharp-in-ASPNet/https://www.aspforums.net:443/Threads/464111/Export-Web-page-and-Image-populated-with-base64String-to-PDF-using-iTextSharp-in-ASPNet/Tue, 13 Aug 2019 05:58:35 GMT