logo

How to export Data Table to PDF file using c# windows application (C# .NET)
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
February 0 $0.00 0
January 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
February 1 $231.00 250
January 1 $224.00 813

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
Having around 7 years of experience in designing and implementing software products including windows, console, service oriented and ofcourse predominantly on Web with .Net, XML, Javascript, Oracle, SQL Server
  Rank Winnings Points
February 4 $100.00 108
January 4 $100.00 362

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
February 8 $34.00 37
January 7 $36.00 129

Use PDFSharp
K S 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/
  Rank Winnings Points
February 23 $0.00 4
January 11 $21.00 77

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
February 0 $0.00 0
January 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
February 0 $0.00 0
January 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
February 0 $0.00 0
January 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
February 0 $0.00 0
January 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
Having around 7 years of experience in designing and implementing software products including windows, console, service oriented and ofcourse predominantly on Web with .Net, XML, Javascript, Oracle, SQL Server
  Rank Winnings Points
February 4 $100.00 108
January 4 $100.00 362


Didn't Find The Answer You Were Looking For?

EggHeadCafe has experts online right now that may know the answer to your question.  We pay them a bonus for answering as many questions as they can.  So, why not help them and yourself by becoming a member (free) and ask them your question right now?
Ask Question In Live Forum

If you have an OpenID and do not want to become a member of the EggHeadCafe forum, you can also sign on to Chat Chaos and post your question to our real time Silverlight chat application.
Ask Question In Chat Chaos










  $1000 Contest    [)ia6l0 iii - $231  |  Jonathan VH - $153  |  Huggy Bear - $133  |  egg egg - $100  |  F Cali - $93  |  more Advertise  |  Privacy  |   (c) 2010