In this article I’m going to explain how
to create PDF document in ASP.NET using iTextSharp. I have already written many
articles related to iTextSharp PDF.
How to use Tables in PDF document using iTextSharp
How to change PDF document page size using iTextSharp
How to use Images in PDF document using iTextSharp
Image alignment in PDF document using iTextSharp
How
to export GridView records to PDF document using iTextSharp
Export Chart control
to PDF document in ASP.NET using iTextSharp
What is iTextSharp:
iTextSharp is a C#
port of iText, and open source Java library for PDF generation and
manipulation. It can be used to create PDF documents from scratch, to convert
XML to PDF (using the extra XFA Worker DLL), to fill out interactive PDF
forms, to stamp new content on existing PDF documents, to split and merge
existing PDF documents, and much more.
Here I’m going to explain how to create PDF
document in ASP.NET using iTextSharp. First we have to
download iTextSharp.dll class library and include to our project.
You can download iTextSharp.dll class library here
http://sourceforge.net/projects/itextsharp/
Then open your project and include that iTextSharp.dll class library.

Browse the iTextSharp.dll file which is downloaded now and click ok.

Designer
source code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default"%>
<!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>Demo</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnShow" runat="server" Text="Show PDF"
OnClick="btnShow_OnClick"/>
</div>
</form>
</body>
</html>
Here
you have to use following namespaces
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
Code
Behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
public partial class _Default :
System.Web.UI.Page
{
protected void Page_Load(object
sender, EventArgs e)
{
}
protected void btnShow_OnClick(object
sender, EventArgs e)
{
Document
pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
PdfWriter.GetInstance(pdfDoc,
Response.OutputStream);
pdfDoc.Open();
pdfDoc.Add(new
Paragraph("Welcome
to dotnetfox"));
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;" +
"filename=sample.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();
}
}