Showing posts with label Export pdf. Show all posts
Showing posts with label Export pdf. Show all posts

9/9/08

Export PDf asp.net, Gridview to Pdf, Open Source PDF Libraries in C#

Recently I have been trying to generate some reports to PDF file format from ASP. NET2.0 application. There are a lot of open source PDF libraries out there that you can use to export to PDF such as iTextSharp, Gios PDF .NET Library and PDFSharp. You can go to this link to find out more open source PDF libraries in C#.

Later I will show you a working solution on how to export GridView to PDF by using one of the free libraries – iTextSharp.

ITextSharp is a port of the iText open source java library written entirely in C# for the .NET platform. It is a library that allows developers to extend the capabilities of their web server applications with dynamic PDF document generation and generate PDF file on the fly.

Before that, you need to download the iTextSharp library. Here is the download link.

Add in the iTextSharp.dll as a reference into your web application.

Here is my sample of code:


using iTextSharp.text;
using iTextSharp.text.pdf;

protected void Page_Load(object sender, EventArgs e)
{
ExportToPDF();
}

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();
}


Hope these codes can help those people who are new to asp.net developing and save some time on their searching solutions. You can also find the tutorial of iTextSharp at here.

How to create a PDF File in C#

FileStream fs=new FileStream(name,FileMode.Create,FileAccess.Write);

StreamWriter sw=new StreamWriter(fs,System.Text.Encoding.GetEncoding("gb2312"));

sw.WriteLine("Hello World");

sw.Close();

// If inside of a page:

Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(name));
Response.ContentType = "application/pdf";

Response.WriteFile(name);
Response.End();

Export ASP.NET page to Word, Excel or PDF

Many times we would like to export our page as an Excel sheet, Word doc or PDF file. This can be simply achieved by changing the ContentType of the Response object and overriding the RenderControl method of the control to be exported:

Page Load()
{
//bind data to data bound controls and do other stuff

Response.Clear(); //this clears the Response of any headers or previous output
Response.Buffer = true; //make sure that the entire output is rendered simultaneously

///
///Set content type to MS Excel sheet
///Use "application/msword" for MS Word doc files
///"application/pdf" for PDF files
///

Response.ContentType = "application/vnd.ms-excel";
StringWriter stringWriter = new StringWriter(); //System.IO namespace should be used

HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);

///
///Render the entire Page control in the HtmlTextWriter object
///We can render individual controls also, like a DataGrid to be
///exported in custom format (excel, word etc)
///
this.RenderControl(htmlTextWriter);
Response.Write(stringWriter.ToString());
Response.End();
} //end page load

For exporting ASP.NET pages as PDF files, you need to know the Adobe PDF specs for generating the PDf correctly. There is already a wonderful component which does the same and its free! See this article along with sample source code on how to export a page to PDF:

http://www.codeproject.com/cs/library/giospdfnetlibrary.asp

Hope this would be helpful...!

Welcome