How to export Data Table to PDF file using c# windows application

Asked By masuri suhasini
16-Apr-09 08:02 AM
Earn up to 0 extra points for answering this tough question.

Hi

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

 

  You can use iTextSharp

[)ia6l0 iii replied to masuri suhasini
16-Apr-09 08:19 AM
Check out the following link for more details.
http://itextsharp.sourceforge.net/

CodePlex has a project on this too.

  Re::How to export Data Table to PDF file using c# windows application

Santhosh N replied to masuri suhasini
16-Apr-09 08: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/

  re

Web Star replied to masuri suhasini
16-Apr-09 08: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
  Use PDFSharp
K S replied to masuri suhasini
16-Apr-09 08: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)
  TRY THIS
cool adch replied to masuri suhasini
16-Apr-09 09: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();
}
  TRY THIS
cool adch replied to masuri suhasini
16-Apr-09 09: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
  TRY THIS
cool adch replied to masuri suhasini
16-Apr-09 09: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..
  Thanks for helping
masuri suhasini replied to Santhosh N
20-Apr-09 03:06 AM

Hi,

Santhosh kappa  this site is very use full for me.

thanks

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

 

  Welcome..
Santhosh N replied to masuri suhasini
20-Apr-09 03:46 AM
BTW, i am santhosh Kapa  :)
  re: TRY THIS
sumana replied to cool adch
12-Aug-10 01:02 AM
how can we view the save dPDF doc
'
  re: How to export Data Table to PDF file using c# windows application
Wallace Stephen replied to masuri suhasini
28-Feb-11 12:14 AM
Recommend a safe export way to help you export data from datatable to PDF.
1, directly download my "Datable to PDF" tool.
2, use visual stidio, c# programming and a free .NET component.

Click my blog to know details of steps.
Create New Account