I am using iTextSharp you can download it from internet. it is free.
you need to create a simple asp.net page and get data from database.Then pass it to ExportToPDF method.We can set PDF page margins, change page orientation (portrait, landscape), customize headers and footers add page numbers and more.Do some R & D with iTextSharp.
public void ExportToPdf(DataTable myDataTable)
{
Document pdfDoc = new Document(PageSize.A4, 10, 10, 10, 10);
try
{
PdfWriter.GetInstance(pdfDoc, System.Web.HttpContext.Current.Response.OutputStream);
pdfDoc.Open();
Chunk c = new Chunk("" + System.Web.HttpContext.Current.Session["CompanyName"] + "", FontFactory.GetFont("Verdana", 11));
Paragraph p = new Paragraph();
p.Alignment = Element.ALIGN_CENTER;
p.Add(c);
pdfDoc.Add(p);
string clientLogo = System.Web.HttpContext.Current.Session["CompanyName"].ToString();
clientLogo = clientLogo.Replace(" ", "");
string clogo = clientLogo + ".jpg";
string imageFilePath = System.Web.HttpContext.Current.Server.MapPath("../ClientLogo/" + clogo + "");
iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageFilePath);
//Resize image depend upon your need
jpg.ScaleToFit(80f, 60f);
//Give space before image
jpg.SpacingBefore = 0f;
//Give some space after the image
jpg.SpacingAfter = 1f;
jpg.Alignment = Element.HEADER;
pdfDoc.Add(jpg);
Font font8 = FontFactory.GetFont("ARIAL", 7);
DataTable dt = myDataTable;
if (dt != null)
{
//Craete instance of the pdf table and set the number of column in that table
PdfPTable PdfTable = new PdfPTable(dt.Columns.Count);
PdfPCell PdfPCell = null;
for (int rows = 0; rows < dt.Rows.Count; rows++)
{
for (int column = 0; column < dt.Columns.Count; column++)
{
PdfPCell = new PdfPCell(new Phrase(new Chunk(dt.Rows[rows][column].ToString(), font8)));
PdfTable.AddCell(PdfPCell);
}
}
//PdfTable.SpacingBefore = 15f; // Give some space after the text or it may overlap the table
pdfDoc.Add(PdfTable); // add pdf table to the document
}
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment; filename= SampleExport.pdf");
System.Web.HttpContext.Current.Response.Write(pdfDoc);
Response.Flush();
Response.End();
//HttpContext.Current.ApplicationInstance.CompleteRequest();
}
catch (DocumentException de)
{
System.Web.HttpContext.Current.Response.Write(de.Message);
}
catch (IOException ioEx)
{
System.Web.HttpContext.Current.Response.Write(ioEx.Message);
}
catch (Exception ex)
{
System.Web.HttpContext.Current.Response.Write(ex.Message);
}
}
Thanks & Regards
Santosh Singh
Hi azeem i found the article very useful for me can u help me how to add header and footer with page numbers in ur code.
ReplyDeletethanks in advance.
Thanks sir, it is really very useful article.
ReplyDeleteThanks so much, its very useful.
ReplyDeletegood article, good job...
ReplyDeletehttp://dotnet-qa.blogspot.in/
This comment has been removed by the author.
ReplyDeleteAmazing. Thank you so much!
ReplyDeleteIf anyone else has tried other examples and keep getting the error about "Input string was not in a correct format." when calling "htmlparser.Parse(sr);". Then use this method instead. Works Brilliantly!
Thank you so much!
ReplyDeletewhere i should put this code? under button_click event or page_load event.
ReplyDeletei tried with button but it says [ Object reference not set to an instance of an object ]
Hi,
ReplyDeleteI tried the sample code in my project. It didn't gave me error on the code but suddenly popup up exception in jquery. No details of that exception either. The pdf also didn't showed up.
Can canbody help?
Generating a PDF from a DataTable in ASP.NET requires libraries like iTextSharp or PdfSharp. What Encryption Military Transforming tabular data into a structured PDF document offers dynamic reporting capabilities.
ReplyDeleteAntalya
ReplyDeleteKonya
Adana
Ankara
Van
5DU
bingöl
ReplyDeleteelazığ
hakkari
sakarya
erzincan
8İU
1F534
ReplyDeleteBayburt Lojistik
Bitlis Evden Eve Nakliyat
Van Lojistik
Bingöl Lojistik
Çanakkale Evden Eve Nakliyat
B423B
ReplyDeleteErzurum Parça Eşya Taşıma
Balıkesir Şehirler Arası Nakliyat
Altındağ Boya Ustası
Adana Lojistik
Balıkesir Lojistik
Çerkezköy Yol Yardım
Denizli Şehirler Arası Nakliyat
Çerkezköy Sineklik
Ankara Şehir İçi Nakliyat
46CDC
ReplyDeleteKırıkkale Tamamen Ücretsiz Sohbet Siteleri
en iyi görüntülü sohbet uygulaması
Erzurum En İyi Görüntülü Sohbet Uygulaması
Düzce Canlı Sohbet Siteleri Ücretsiz
Kırklareli Kadınlarla Rastgele Sohbet
Mersin Telefonda Kızlarla Sohbet
artvin sesli sohbet uygulamaları
telefonda canlı sohbet
canli goruntulu sohbet siteleri
C7555
ReplyDeleteurfa sesli sohbet odası
artvin yabancı canlı sohbet
karaman yabancı görüntülü sohbet uygulamaları
Ankara En İyi Ücretsiz Görüntülü Sohbet Siteleri
Izmir Canlı Sohbet Ücretsiz
random görüntülü sohbet
Tekirdağ Telefonda Rastgele Sohbet
ordu ücretsiz sohbet uygulaması
Denizli Rastgele Sohbet Odaları
49AAB
ReplyDeletekocaeli rastgele sohbet
erzurum görüntülü sohbet siteleri
Artvin Sohbet Siteleri
adana mobil sohbet odaları
kırıkkale canlı sohbet
istanbul görüntülü sohbet ücretsiz
bitlis rastgele görüntülü sohbet uygulaması
siirt yabancı sohbet
muğla yabancı görüntülü sohbet siteleri
24D2B
ReplyDeleteantalya mobil sesli sohbet
kadınlarla rastgele sohbet
mardin en iyi görüntülü sohbet uygulaması
en iyi ücretsiz sohbet siteleri
canlı görüntülü sohbet siteleri
Hakkari Sohbet Uygulamaları
kayseri ücretsiz sohbet sitesi
Tunceli Canlı Sohbet Et
Karaman Sesli Görüntülü Sohbet