iTextSharp Export ASP.Net WebPage with Image to PDF Error - Could not find a part of the path

Last Reply 10 months ago By dharmendr

Posted 10 months ago

Hi,

I am using below link to export entire web page.

https://www.aspsnippets.com/Articles/Export-ASPNet-Web-Page-with-images-to-PDF-using-ITextsharp.aspx

This web page is using MasterPage as well. Master Page and Web Page both has many html image tag + asp.net controls + 3rd party controls.

When I use this code in my application, I get below error:

System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Content\img\mega-menu-icon-1.svg'.

at line: htmlparser.Parse(sr);

Even though the file is present at the location, also I tried to give Full access to the folders but still facing the same error.

I have images inside 'Folder' located in my application itself. How can I pass URL for that ?

I need to keep images in Folder. Let me know how to achieve it ?

Thanks in advance

Posted 10 months ago

Hi Richa,

If you want the page to work with images you need to convert the Relative Urls to Absolute ones.

Example : ~/Images/1.jpg must be changed to http://localhost/Images/1.jpg

Check this example. Now please take its reference and correct your code.

HTML

<div>
    <img runat="server" id="img" /><br />
</div>
<div style="font-family: Arial">
    This is a test page</div>
<div>
    <table border="1" width="100">
        <tr>
            <td>Name</td>
            <td>Age</td>
        </tr>
        <tr>
            <td>John</td>
            <td>11</td>
        </tr>
        <tr>
            <td>Sam</td>
            <td>13</td>
        </tr>
        <tr>    
            <td>Tony</td>
            <td>12</td>
        </tr>
    </table>
</div>
<div>
    <asp:Button ID="btnExport" runat="server" Text="Export" OnClick="btnExport_Click" /></div>

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        img.Src = GetUrl("Images/1.jpg");
        img.Width = 50;
        img.Height = 50;
    }
}
protected void btnExport_Click(object sender, EventArgs e)
{
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
    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('/');
    if (splits.Length >= 2)
    {
        string url = splits[0] + "//";
        for (int i = 2; i < splits.Length - 1; i++)
        {
            url += splits[i];
            url += "/";
        }
        return url + imagepath;
    }
    return imagepath;
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not IsPostBack Then
        img.Src = GetUrl("Images/1.jpg")
        img.Width = 50
        img.Height = 50
    End If
End Sub
Protected Sub btnExport_Click(sender As Object, e As EventArgs)
    Response.ContentType = "application/pdf"
    Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf")
    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("/"c)
    If splits.Length >= 2 Then
        Dim url As String = splits(0) & "//"
        For i As Integer = 2 To splits.Length - 1 - 1
            url += splits(i)
            url += "/"
        Next

        Return url & imagepath
    End If

    Return imagepath
End Function

Namespaces

C#

using System.IO;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
using System.Text;

VB.Net

Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.html.simpleparser
Imports iTextSharp.text.pdf

Screenshot