Export div content to PDF Using ITextSharp in asp.net
In this short article I will explain how to export content in a div to a PDF using ITextSharp in ASP.Net.
Step1: ITextSharp is a free and open source assembly that helps to convert page output or HTML content in a PDF file.Download it first from given link and add dll file in bin folder-https://sourceforge.net/projects/itextsharp/files/itextsharp/
Step2-add Page
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Studentdetails.aspx.cs" Inherits="Studentdetails" EnableEventValidation = "false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div id="studentdetails" runat="server">
Export Div Content to PDF Using ITextSharp
<br />
<table border="1">
<tr>
<td colspan="2">
<b>Student Detail</b>
</td>
</tr>
<tr>
<td><b>Student:</b></td>
<td><asp:Label ID="lblEmployeeId" runat="server" Text="Stu123"></asp:Label></td>
</tr>
<tr>
<td><b>First Name:</b></td>
<td><asp:Label ID="lblname" runat="server" Text="Manvendra"></asp:Label></td>
</tr>
<tr>
<td><b>Last Name:</b></td>
<td><asp:Label ID="lbllast" runat="server" Text="Patel"></asp:Label></td>
</tr>
<tr>
<td><b>City:</b></td>
<td><asp:Label ID="lblcity" runat="server" Text="Delhi"></asp:Label></td>
</tr>
<tr>
<td><b>Postal Code:</b></td>
<td><asp:Label ID="lblpcode" runat="server" Text="2828222"></asp:Label></td>
</tr>
<tr>
<td><b>Country:</b></td>
<td><asp:Label ID="lblcontry" runat="server" Text="India"></asp:Label></td>
</tr>
</table>
</div>
<asp:Button ID="btnexportstudent" runat="server" Text="Export" OnClick="btnexportstudent_Click" />
</form>
</body>
</html>
Step3- add code in c# codebehind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
using System.Text;
public partial class Studentdetails : System.Web.UI.Page
{
protected void btnexportstudent_Click(object sender, EventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=studentdetails.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
studentdetails.RenderControl(htmlTextWriter);
StringReader stringReader = new StringReader(stringWriter.ToString());
Document Doc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
HTMLWorker htmlparser = new HTMLWorker(Doc);
PdfWriter.GetInstance(Doc, Response.OutputStream);
Doc.Open();
htmlparser.Parse(stringReader);
Doc.Close();
Response.Write(Doc);
Response.End();
}
}
Browser view of Page
After generated Pdf View .
Thx for the long information, however it's too complex by using itextsharp...why not choose iditect.converter, it's quiet easy, 3 lines code to export html content to pdf using c#
ReplyDeleteI have found a lot of technical forums about Export div content to PDF Using ITextSharp in asp.net, but many of them are convert pdf to text c#. This technical post shared by bloggers is really useful, thank you!
ReplyDeleteThis is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value of providing a quality resource for free. convert pdf to text
ReplyDelete0imcribYviomaPaterson Kevin Islam https://wakelet.com/wake/v5b5rxFdnw7J5tA9Q8R-d
ReplyDeletehuntsymwatchdrum
probalgyra_1986 Nikki Marie link
ReplyDeleteclick
download
click here
clinlatabmarg