C# .NET - How to export Data Table to PDF file using c# windows application
Asked By masuri suhasini
16-Apr-09 08:02 AM
Hi
can anyone help about how to export or write data table to PDF file.
You can use iTextSharp
Check out the following link for more details.
http://itextsharp.sourceforge.net/
CodePlex has a project on this too.
http://www.codeplex.com/Exporter
Santhosh N replied to masuri suhasini
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
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
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

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

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
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..
try this link
http://www.codeproject.com/KB/graphics/iTextSharpTutorial.aspx
http://www.codeproject.com/cs/library/giospdfnetlibrary.asp
http://asp-net-whidbey.blogspot.com/2006/04/generating-pdf-files-with-itextsharp.html
Thanks for helping
Hi,
Santhosh kappa this site is very use full for me.
thanks
http://www.codeproject.com/KB/graphics/iTextSharpTutorial.aspx
Welcome..
BTW, i am santhosh Kapa :)
sumana replied to cool adch
how can we view the save dPDF doc
'
Wallace Stephen replied to masuri suhasini
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 http://stephenchy520.blog.com/2011/02/28/the-safest-way-to-export-datatable-to-pdf-by-easily-using-c/ to know details of steps.

The SQL Server FILESTREAM Type Developers often work with unstructured data such as text documents, images, and videos. This the database, separate from its relational structured data. This separation can cause data management complexities. FILESTREAM integrates the SQL Server Database Engine with an NTFS file system by storing varbinary(max data. This is an excellent solution for say, a web site that hosts video content. FILESTREAM uses the NT system cache for caching file data. This helps reduce any effect that FILESTREAM data might have on Database Engine performance. The SQL Server buffer pool is not used therefore, this memory is available for query processing. When Should You Use FILESTREAM: The size and use of the data determines whether you should use database storage or file system storage. If the following conditions are true, you should consider using FILESTREAM: Objects that are being stored are, on average, larger than 1 MB. Fast read access varbinary(max) BLOBs in the database often provides better streaming performance. To enable and change FILESTREAM settings: On the Start menu, point to All Programs, point to Microsoft SQL Server 2008 Manager snap-in, locate the instance of SQL Server on which you want to enable FILESTREAM. Right-click the instance, and then click Properties. In the SQL Server Properties dialog box
so for this purpose i get the first 500 records and write it into a FileStream and again try to write another 500 to same file using fileStream.Write method but every time it overwrite on previous data where as i wnat to thread / 78ba0439-8063-49d3-a4cd-8e76c65a67b1 / Regards, Santhosh open your file in append mode as , FileStream fileStream = new FileStream ( @"c: \ abc.txt" , FileMode .Append) ; then write as many times u want, it wont overwrite existing values. ' Set the stream position to the beginning of the stream. fileStream.Seek(0, SeekOrigin.Begin) ' Set the stream position to the End of the stream. fileStream.Seek(0, SeekOrigin.End) Otherwise, as mentioned in other post you can open the file
logHandler ("Process() end"); } } } / / The FileLogger class merely encapsulates the file I / O public class FileLogger { FileStream fileStream; StreamWriter streamWriter; / / Constructor public FileLogger(string filename) { fileStream = new FileStream(filename, FileMode.Create); streamWriter = new StreamWriter(fileStream); } / / Member Function which is used in the Delegate public void Logger (string
How to read word doc using FileStream and StreamReader Class Can anyone tell me how to read word file using Stream Classes ref nullobj); WordApp.Quit(ref nullobj, ref nullobj, ref nullobj); or try this using filestram. . . FileStream fstream = new FileStream("Sample.doc", FileMode.Open, FileAccess.Read); StreamReader sreader = new StreamReader(fstream); txtFileContent.Text = sreader.ReadToEnd(); The below links ref nullobj); WordApp.Quit( ref nullobj, ref nullobj, ref nullobj); or try this using filestram. . . FileStream fstream = new FileStream( "Sample.doc" , FileMode.Open, FileAccess.Read); StreamReader sreader = new StreamReader(fstream); txtFileContent.Text = sreader.ReadToEnd(); Hi, try the also allow us to incorporate data types in files. The most commonly used classes are FileStream, BinaryReader, BinaryWriter, StreamReader and StreamWriter. FileStream Class This class provides access to standard input and