Export Page content with Image to PDF using iTextSharp in ASP.Net

Last Reply one month ago By dharmendr

Posted one month ago

Thousands of seniors are missing out on new 2019 discounts for not knowing about them...

Just about every senior who likes to save money knows about the standard discounts available at most restaurants and retailers, but what about the ones that are not so well known? The truth is, the everyday senior discounts pale in comparison to some of the special offers available online. But you have to know where to look and you won’t get them unless YOU ASK!  

We have compiled the most recent and best money-saving discounts seniors can take advantage of in 2019 to save up to $6,100! That's right being a senior citizen has its perks. Many seniors are surprised to find they can save thousands by just taking advantage of these new offers.

1. Get A Discount On A New Walk In Tub

Walk in tubs are a perfect addition for seniors living with mobility challenges or anyone worried about fall related injuries. Statistics tell us that falls happen in your bathroom more than any other room in the home.

In fact two-thirds of all bathroom injuries happen near the shower or bath-tub, according to The Center for Disease Control. Most falls typically happen when your stepping over the tub, slipping on a wet floor or when leaning on a glass door that wasn’t designed to support your weight.

A Walk-in tub is an investment in your future a simple modification to your bathroom can change your life. Keeping your independence and dignity, plus peace of mind for your loved ones all ad up to a better quality of life. Right now, Walk-in tub manufacturers all over the country are offering big senior discounts. Visit the walk-in tub website to find discounts and get a free quote on a new Walk-in tub.

How to convert the text as the above from a web page to a pdf file.  Save it in a folder in project solution, use C# and ASP.Net

How can I add an image in this webpage then it will convert to pdf too?

You are viewing reply posted by: dharmendr one month ago.
Posted one month ago Modified on one month ago

Hi KatieNgoc,

Place your content inside a Panel. Then export the Panel Content to pdf and save in folder.

For more details refer below article.

Export HTML DIV contents to PDF using iTextSharp in ASP.Net

HTML

<asp:Panel ID="pnlDetails" runat="server">
    <asp:Image ID="imgPhoto" ImageUrl="~/Penguins.jpg" runat="server" /><br />
    <p>1. Get A Discount On A New Walk In Tub</p><br />
    <p>
        Walk in tubs are a perfect addition for seniors living with mobility challenges
        or anyone worried about fall related injuries. Statistics tell us that falls happen
        in your bathroom more than any other room in the home.</p><br />
    <p>
        In fact two-thirds of all bathroom injuries happen near the shower or bath-tub,
        according to The Center for Disease Control. Most falls typically happen when your
        stepping over the tub, slipping on a wet floor or when leaning on a glass door that
        wasn't designed to support your weight</p><br />
    <p>
        A Walk-in tub is an investment in your future a simple modification to your bathroom
        can change your life. Keeping your independence and dignity, plus peace of mind
        for your loved ones all ad up to a better quality of life. Right now, Walk-in tub
        manufacturers all over the country are offering big senior discounts. Visit the
        walk-in tub website to find discounts and get a free quote on a new Walk-in tub.</p>
</asp:Panel>
<asp:Button ID="btnExport" runat="server" Text="Export" OnClick="btnExport_Click" />

Namespaces

C#

using System.IO;
using System.Web.UI;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;

VB.Net

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

Code

C#

protected void btnExport_Click(object sender, EventArgs e)
{
    imgPhoto.ImageUrl = GetUrl("Penguins.jpg");
    if (!Directory.Exists(Server.MapPath("~/Files/")))
    {
        Directory.CreateDirectory(Server.MapPath("~/Files/"));
    }
    FileStream file = new FileStream(Server.MapPath("~/Files/") + "Test.PDF", FileMode.Create, System.IO.FileAccess.Write);
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    pnlDetails.RenderControl(hw);
    StringReader sr = new StringReader(sw.ToString());
    Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
    MemoryStream ms = new MemoryStream();
    PdfWriter.GetInstance(pdfDoc, file);
    pdfDoc.Open();
    htmlparser.Parse(sr);
    pdfDoc.Close();
    file.Close();
}

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

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 btnExport_Click(ByVal sender As Object, ByVal e As EventArgs)
    imgPhoto.ImageUrl = GetUrl("Penguins.jpg")
    If Not Directory.Exists(Server.MapPath("~/Files/")) Then
        Directory.CreateDirectory(Server.MapPath("~/Files/"))
    End If
    Dim file As FileStream = New FileStream(Server.MapPath("~/Files/") & "Test.PDF", FileMode.Create, System.IO.FileAccess.Write)
    Dim sw As StringWriter = New StringWriter()
    Dim hw As HtmlTextWriter = New HtmlTextWriter(sw)
    pnlDetails.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 htmlparser As HTMLWorker = New HTMLWorker(pdfDoc)
    Dim ms As MemoryStream = New MemoryStream()
    PdfWriter.GetInstance(pdfDoc, file)
    pdfDoc.Open()
    htmlparser.Parse(sr)
    pdfDoc.Close()
    file.Close()
End Sub

Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
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

Screenshot