Exporting ASP.Net Repeater using iTextSharp Error: The Uri string is too long

Last Reply 2 months ago By pandeyism

Posted 2 months ago

I have a panel that contains an image and a repeater control.

I am getting the following error when this code executed : System.UriFormatException: 'Invalid URI: The Uri string is too long.' on line : 

htmlparser.Parse(sr)
        Response.ContentType = "application/pdf"
        Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf")
        Response.Cache.SetCacheability(HttpCacheability.NoCache)
        Dim sw As New StringWriter()
        Dim hw As New HtmlTextWriter(sw)
        PanelEmployeeMedicalHistory.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()

 

Posted 2 months ago

Hi Tevin,

Refer below sample.

HTML

<asp:Panel ID="PanelEmployeeMedicalHistory" CssClass="panel panel-default absolutePrintElement"
    Enabled="true" runat="server">
    <div class="panel-heading" style="text-align: center;">
        Employee Medical History</div>
    <div class="panel-body">
        <div class="form-row">
            <div class="col">
            </div>
            <div class="col">
                <img runat="server" id="ImgLogo" src="http://www.lumotech.co.za/images/img_who-we-are_logo.png"
                    style="width: 200px;" />
            </div>
            <div class="col">
            </div>
        </div>
        <br />
        <div class="form-row">
            <div class="col-3">
            </div>
            <div class="col">
                <asp:Label ID="lblAddress" runat="server" Font-Bold="true" ForeColor="#0099ff" Text="Fitzpatrick St, Cape Road Industrial | Uitenhage | P.O.Box 277 | 6229 | 041 9953016"> </asp:Label>
            </div>
            <div class="col-3">
            </div>
        </div>
        <hr />
        <asp:Repeater ID="RepeaterEmployee" runat="server">
            <ItemTemplate>
                <table id="Table1" runat="server" class="table table-default table-striped table-bordered table-condensed">
                    <tr>
                        <td colspan="2" style="text-align: center;">
                            <asp:Image ID="Image1" runat="server" ImageAlign="Middle" ImageUrl='<%# GetUrl(Eval("Photo").ToString()) %>' />
                        </td>
                    </tr>
                    <tr>
                        <td style="vertical-align: middle;">
                        </td>
                        <td>
                            <table id="Table2" runat="server" style="width: 100%;">
                                <tr>
                                    <td>
                                        <b>First Name:</b>
                                    </td>
                                    <td>
                                        <asp:Label ID="FirstName" runat="server" Text='<%#Eval("FirstName") %>'></asp:Label>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <b>Last Name:</b>
                                    </td>
                                    <td>
                                        <asp:Label ID="lblLastName" runat="server" Text='<%#Eval("LastName") %>'></asp:Label>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <b>Employee Code:</b>
                                    </td>
                                    <td>
                                        <asp:Label ID="lblCode" runat="server" Text='<%#Eval("Code") %>'></asp:Label>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <b>ID:</b>
                                    </td>
                                    <td>
                                        <asp:Label ID="lblEmployeeID" runat="server" Text='<%#Eval("EmployeeID") %>'></asp:Label>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <b>Cost Centre:</b>
                                    </td>
                                    <td>
                                        <asp:Label ID="lblCostCentre" runat="server" Text='<%#Eval("CostCentre") %>'></asp:Label>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <b>Medical Report:</b>
                                    </td>
                                    <td>
                                        <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click" CommandName="download">Download</asp:LinkButton>
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>
            </ItemTemplate>
        </asp:Repeater>
    </div>
</asp:Panel>

Namespaces

C#

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

VB.Net

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

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        BindRepeater();
    }
}

private void BindRepeater()
{
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[] { new DataColumn("Photo", typeof(string)), new DataColumn("FirstName", typeof(string)), new DataColumn("LastName", typeof(string)), new DataColumn("Code", typeof(string)), new DataColumn("EmployeeID", typeof(string)), new DataColumn("CostCentre", typeof(string)) });
    dt.Rows.Add("ASPSnippetsLogo.png", "Mudassar", "Khan", "c1", "1", "Mumbai");
    RepeaterEmployee.DataSource = dt;
    RepeaterEmployee.DataBind();
}

protected void LinkButton1_Click(object sender, EventArgs e)
{
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    PanelEmployeeMedicalHistory.RenderControl(hw);
    StringReader sr = new StringReader(sw.ToString());
    Document pdfDoc = new Document(PageSize.A4, 10.0F, 10.0F, 100.0F, 0.0F);
    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
    Response.Write(pdfDoc);
    pdfDoc.Open();
    htmlparser.Parse(sr);
    pdfDoc.Close();
    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;
}

public override void VerifyRenderingInServerForm(Control control)
{
    /* Verifies that the control is rendered */
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        BindRepeater()
    End If
End Sub

Private Sub BindRepeater()
    Dim dt As DataTable = New DataTable()
    dt.Columns.AddRange(New DataColumn() {New DataColumn("Photo", GetType(String)), New DataColumn("FirstName", GetType(String)), New DataColumn("LastName", GetType(String)), New DataColumn("Code", GetType(String)), New DataColumn("EmployeeID", GetType(String)), New DataColumn("CostCentre", GetType(String))})
    dt.Rows.Add("ASPSnippetsLogo.png", "Mudassar", "Khan", "c1", "1", "Mumbai")
    RepeaterEmployee.DataSource = dt
    RepeaterEmployee.DataBind()
End Sub

Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As EventArgs)
    Response.ContentType = "application/pdf"
    Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf")
    Response.Cache.SetCacheability(HttpCacheability.NoCache)
    Dim sw As StringWriter = New StringWriter()
    Dim hw As HtmlTextWriter = New HtmlTextWriter(sw)
    PanelEmployeeMedicalHistory.RenderControl(hw)
    Dim sr As StringReader = New StringReader(sw.ToString())
    Dim pdfDoc As Document = New Document(PageSize.A4, 10.0F, 10.0F, 100.0F, 0.0F)
    Dim htmlparser As HTMLWorker = New HTMLWorker(pdfDoc)
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
    Response.Write(pdfDoc)
    pdfDoc.Open()
    htmlparser.Parse(sr)
    pdfDoc.Close()
    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

Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
End Sub

Screenshot