In our projects requirement may araise to generate pdf reports.
PDF reports can be easily generated with out use of any crystal reports using a 3rd party library named
"ITEXTSHARP"
It can be downloaded in this website ITEXTSHARP
Now let us see a example how can we create a simple pdf document on button click (code is written in c#)
- First step is to download the attachment unzip it and you can find file itextsharp.dll in it.
- Now go to visualstudio and create a new website and drag and drop a button on to the page.
- Now add the itextsharp.dll reference to the project by clicking on reference.
- Now add the namespaces to the .cs file (see the code for the namespaces).
- Now double click on button to get the event handler.
Code is as follows
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 iTextSharp.text.html;
public partial class registering_page : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void img_btn_pdf_Click(object sender, ImageClickEventArgs e)
{
try
{
// Creates a PDF document
bool LandScape = true;
Document document = null;
if (LandScape == true)
{
// Sets the document to A4 size and rotates it so that the orientation of the page is Landscape.
document = new Document(PageSize.A4.Rotate(), 0, 0, 15, 5);
}
else
{
document = new Document(PageSize.A4, 0, 0, 15, 5);
}
// Gets the instance of the document created and writes it to the output stream of the Response object.
PdfWriter.GetInstance(document, Response.OutputStream);
// Creates a footer for the PDF document.
HeaderFooter pdfFooter = new HeaderFooter(new Phrase(), true);
pdfFooter.Alignment = Element.ALIGN_CENTER;
pdfFooter.Border = iTextSharp.text.Rectangle.NO_BORDER;
// Sets the document footer to pdfFooter.
document.Footer = pdfFooter;
// Opens the document.
document.Open();
// Adds the mainTable to the document.
document.Add(getContent());
document.NewPage();
document.Close();
string filename = "introduction" + ".pdf";
string attachment = "attachment; filename=" + filename;
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", attachment);
Response.End();
}
Catch
{
//handle your catch block
}
}
/// <summary>
/// Gets the content in to the PDF document.
/// </summary>
/// <returns></returns>
private PdfPTable getContent());
{
float HeaderTextSize = 10;
float ReportNameSize = 10;
float ReportTextSize = 8;
float ApplicationNameSize = 8;
// Creates a PdfPTable with column count of the table equal to no of columns of the gridview or gridview datasource.
iTextSharp.text.pdf.PdfPTable mainTable = new iTextSharp.text.pdf.PdfPTable(2);
// Sets the first 4 rows of the table as the header rows which will be repeated in all the pages.
mainTable.HeaderRows = 6;
// Creates a PdfPTable with 2 columns to hold the header in the exported PDF.
iTextSharp.text.pdf.PdfPTable headerTable = new iTextSharp.text.pdf.PdfPTable(3);
// Creates a phrase to hold the application name at the left hand side of the header.
Phrase phApplicationName = new Phrase("Empid", FontFactory.GetFont("Arial", ApplicationNameSize, iTextSharp.text.Font.NORMAL));
// Creates a PdfPCell which accepts a phrase as a parameter.
PdfPCell clApplicationName = new PdfPCell(phApplicationName);
// Sets the border of the cell to zero.
clApplicationName.Border = PdfPCell.NO_BORDER;
// Sets the Horizontal Alignment of the PdfPCell to left.
clApplicationName.HorizontalAlignment = Element.ALIGN_LEFT;
// Creates a phrase to show the current date at the right hand side of the header.
Phrase phDate = new Phrase("Date", FontFactory.GetFont("Arial", ApplicationNameSize, iTextSharp.text.Font.NORMAL));
// Creates a PdfPCell which accepts the date phrase as a parameter.
PdfPCell clDate = new PdfPCell(phDate);
// Sets the Horizontal Alignment of the PdfPCell to right.
clDate.HorizontalAlignment = Element.ALIGN_RIGHT;
// Sets the border of the cell to zero.
clDate.Border = PdfPCell.NO_BORDER;
Phrase pPname = new Phrase("Name", FontFactory.GetFont("Arial", ApplicationNameSize, iTextSharp.text.Font.NORMAL));
// Creates a PdfPCell which accepts the date phrase as a parameter.
PdfPCell cpPname = new PdfPCell(pPname);
// Sets the Horizontal Alignment of the PdfPCell to right.
cpPname.HorizontalAlignment = Element.ALIGN_CENTER;
// Sets the border of the cell to zero.
cpPname.Border = PdfPCell.NO_BORDER;
// Adds the cell which holds the application name to the headerTable.
headerTable.AddCell(clApplicationName);
headerTable.AddCell(cpPname);
// Adds the cell which holds the date to the headerTable.
headerTable.AddCell(clDate);
// Sets the border of the headerTable to zero.
headerTable.DefaultCell.Border = PdfPCell.NO_BORDER;
// Creates a PdfPCell that accepts the headerTable as a parameter and then adds that cell to the main PdfPTable.
PdfPCell cellHeader = new PdfPCell(headerTable);
cellHeader.Border = PdfPCell.NO_BORDER;
// Sets the column span of the header cell to noOfColumns.
cellHeader.Colspan = 3;
// Adds the above header cell to the table.
mainTable.AddCell(cellHeader);
// Creates a phrase which holds the file name.
Phrase phHeader = new Phrase("Leave Request", FontFactory.GetFont("Arial", ReportNameSize, iTextSharp.text.Font.BOLD));
PdfPCell clHeader = new PdfPCell(phHeader);
clHeader.Colspan = 3;
clHeader.Border = PdfPCell.NO_BORDER;
clHeader.HorizontalAlignment = Element.ALIGN_CENTER;
mainTable.AddCell(clHeader);
// Creates a phrase for a new line.
Phrase phSpace = new Phrase("\n");
PdfPCell clSpace = new PdfPCell(phSpace);
clSpace.Border = PdfPCell.NO_BORDER;
clSpace.Colspan = 3;
mainTable.AddCell(clSpace);
// Sets the gridview column names as table headers.
mainTable.CompleteRow();
// Reads the gridview rows and adds them to the mainTable
Phrase mph = new Phrase("Leave Type", FontFactory.GetFont("Arial", ReportTextSize, iTextSharp.text.Font.NORMAL));
mainTable.AddCell(mph);
mainTable.CompleteRow();
Phrase mph = new Phrase("Leave Type", FontFactory.GetFont("Arial", ReportTextSize, iTextSharp.text.Font.NORMAL));
mainTable.AddCell(mph);
mainTable.CompleteRow();
Phrase mph = new Phrase("Start date", FontFactory.GetFont("Arial", ReportTextSize, iTextSharp.text.Font.NORMAL));
mainTable.AddCell(mph);
mainTable.CompleteRow();
Phrase mph = new Phrase("End date", FontFactory.GetFont("Arial", ReportTextSize, iTextSharp.text.Font.NORMAL));
mainTable.AddCell(mph);
mainTable.CompleteRow();
Phrase mph = new Phrase("Reason", FontFactory.GetFont("Arial", ReportTextSize, iTextSharp.text.Font.NORMAL));
mainTable.AddCell(mph);
mainTable.CompleteRow();
return mainTable;
}
}
This is how we can generate pdf documents.
Please free to share your comments and feedback
No comments:
Post a Comment