Copy from this site http://csharpdotnetfreak.blogspot.com/2009/01/itextsharp-export-paging-gridview-pdf.html
itextsharp.dll in Bin folder of Application
using iTextSharp.text; using iTextSharp.text.pdf; using iTextSharp.text.html;
itextsharp.dll in Bin folder of Application
using iTextSharp.text; using iTextSharp.text.pdf; using iTextSharp.text.html;
protected void btnExport_Click(object sender, EventArgs e) { int columnCount = GridView1.Columns.Count; int rowCount = GridView1.Rows.Count; int tableRows = rowCount + 3; iTextSharp.text.Table grdTable= new iTextSharp.text.Table(columnCount, tableRows); grdTable.BorderWidth = 1; grdTable.BorderColor = new Color(0, 0, 255); grdTable.Cellpadding = 5; grdTable.Cellspacing = 5; Cell c1 = new Cell("Exporting paging enabled GridView to PDF example"); c1.Header = true;c1.Colspan = 2; grdTable.AddCell(c1); Cell c2 = new Cell("By amiT jaiN , amit_jain_online@yahoo.com"); c2.Colspan = 2; grdTable.AddCell(c2); grdTable.AddCell("Name"); grdTable.AddCell("Location"); for (int rowCounter = 0;rowCounter < rowCount; rowCounter++) {
for (int columnCounter = 0;columnCounter < columnCount; columnCounter++) {
string strValue =GridView1.Rows[rowCounter].Cells[columnCounter].Text; grdTable.AddCell(strValue); } } Document Doc = new Document(); PdfWriter.GetInstance(Doc, Response.OutputStream); Doc.Open();
Doc.Add(grdTable); Doc.Close(); Response.ContentType = "application/pdf"; Response.AddHeader ("content-disposition", "attachment; filename=AmitJain.pdf"); Response.End();
}
2nd Example
using iTextSharp.text; using iTextSharp.text.pdf; using iTextSharp.text.html; using System.IO; using System.Collections; using System.Net;Now in Click event of button i m creating a new HtmlForm and adding the gridview control to this form in code behind , than creating instance of StringWriter class and HtmlTextWriter to write strings and than rendernig these to form created earlier
protected void btnExport_Click(object sender, EventArgs e)
{
HtmlForm form = new HtmlForm();
form.Controls.Add(GridView1);
StringWriter sw = new StringWriter();
HtmlTextWriter hTextWriter = new HtmlTextWriter(sw);
form.Controls[0].RenderControl(hTextWriter);
string html = sw.ToString();
Document Doc = new Document();
//PdfWriter.GetInstance
//(Doc, new FileStream(Request.PhysicalApplicationPath
//+ "\\AmitJain.pdf", FileMode.Create));
PdfWriter.GetInstance
(Doc, new FileStream(Environment.GetFolderPath
(Environment.SpecialFolder.Desktop)
+ "\\AmitJain.pdf", FileMode.Create));
Doc.Open();
Chunk c = new Chunk
("Export GridView to PDF Using iTextSharp \n",
FontFactory.GetFont("Verdana", 15));
Paragraph p = new Paragraph();
p.Alignment = Element.ALIGN_CENTER;
p.Add(c);
Chunk chunk1 = new Chunk
("By Amit Jain, amit_jain_online@yahoo.com \n",
FontFactory.GetFont("Verdana", 8));
Paragraph p1 = new Paragraph();
p1.Alignment = Element.ALIGN_RIGHT;
p1.Add(chunk1);
Doc.Add(p);
Doc.Add(p1);
System.Xml.XmlTextReader xmlReader =
new System.Xml.XmlTextReader(new StringReader(html));
HtmlParser.Parse(Doc, xmlReader);
Doc.Close();
string Path = Environment.GetFolderPath
(Environment.SpecialFolder.Desktop)
+ "\\AmitJain.pdf";
ShowPdf(Path);
}
private void ShowPdf(string strS)
{
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.AddHeader
("Content-Disposition","attachment; filename=" + strS);
Response.TransmitFile(strS);
Response.End();
//Response.WriteFile(strS);
Response.Flush();
Response.Clear();
}
3rd Example
//Format of table
private void ExportToPDF()
{
Document document = new Document(PageSize.A4, 0, 0, 50, 50);
System.IO.MemoryStream msReport = new System.IO.MemoryStream();
try {
// creation of the different writers
PdfWriter writer = PdfWriter.GetInstance(document, msReport);
// we add some meta information to the document
document.AddAuthor("eJuly");
document.AddSubject("Export to PDF");
document.Open();
iTextSharp.text.Table datatable = new iTextSharp.text.Table(7);
datatable.Padding = 2;
datatable.Spacing = 0;
float[] headerwidths = { 6, 20, 32, 18, 8, 8, 8 };
datatable.Widths = headerwidths;
// the first cell spans 7 columns
Cell cell = new Cell(new Phrase("System Users Report", FontFactory.GetFont(FontFactory.HELVETICA, 16, Font.BOLD)));
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.Leading = 30;
cell.Colspan = 7;
cell.Border = Rectangle.NO_BORDER;
cell.BackgroundColor = new iTextSharp.text.Color(System.Drawing.Color.Gray);
datatable.AddCell(cell);
// These cells span 2 rows
datatable.DefaultCellBorderWidth = 1;
datatable.DefaultHorizontalAlignment = 1;
datatable.DefaultRowspan = 2;
datatable.AddCell("No.");
datatable.AddCell(new Phrase("Full Name", FontFactory.GetFont(FontFactory.HELVETICA, 14, Font.NORMAL)));
datatable.AddCell("Address");
datatable.AddCell("Telephone No.");
// This cell spans the remaining 3 columns in 1 row
datatable.DefaultRowspan = 1;
datatable.DefaultColspan = 3;
datatable.AddCell("Just Put Anything");
// These cells span 1 row and 1 column
datatable.DefaultColspan = 1;
datatable.AddCell("Col 1");
datatable.AddCell("Col 2");
datatable.AddCell("Col 3");
datatable.DefaultCellBorderWidth = 1;
datatable.DefaultRowspan = 1;
for (int i = 1; i < 20; i++) {
datatable.DefaultHorizontalAlignment = Element.ALIGN_LEFT;
datatable.AddCell(i.ToString());
datatable.AddCell("This is my name.");
datatable.AddCell("I
have a very, very, very, very, very, very, very, very, very, very,
very, very, very, very, very, very, very, very, very, very, very, very,
very, very, very, very, very long long address.");
datatable.AddCell("0123456789");
datatable.DefaultHorizontalAlignment = Element.ALIGN_CENTER;
datatable.AddCell("No");
datatable.AddCell("Yes");
datatable.AddCell("No");
}
document.Add(datatable);
}
catch (Exception e) {
Console.Error.WriteLine(e.Message);
}
// we close the document
document.Close();
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=Export.pdf");
Response.ContentType = "application/pdf";
Response.BinaryWrite(msReport.ToArray());
Response.End();
}