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.iTextSharp Export ASP.Net WebPage with Image to PDF Error - Could not find a part of the path<p>Hi <a class="username" rel="Richa"> Richa</a>,</p> <p>If you want the page to work with images you need to convert the Relative Urls to Absolute ones.</p> <p>Example : ~/Images/1.jpg must be changed to http://localhost/Images/1.jpg</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;div&gt; &lt;img runat=&#34;server&#34; id=&#34;img&#34; /&gt;&lt;br /&gt; &lt;/div&gt; &lt;div style=&#34;font-family: Arial&#34;&gt; This is a test page&lt;/div&gt; &lt;div&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;/div&gt; &lt;div&gt; &lt;asp:Button ID=&#34;btnExport&#34; runat=&#34;server&#34; Text=&#34;Export&#34; OnClick=&#34;btnExport_Click&#34; /&gt;&lt;/div&gt;</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 (!IsPostBack) { img.Src = GetUrl(&#34;Images/1.jpg&#34;); img.Width = 50; img.Height = 50; } } protected void btnExport_Click(object sender, EventArgs e) { Response.ContentType = &#34;application/pdf&#34;; Response.AddHeader(&#34;content-disposition&#34;, &#34;attachment;filename=TestPage.pdf&#34;); Response.Cache.SetCacheability(HttpCacheability.NoCache); StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); this.Page.RenderControl(hw); StringReader sr = new StringReader(sw.ToString()); Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f); HTMLWorker htmlparser = new HTMLWorker(pdfDoc); PdfWriter.GetInstance(pdfDoc, Response.OutputStream); pdfDoc.Open(); htmlparser.Parse(sr); pdfDoc.Close(); Response.Write(pdfDoc); Response.End(); } 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; }</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 IsPostBack Then img.Src = GetUrl(&#34;Images/1.jpg&#34;) img.Width = 50 img.Height = 50 End If End Sub Protected Sub btnExport_Click(sender As Object, e As EventArgs) Response.ContentType = &#34;application/pdf&#34; Response.AddHeader(&#34;content-disposition&#34;, &#34;attachment;filename=TestPage.pdf&#34;) Response.Cache.SetCacheability(HttpCacheability.NoCache) Dim sw As New StringWriter() Dim hw As New HtmlTextWriter(sw) Me.Page.RenderControl(hw) Dim sr As New StringReader(sw.ToString()) Dim pdfDoc As New Document(PageSize.A4, 10.0F, 10.0F, 100.0F, 0.0F) Dim htmlparser As New HTMLWorker(pdfDoc) PdfWriter.GetInstance(pdfDoc, Response.OutputStream) pdfDoc.Open() htmlparser.Parse(sr) pdfDoc.Close() Response.Write(pdfDoc) Response.[End]() 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</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 iTextSharp.text; using iTextSharp.text.html.simpleparser; using iTextSharp.text.pdf; using System.Text;</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</pre> <p><strong><span style="text-decoration: underline;">Screenshot</span></strong></p> <p><img src="https://i.imgur.com/DHzqcxp.jpg" alt="" width="413" height="326" /></p>https://www.aspforums.net:443/Threads/116337/iTextSharp-Export-ASPNet-WebPage-with-Image-to-PDF-Error---Could-not-find-a-part-of-the-path/https://www.aspforums.net:443/Threads/116337/iTextSharp-Export-ASPNet-WebPage-with-Image-to-PDF-Error---Could-not-find-a-part-of-the-path/Tue, 27 Feb 2018 03:46:08 GMT