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 Page content with Image to PDF using iTextSharp in ASP.Net<p>Hi <a class="username" rel="KatieNgoc"> KatieNgoc</a>,</p> <p>Place your content inside a Panel. Then export the Panel Content to pdf and save in folder.</p> <p>For more details refer below article.</p> <h1 class="header"><a title="Export HTML DIV contents to PDF using iTextSharp in ASP.Net'a" href="https://www.aspsnippets.com/Articles/Export-HTML-DIV-contents-to-PDF-using-iTextSharp-in-ASPNet.aspx">Export HTML DIV contents to PDF using iTextSharp in ASP.Net</a></h1> <p><strong><span style="text-decoration: underline;">HTML</span></strong></p> <pre class="brush: html">&lt;asp:Panel ID=&#34;pnlDetails&#34; runat=&#34;server&#34;&gt; &nbsp;&nbsp;&nbsp; &lt;asp:Image ID=&#34;imgPhoto&#34; ImageUrl=&#34;~/Penguins.jpg&#34; runat=&#34;server&#34; /&gt;&lt;br /&gt; &nbsp;&nbsp;&nbsp; &lt;p&gt;1. Get A Discount On A New Walk In Tub&lt;/p&gt;&lt;br /&gt; &nbsp;&nbsp;&nbsp; &lt;p&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Walk in tubs are a perfect addition for seniors living with mobility challenges &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; or anyone worried about fall related injuries. Statistics tell us that falls happen &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; in your bathroom more than any other room in the home.&lt;/p&gt;&lt;br /&gt; &nbsp;&nbsp;&nbsp; &lt;p&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; In fact two-thirds of all bathroom injuries happen near the shower or bath-tub, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; according to The Center for Disease Control. Most falls typically happen when your &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; stepping over the tub, slipping on a wet floor or when leaning on a glass door that &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; wasn&#39;t designed to support your weight&lt;/p&gt;&lt;br /&gt; &nbsp;&nbsp;&nbsp; &lt;p&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; A Walk-in tub is an investment in your future a simple modification to your bathroom &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; can change your life. Keeping your independence and dignity, plus peace of mind &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for your loved ones all ad up to a better quality of life. Right now, Walk-in tub &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; manufacturers all over the country are offering big senior discounts. Visit the &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; walk-in tub website to find discounts and get a free quote on a new Walk-in tub.&lt;/p&gt; &lt;/asp:Panel&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;">Namespaces</span></strong></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.IO; using System.Web.UI; using iTextSharp.text; using iTextSharp.text.html.simpleparser; using iTextSharp.text.pdf;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.IO Imports System.Web.UI Imports iTextSharp.text Imports iTextSharp.text.html.simpleparser 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 btnExport_Click(object sender, EventArgs e) { &nbsp;&nbsp;&nbsp; imgPhoto.ImageUrl = GetUrl("Penguins.jpg"); &nbsp;&nbsp;&nbsp; if (!Directory.Exists(Server.MapPath("~/Files/"))) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Directory.CreateDirectory(Server.MapPath("~/Files/")); &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; FileStream file = new FileStream(Server.MapPath("~/Files/") + "Test.PDF", FileMode.Create, System.IO.FileAccess.Write); &nbsp;&nbsp;&nbsp; StringWriter sw = new StringWriter(); &nbsp;&nbsp;&nbsp; HtmlTextWriter hw = new HtmlTextWriter(sw); &nbsp;&nbsp;&nbsp; pnlDetails.RenderControl(hw); &nbsp;&nbsp;&nbsp; StringReader sr = new StringReader(sw.ToString()); &nbsp;&nbsp;&nbsp; Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f); &nbsp;&nbsp;&nbsp; HTMLWorker htmlparser = new HTMLWorker(pdfDoc); &nbsp;&nbsp;&nbsp; MemoryStream ms = new MemoryStream(); &nbsp;&nbsp;&nbsp; PdfWriter.GetInstance(pdfDoc, file); &nbsp;&nbsp;&nbsp; pdfDoc.Open(); &nbsp;&nbsp;&nbsp; htmlparser.Parse(sr); &nbsp;&nbsp;&nbsp; pdfDoc.Close(); &nbsp;&nbsp;&nbsp; file.Close(); } public override void VerifyRenderingInServerForm(Control control) { &nbsp;&nbsp;&nbsp; /* Verifies that the control is rendered */ } protected string GetUrl(string imagepath) { &nbsp;&nbsp;&nbsp; string[] splits = Request.Url.AbsoluteUri.Split('/'); &nbsp;&nbsp;&nbsp; if (splits.Length &gt;= 2) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string url = splits[0] + "//"; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int i = 2; i &lt; splits.Length - 1; i++) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; url += splits[i]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; url += "/"; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return url + imagepath; &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; return imagepath; } </pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Protected Sub btnExport_Click(ByVal sender As Object, ByVal e As EventArgs) &nbsp;&nbsp;&nbsp; imgPhoto.ImageUrl = GetUrl(&#34;Penguins.jpg&#34;) &nbsp;&nbsp;&nbsp; If Not Directory.Exists(Server.MapPath(&#34;~/Files/&#34;)) Then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Directory.CreateDirectory(Server.MapPath(&#34;~/Files/&#34;)) &nbsp;&nbsp;&nbsp; End If &nbsp;&nbsp;&nbsp; Dim file As FileStream = New FileStream(Server.MapPath(&#34;~/Files/&#34;) &amp; &#34;Test.PDF&#34;, FileMode.Create, System.IO.FileAccess.Write) &nbsp;&nbsp;&nbsp; Dim sw As StringWriter = New StringWriter() &nbsp;&nbsp;&nbsp; Dim hw As HtmlTextWriter = New HtmlTextWriter(sw) &nbsp;&nbsp;&nbsp; pnlDetails.RenderControl(hw) &nbsp;&nbsp;&nbsp; Dim sr As StringReader = New StringReader(sw.ToString()) &nbsp;&nbsp;&nbsp; Dim pdfDoc As Document = New Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 0.0F) &nbsp;&nbsp;&nbsp; Dim htmlparser As HTMLWorker = New HTMLWorker(pdfDoc) &nbsp;&nbsp;&nbsp; Dim ms As MemoryStream = New MemoryStream() &nbsp;&nbsp;&nbsp; PdfWriter.GetInstance(pdfDoc, file) &nbsp;&nbsp;&nbsp; pdfDoc.Open() &nbsp;&nbsp;&nbsp; htmlparser.Parse(sr) &nbsp;&nbsp;&nbsp; pdfDoc.Close() &nbsp;&nbsp;&nbsp; file.Close() End Sub Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control) End Sub Protected Function GetUrl(ByVal imagepath As String) As String &nbsp;&nbsp;&nbsp; Dim splits As String() = Request.Url.AbsoluteUri.Split(&#34;/&#34;c) &nbsp;&nbsp;&nbsp; If splits.Length &gt;= 2 Then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim url As String = splits(0) &amp; &#34;//&#34; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; For i As Integer = 2 To splits.Length - 1 - 1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; url += splits(i) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; url += &#34;/&#34; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Next &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Return url &amp; imagepath &nbsp;&nbsp;&nbsp; End If &nbsp;&nbsp;&nbsp; Return imagepath End Function</pre> <p><strong><span style="text-decoration: underline;">Screenshot</span></strong></p> <p><img src="https://i.imgur.com/RVDkwBa.jpg" alt="" width="472" height="321" /></p>https://www.aspforums.net:443/Threads/211796/Export-Page-content-with-Image-to-PDF-using-iTextSharp-in-ASPNet/https://www.aspforums.net:443/Threads/211796/Export-Page-content-with-Image-to-PDF-using-iTextSharp-in-ASPNet/Mon, 19 Aug 2019 06:27:13 GMT