HTML web Page TO PDF Conversion
A number of times while searching on internet for relevant information we like content of more than one website so we often want to save that content we mostly do the old technique like saving the webpage using Ctrl + s or copy its content and save it in some other Notepad file or Microsoft Word etc. This technique consumes our a lot of time .
So As web Developers we must give easy content saving way to user. For including the Feature of converting a web Page into PDF you can search for number of libraries available on internet . I have used iTextsharp and it works fine for me without any more complications . It takes very less time to implement as compared to other libraries and implements easily
Introduction to iTextSharp
iTextSharp is a free library that is available to create PDF documents with Microsoft Visual studio using C# or Vb# Language. It is very easy to use and give more flexibility as compared to all other libraries available to generate PDF. I have used a lot of libraries almost 4-5 but i feel very
comfortable using iTextsharp library that more full customization features for PDF Documents. It consist of number of objects for creating Tables or paragraphs that make easy to customize the view of your data on PDF Documents
Download iTextSharp
For using iTestSharp library in your project for converting your HTML or aspx Web Page into PDF you have to download iTextSharp.dll file from Link Below :
How To Use This Application Code
1. First Download iTextSharp.dll file By clicking on link above
2. Now Download C# Application To Convert Html/aspx To PDF
3. Extract the winrar file on your computer . Then open web2pdf solution file .
4. Now first, add the reference to download iTextSharp.dll file by following steps :-
( i ) click on Solution Explorer in Right sidebar
( ii ) Now Right click on Reference Folder --> Then click on Add Reference
( iii ) Now in Add Reference Dialog Box click on Browse Tab
upload itextsharp.dll |
( iv ) Now select the iTextSharp.dll file from location where you download it ( make sure you have extracted the iTextSharp file )
( v ) Now click ok
( vi ) Now you can see in Reference folder your iTextSharp file Reference will be added
5. Now Add Reference to following Namespaces in your webform
using iTextSharp.text.html.simpleparser;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.xml;
Application Demo WebPage Code :
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="web2pdf.WebForm1" %>
<!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 id="Head1" runat="server">
<title>Untitled Page</title>
<style type="text/css">
.style5
{
width: 24px;
height: 342px;
}
.style6
{
height: 89px;
}
.style7
{
height: 342px;
}
</style>
</head>
<body>
<form id="form1" runat="server" >
<div style="width:100%;height:375px;">
<table border="1" style="height: 484px; width: 567px" >
<tr>
<td colspan="2" style="background-color:#FFA500;" class="style6">
<h1>Web Page To PDF Conversion Using iTextSharp</h1></td>
</tr>
<tr>
<td style="background-color:#FFD700;" class="style5">
</td>
<td style="background-color:#eeeeee;" class="style7">
Don't Forget To Subscribe us</td>
</tr>
<tr>
<td colspan="2" style="background-color:#FFA500;">
Powered By geeksprogrammings.blogspot.in</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Design of Demo Webform
HTML Web Page To PDF Using iTextSharp C# |
Application C# Code
( Don't Forget to change
project, namespaces names or class names according to your App)
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 System.Text.RegularExpressions;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.xml;
//using System.Xml;
using iTextSharp.text.html.simpleparser;
namespace web2pdf
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected override void Render(HtmlTextWriter writer)
{
MemoryStream mem = new MemoryStream();
StreamWriter twr = new StreamWriter(mem);
HtmlTextWriter myWriter = new HtmlTextWriter(twr);
base.Render(myWriter);
myWriter.Flush();
myWriter.Dispose();
StreamReader strmRdr = new StreamReader(mem);
strmRdr.BaseStream.Position = 0;
string pageContent = strmRdr.ReadToEnd();
strmRdr.Dispose();
mem.Dispose();
writer.Write(pageContent);
CreatePDFDocument(pageContent);
}
public void CreatePDFDocument(string strHtml)
{
string strFileName = HttpContext.Current.Server.MapPath("test.pdf");
// step 1: creation of a document-object
Document document = new Document();
// step 2:
// we create a writer that listens to the document
PdfWriter.GetInstance(document, new FileStream(strFileName, FileMode.Create));
StringReader se = new StringReader(strHtml);
HTMLWorker obj = new HTMLWorker(document);
document.Open();
obj.Parse(se);
document.Close();
ShowPdf(strFileName);
}
public void ShowPdf(string strFileName)
{
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "inline;filename=" + strFileName);
Response.ContentType = "application/pdf";
Response.WriteFile(strFileName);
Response.Flush();
Response.Clear();
}
}
}
If, you have any problem with code working or any other issue with converting HTML / aspx to pdf you can share in comments
Nice sir.....it is really working
ReplyDeletehow to call the function Render( ?)
ReplyDeleteWhen an ASP.NET page runs, it performs a series of g steps that include initialization, instantiating controls, restoring and maintaining state, running event handler code, and rendering
ReplyDeleteso rendering is part of page life cycle , we can say it a runtime event and it is called automatically
When an ASP.NET page runs, it performs a series of steps that include initialization, instantiating controls, restoring and maintaining state, running event handler code, and rendering
ReplyDeleteso rendering is part of Asp.net Page Running cycle and it is automatically called when Asp.net page is fetched or loaded