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.
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
