This article will show you how you can export html content
to pdf in your asp.net application using iTextSharp.
So first we will create a new asp.net application an then
install the nuget package for iTextSharp. Please check the below link and
screen show.
https://www.nuget.org/packages/iTextSharp/
https://www.nuget.org/packages/iTextSharp/
Now open package installer and install the nuget package.
After installing add a button control in your page and
generate the click event.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>HTML to PDF in Asp.Net</title>
</head>
<body>
<form id="form1" runat="server">
<h3>Export Html to PDF</h3><br />
<div>
<asp:Button ID="Button1" runat="server" Text="Export
HTML to PDF" OnClick="Button1_Click" />
</div>
</form>
</body>
</html>
|
Now in your code behind add the below code to export the
html content from code behind to pdf.
using iTextSharp.text;
using
iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string htmlcontent = HTMLString();
Document
pdfDoc = new Document(PageSize.A4, 10f, 10f,
10f, 0f);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
List
for (int k = 0; k < htmlarraylist.Count; k++)
{
pdfDoc.Add((IElement)htmlarraylist[k]);
}
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=sample.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
}
public string HTMLString()
{
StringBuilder
htmltext = new
StringBuilder();
htmltext.Append("<div><h1>Welcome
To aspdotnetpools</h1></div>");
htmltext.Append("<table width='100%'
border='1'>");
htmltext.Append("<tr>");
htmltext.Append("<td>Data
1</td>");
htmltext.Append("<td>Data 1</td>");
htmltext.Append("<td>Data
1</td>");
htmltext.Append("<td>Data
1</td>");
htmltext.Append("</tr>");
htmltext.Append("<tr>");
htmltext.Append("<td>Data
1</td>");
htmltext.Append("<td>Data
1</td>");
htmltext.Append("<td>Data
1</td>");
htmltext.Append("<td>Data
1</td>");
htmltext.Append("</tr>");
htmltext.Append("<tr>");
htmltext.Append("<td>Data
1</td>");
htmltext.Append("<td>Data
1</td>");
htmltext.Append("<td>Data 1</td>");
htmltext.Append("<td>Data
1</td>");
htmltext.Append("</tr>");
htmltext.Append("<tr>");
htmltext.Append("<td>Data
1</td>");
htmltext.Append("<td>Data 1</td>");
htmltext.Append("<td>Data
1</td>");
htmltext.Append("<td>Data
1</td>");
htmltext.Append("</tr>");
htmltext.Append("<tr>");
htmltext.Append("<td>Data
1</td>");
htmltext.Append("<td>Data
1</td>");
htmltext.Append("<td>Data
1</td>");
htmltext.Append("<td>Data
1</td>");
htmltext.Append("</tr>");
htmltext.Append("<tr>");
htmltext.Append("<td>Data
1</td>");
htmltext.Append("<td>Data
1</td>");
htmltext.Append("<td>Data 1</td>");
htmltext.Append("<td>Data
1</td>");
htmltext.Append("</tr>");
htmltext.Append("<tr>");
htmltext.Append("<td>Data
1</td>");
htmltext.Append("<td>Data 1</td>");
htmltext.Append("<td>Data
1</td>");
htmltext.Append("<td>Data
1</td>");
htmltext.Append("</tr>");
htmltext.Append("</table>");
htmltext.Append("<br/><br/>");
htmltext.Append("Thanks you for
downloading");
htmltext.Append("aspdotnet-pools.com");
return htmltext.ToString();
}
}
}
|
In above code first I have prepared an html string and then
exported it to pdf. Now run the application and check the output.
Here is the output.
DOWNLOAD
0 comments:
Please let me know your view