Search EggHeadCafe's Job Board
EggHeadCafe Silverlight WPF ASP.NET VB.NET C# Excel SQL Server SharePoint
search
MicrosoftArticlesForumsFAQs
C# .NET
VB.NET
Visual Studio .NET
ADO.NET
Xml / Xslt
VB 6.0
.NET CF
GDI+
LINQ
Deployment
Security
FoxPro
Silverlight / WPF
Entity Framework
RIA Services

WebArticlesForumsFAQs
JavaScript
ASP
ASP.NET
WCF

DatabasesArticlesForumsFAQs
SQL Server
Access
Oracle
MySQL
Other Databases

OfficeArticlesForumsFAQs
Excel
Word
Powerpoint
Outlook
Publisher
Money

Non-MicrosoftArticlesForumsFAQs
NHibernate
Perl
PHP
Ruby
Java
Linux / Unix
Apple
Open Source

Operating SysArticlesForumsFAQs
Windows 7
Windows Server
Windows Vista
Windows XP
Windows Update
MAC
Linux / UNIX

Server PlatformsArticlesForumsFAQs
BizTalk
Site Server
Exhange Server
IIS

Graphic DesignArticlesForumsFAQs
Macromedia Flash
Adobe PhotoShop
Expression Blend
Expression Design
Expression Web

OtherArticlesForumsFAQs
Lounge
Subversion / CVS
Ask Dr. Dotnetsky
Active Directory
Networking
Uninstall Virus
Job Openings
Product Reviews
Search Engines
Resumes

 

  View Other C# .NET Posts   Ask New Question  Ask New Question With Power Editor

How to export Data Table to PDF file using c# windows application
masuri suhasini posted at Thursday, April 16, 2009 8:02 AM

Hi

can anyone help about how to export or write data table to PDF file.

 

Reply    Reply Using Power Editor
  Rank Winnings Points
November 0 $0.00 0
October 0 $0.00 0
You can use iTextSharp
[)ia6l0 iii provided a rated reply to masuri suhasini on Thursday, April 16, 2009 8:19 AM

Check out the following link for more details.
http://itextsharp.sourceforge.net/

CodePlex has a project on this too.
Reply    Reply Using Power Editor
  Rank Winnings Points
November 1 $217.00 533
October 1 $226.00 771

Re::How to export Data Table to PDF file using c# windows application
egg egg provided a rated reply to masuri suhasini on Thursday, April 16, 2009 8:22 AM

You could actually do that using iTextSharp dll which is explained in the following ones...

http://www.codeproject.com/KB/graphics/iTextSharpTutorial.aspx

and

http://hspinfo.wordpress.com/2008/01/12/how-to-convert-html-content-to-pdf-file/

Reply    Reply Using Power Editor
  Rank Winnings Points
November 9 $17.00 41
October 21 $0.00 11

re
Web star provided a rated reply to masuri suhasini on Thursday, April 16, 2009 8:22 AM

AFAIK, it is not possible to generate PDF files natively with C# or VB.NET. You can explore these 2 free & open source libraries  to generate PDFs on the fly:
PDFSharp - http://www.pdfsharp.com/PDFsharp/index.php?option=com_frontpage&Itemid=1
iText#  - http://sourceforge.net/projects/itextsharp/

Sample code is also available from the above links
Reply    Reply Using Power Editor
  Rank Winnings Points
November 5 $55.00 134
October 10 $28.00 94

Use PDFSharp
Kalit Sikka replied to masuri suhasini on Thursday, April 16, 2009 8:34 AM

PDFsharp is a C# library that easily creates PDF documents on the fly. The same GDI+ like drawing routines can be used to create PDF documents, draw on the screen, or send output to any printer. PDFsharp can also modify, merge, and split existing PDF files or incorporate pages from existing PDF files into new PDF documents

Features of this library:

  • Creates PDF documents on the fly from any .Net language
  • Easy to understand object model to compose documents
  • One source code for drawing on a PDF page as well as in a window or on the printer
  • Modify, merge, and split existing PDF files
  • Images with transparency (color mask, monochrome mask, alpha mask)
  • Newly designed from scratch and written entirely in C#
  • The graphical classes go well with .Net
  • Includes MigraDoc Lite for high level text layouting (you can use both PDFsharp and MigraDoc Lite in one document)
Reply    Reply Using Power Editor
Visit: http://kalitsikka.blogspot.com/ http://kalitinterviewquestions.blogspot.com/
  Rank Winnings Points
November 0 $0.00 0
October 0 $0.00 0

TRY THIS
cool adch replied to masuri suhasini on Thursday, April 16, 2009 9:26 AM

I would suggest using iTextSharp. Here's a link to the tutorials page http://itextsharp.sourceforge.net/tutorial/index.html

You should be able to follow their examples to do what you need

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();
}
Reply    Reply Using Power Editor
  Rank Winnings Points
November 0 $0.00 0
October 0 $0.00 0

TRY THIS
cool adch replied to masuri suhasini on Thursday, April 16, 2009 9:31 AM

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim connect As String = "Data Source=com20;Initial Catalog=Test;Integrated Security=True"
Dim con As New SqlConnection(connect)
con.Open()
Dim str As String = "SELECT * from login"
Dim cmd As New SqlCommand(str, con)
Dim rd As SqlDataReader = cmd.ExecuteReader
gridview1.DataSource = rd
gridview1.DataBind()

End Sub


Protected Sub btn1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn1.Click
form1.Controls.Clear()
form1.Controls.Add(gridview1)
Dim sw As New System.Text.StringBuilder()
Dim htw As New HtmlTextWriter(New System.IO.StringWriter(sw))
form1.RenderControl(htw)
Dim html As String = "<html><body><form runat=""server"">" + sw.ToString() + "</form></body></html>"
Response.Clear()
Response.ContentType = "application/pdf"
Dim document As New iTextSharp.text.Document(PageSize.A4, 80, 50, 30, 65)
Dim writer As iTextSharp.text.pdf.PdfWriter = PdfWriter.GetInstance(document, Response.OutputStream)
document.Open()
Dim tempFile As String = Path.GetTempFileName()
Using tempwriter As New IO.StreamWriter(tempFile, False)
tempwriter.Write(html)
End Using
HtmlParser.Parse(document, tempFile)
document.Close()
writer.Close()
File.Delete(tempFile)
writer = Nothing
document = Nothing
Response.[End]()
End Sub
Reply    Reply Using Power Editor
  Rank Winnings Points
November 0 $0.00 0
October 0 $0.00 0

TRY THIS
cool adch replied to masuri suhasini on Thursday, April 16, 2009 9:32 AM

using System.IO;
using System.Diagnostics;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;

Document myDocument;
myDocument = new Document(PageSize.A4.Rotate());
PdfWriter.GetInstance(myDocument, new FileStream("c:\\zz.pdf", FileMode.Create));
myDocument.Open();
// myDocument.Add(new Paragraph(tmp));

//myDocument.Close();

You have to add refrence of iTextSharp..
Reply    Reply Using Power Editor
  Rank Winnings Points
November 0 $0.00 0
October 0 $0.00 0


Thanks for helping
masuri suhasini replied to egg egg on Monday, April 20, 2009 3:06 AM

Hi,

Santhosh kappa  this site is very use full for me.

thanks

 http://www.codeproject.com/KB/graphics/iTextSharpTutorial.aspx

 

Reply    Reply Using Power Editor
  Rank Winnings Points
November 0 $0.00 0
October 0 $0.00 0

Welcome..
egg egg replied to masuri suhasini on Monday, April 20, 2009 3:46 AM

BTW, i am santhosh Kapa  :)
Reply    Reply Using Power Editor
  Rank Winnings Points
November 9 $17.00 41
October 21 $0.00 11