Export Web page and Image populated with base64String to PDF using iTextSharp in ASP.Net

Last Reply 2 months ago By dharmendr

Posted 2 months ago

I am able to export the text elements to PDF but Image, glyphicons, buttons not displaying. My major concern for this question is exporting the Image, but if you assist me with the glyphicons and buttons not displaying, I will be most grateful. Error I get is:

The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.

which points at code:

iTextSharp.text.Image picture = iTextSharp.text.Image.GetInstance(imageURL);

This is the code for selecting the image from the database:

string queryimg = "Select `picture` from image where frpid=" + Session["app_id"].ToString();
command = new MySqlCommand(queryimg, con);
byte[] bytes = (byte[])command.ExecuteScalar();

string strBase64 = Convert.ToBase64String(bytes);
if (strBase64 != null)
{
    prvimage.ImageUrl = "data:Image/png;base64," + strBase64;
}
else
{
    Response.Write("Must upload an Image");
}

This is the code for exporting the webform to PDF:

        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(imageURL);
                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();
            }
        }

Please help.

You are viewing reply posted by: dharmendr 2 months ago.
Posted 2 months ago

Hi milepredy,

milepredy says:
iTextSharp.text.Image picture = iTextSharp.text.Image.GetInstance(imageURL);

 You cant pass directly Image url (base64 string) to the GetInstance method.

You need to remove data:Image/png;base64, from the string.

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

HTML

<asp:Image ID="prvimage" runat="server" />
<br />
<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>
<br />
<asp:Button ID="btnExport" runat="server" Text="Export" OnClick="btnExport_Click" />

Namespace

C#

using System.IO;
using System.Web;
using System.Web.UI;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.tool.xml;

VB.Net

Imports System.IO
Imports System.Web
Imports System.Web.UI
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports iTextSharp.tool.xml

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    // 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 */
}

VB.Net

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," & 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

Screenshot