Subscribe to Subscribers

Forums » .NET » .NET »

Export Datable to PDF


Posted Date: 18 Jun 2012      Posted By:: Pradeep.p     Member Level: Bronze    Member Rank: 2485     Points: 1   Responses: 3



Hi All,


I would like to Export Datable to PDF using windows application
pls let me know the resource

Thanks & Regards,
Pradeep




Responses

#675990    Author: chidambaram      Member Level: Gold      Member Rank: 335     Date: 18/Jun/2012   Rating: 2 out of 52 out of 5     Points: 4

hi,
first download the itextsharp.dll from

http://sourceforge.net/projects/itextsharp/

then add it reference to your windows application.

Then try this

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


public static void Main(string[] args)
{


//creation of a document-object
Document document = new Document();

try
{


// we create a writer that listens to the document
// and directs a PDF-stream to a file

PdfWriter.GetInstance(document, new FileStream("D:\\pdfcreated\\file.pdf", FileMode.Create));

//add some metadata and open the document

document.AddTitle("dotnet spider");

document.AddKeywords("Metadata abcd");

document.AddAuthor("your name");

document.Open();

// add a paragraph to the document
document.Add(new Paragraph("Hello World"));

}
catch (DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch (IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}

//close the document
document.Close();

}


goodluck..

With Regards,
Chidambaram
+91 94898 10101


 
#676033    Author: Pradeep.p      Member Level: Bronze      Member Rank: 2485     Date: 18/Jun/2012   Rating: 2 out of 52 out of 5     Points: 1

I could solve with the following code


public void ExportToPdf(DataTable dt)
{

Document document = new Document();
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("c://newchk.pdf", FileMode.Create));
document.Open();
//iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance("c://ggi logo.bmp");
//document.Add(img);
iTextSharp.text.Font font5 = iTextSharp.text.FontFactory.GetFont(FontFactory.HELVETICA, 5);
//float[] columnDefinitionSize = { 22F, 22F, 12F, 7.75F, 7.77F, 7.77F, 7.77F, 7.77F, 10.88F, 10.88F, 10.88F, 4.75F, 7.77F, 7.77F, 7.77F, 7.77F, 7.77F, 7.77F, 9F };

PdfPTable table = new PdfPTable(dt.Columns.Count);
PdfPRow row = null;
float[] widths = new float[] { 4f, 4f, 4f, 4f };

table.SetWidths(widths);

table.WidthPercentage = 100;
int iCol = 0;
string colname = "";
PdfPCell cell = new PdfPCell(new Phrase("Products"));


////table.AddCell(cell);
cell.Colspan = dt.Columns.Count;

//cell.Border = 0;

//cell.HorizontalAlignment = 1;
foreach (DataColumn c in dt.Columns)
{

table.AddCell(new Phrase(c.ColumnName, font5));
}



//cell.BackgroundColor = new iTextSharp.text.Color(0xC0, 0xC0, 0xC0);


foreach (DataRow r in dt.Rows)
{
if (dt.Rows.Count > 0)
{
table.AddCell(new Phrase(r[0].ToString(), font5));
table.AddCell(new Phrase(r[1].ToString(), font5));
table.AddCell(new Phrase(r[2].ToString(), font5));
table.AddCell(new Phrase(r[3].ToString(), font5));



}


} document.Add(table);
document.Close();
}

with regards,
Pradeep


 
#676313    Author: Ultimaterengan        Member Level: Gold      Member Rank: 9     Date: 19/Jun/2012   Rating: 2 out of 52 out of 5     Points: 4

This Application is used to convert Gridview to PDF document
using itext itextsharp(Its a free tool).
Download the itextsharp from link given below and add this dll in the bin directory.
http://sourceforge.net/projects/itextsharp/
or
http://nodevice.in/dll/itextsharp_dll/item21198.html
After placing the dll in the bin directory write the below code in the aspx.vb page.

In your code-behind, try just adding

Imports iTextSharp.text.pdf
Imports iTextSharp.text

Here with i had attached the sample project.



Imports iTextSharp.text.pdf
Imports iTextSharp.text
Imports System.IO
Imports System.Data
Imports System.Data.SqlClient
Partial Class _Default
Inherits System.Web.UI.Page
Dim ds As New DataSet

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
' ShowGridview()
End If
End Sub

Sub ShowGridview()
'bind the database values to Gridveiw using dataset control
Dim con As SqlClient.SqlConnection
Dim da As SqlDataAdapter

Dim str As String
str = "USER=UserID;PASSWORD=PASSWORD;SERVER=SERVER-Name;DATABASE=DATABASE-Name"
con = New SqlConnection(str)
da = New SqlDataAdapter("select FieldName1,FieldName2 from Table_Name", con)
da.Fill(ds, "Employee")
ViewState("DataSetValue") = ds
grvGrid.DataSource = ds
grvGrid.DataBind()
con.Close()

End Sub
Private Sub ConvertGridviewToPdfDocument(ByVal dstHeader2 As DataSet)
Dim doc As Document = New Document
PdfWriter.GetInstance(doc, New FileStream(Request.PhysicalApplicationPath + "\3.pdf", FileMode.Create))
doc.Open()
Dim table As New PdfPTable(dstHeader2.Tables(0).Columns.Count)
Dim widths As Integer() = {25, 25}
table.WidthPercentage = "100"
For i As Integer = 0 To dstHeader2.Tables(0).Columns.Count - 1
Dim ColumnHeader As New Paragraph(dstHeader2.Tables(0).Columns(i).ColumnName.ToString(), FontFactory.GetFont("verdana", 11))
ColumnHeader.Font.SetStyle(Font.BOLD)
Dim cell As New PdfPCell(New Phrase(ColumnHeader))
cell.HorizontalAlignment = Element.ALIGN_CENTER
table.AddCell(cell)
Next

For k As Integer = 0 To dstHeader2.Tables(0).Rows.Count - 1
For j As Integer = 0 To dstHeader2.Tables(0).Columns.Count - 1
Dim ColumnValue As New Paragraph(dstHeader2.Tables(0).Rows(k)(j).ToString(), FontFactory.GetFont("verdana", 10))
table.AddCell(ColumnValue)
Next
Next
table.SetWidths(widths)
doc.Add(table)
doc.Add(New Paragraph(vbLf))
doc.Close()
Response.Redirect("~/3.pdf")
End Sub

Protected Sub btnConvertGridviewToPDF_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnConvertGridviewToPDF.Click
'ConvertGridviewToPdfDocument(ViewState("DataSetValue"))
Dim str As String = ""
Dim doc As Document = New Document
PdfWriter.GetInstance(doc, New FileStream(FileUpload1.PostedFile.FileName, FileMode.Create))
'string strFileNameWithPath = FileUpload1.PostedFile.FileName;
doc.Open()
doc.Close()

End Sub
End Class




Thanks & Regards
G.Renganathan
Nothing is mine ,Everything is yours!!!


http://renganathan1984.blogspot.com/





 
Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.



Next : What is DOM? Explain with proper explanation and code...
Previous : Sorting a List value in c#
Return to Discussion Forum
Post New Message
Category:

Related Messages

Awards & Gifts
Talk to Webmaster Tony John
Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
2005 - 2013 All Rights Reserved.
.NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
Articles, tutorials and all other content offered here is for educational purpose only.
We are not associated with Microsoft or its partners.