Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello, i am working with the Vb.Net Desktop Application. i want to do Data Transfer from Datagrid View To PDF and data is coming from DataSet.
Posted

 
Share this answer
 
v2
Comments
Dipak V Nakum 17-Apr-12 4:47am    
i want to do in the DESKTOP Application Not in the Web.
Hi,
Based on your description, do you mean you want to export data form dataset or datagridview to PDF file?

If so, there are many ways can chieve that:

1.Using Reporting Services report generation: http://www.codeproject.com/KB/reporting-services/ReportExporters_WinForms.aspx
2.Using ReportViewer.Drop a Table report item on an RDLC report, populate it with data and it can be exported to PDF or Excel.
3.Using a free tool.See CompletIT DataGridView Extension on http://www.completit.com/Products/DGVE/Overview.aspx

I am not sure whether it can achieve by Crystal Reports. If you want to use Crustal Report, you can go to the Crystal Reports for Visual Studio Forum for help.
 
Share this answer
 
v2
Comments
Dipak V Nakum 17-Apr-12 4:49am    
i am using Datagridview not Crystal Report.
u just download iTextSharp.dll , it is completly free and working in my application is very well.

just add referance of this dll in your application put this code u will get perfect pdf what u need

using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;

public void CreatePdfFiles()
{
try
{


DataSet objDS = db.GetDSBySP("spname");//here call method and fill dataset

string fPath = Server.MapPath("Report/BalanceDetails.pdf");

if (File.Exists(fPath))
File.Delete(fPath);

Document objDoc = new Document(PageSize.A4, 50, 50, 30, 30);
PdfWriter objWriter = PdfWriter.GetInstance(objDoc, new FileStream(fPath, FileMode.Create));

objDoc.Open();
// set header image table
PdfPTable HeaderImgdatatable = new PdfPTable(2);
HeaderImgdatatable.DefaultCell.Padding = 0;
HeaderImgdatatable.WidthPercentage = 70; // percentage
HeaderImgdatatable.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
HeaderImgdatatable.DefaultCell.BorderWidth = 0;
//HeaderImgdatatable.DefaultCell.MinimumHeight = 20;


float[] HeaderImgcolumnWidths = { 20, 60 }; // This is without barcode
HeaderImgdatatable.SetWidths(HeaderImgcolumnWidths);


//Header set
PdfPTable Headerdatatable = new PdfPTable(1);
Headerdatatable.DefaultCell.Padding = 0;
Headerdatatable.WidthPercentage = 100; // percentage
Headerdatatable.DefaultCell.HorizontalAlignment = Element.ALIGN_LEFT;
Headerdatatable.DefaultCell.BorderWidth = 0;
Headerdatatable.DefaultCell.MinimumHeight = 14;


float[] HeadercolumnWidths = { 100 }; // This is without barcode
Headerdatatable.SetWidths(HeadercolumnWidths);

string[] strArr = new string[4];
strArr[0] = " -:STATEMENT:- ";
strArr[1] = " Comaney Name LTD. ";
strArr[2] = " Address, New Delhi-67";
strArr[3] = "other info";
//strArr[4] = objDSBookShop.Tables[0].Rows[0]["BookShopName"].ToString() + ", " + objDSBookShop.Tables[0].Rows[0]["City"].ToString();
for (int i = 0; i < 4; i++)
{
if (i == 1)
{
Headerdatatable.DefaultCell.BackgroundColor = iTextSharp.text.Color.WHITE;
Phrase phrase = new Phrase(strArr[i].ToString(), FontFactory.GetFont("Times New Roman", 12, iTextSharp.text.Font.BOLD));
Headerdatatable.AddCell(phrase);
}
else
{
Headerdatatable.DefaultCell.BackgroundColor = iTextSharp.text.Color.WHITE;
Phrase phrase = new Phrase(strArr[i].ToString(), FontFactory.GetFont("Verdana", 9));
Headerdatatable.AddCell(phrase);
}
}



HeaderImgdatatable.AddCell(" ");
HeaderImgdatatable.AddCell(Headerdatatable);
objDoc.Add(HeaderImgdatatable);
//objDoc.Add(new Paragraph(strArr[4].ToString()));

iTextSharp.text.Image jpeg = iTextSharp.text.Image.GetInstance(Server.MapPath("images/Logo2.jpg"));
jpeg.SetAbsolutePosition(60, objDoc.PageSize.Height - 90);
objDoc.Add(jpeg);


objDoc.Add(new Paragraph(" "));

int numColumns = objDS.Tables[0].Columns.Count;
PdfPTable datatable = new PdfPTable(numColumns);
datatable.DefaultCell.Padding = 0;
datatable.WidthPercentage = 100; // percentage
datatable.DefaultCell.HorizontalAlignment = Element.ALIGN_LEFT;
if (numColumns == 3)
{
float[] columnWidths = { 30, 30,40 };
datatable.SetWidths(columnWidths);
}
else
{
float[] columnWidths = { 40,20,20,20};
datatable.SetWidths(columnWidths);
}


//header row
datatable.DefaultCell.BorderWidth = 0;
datatable.DefaultCell.GrayFill = 0.8f;
datatable.DefaultCell.MinimumHeight = 20;
//datatable.DefaultCell.VerticalAlignment = Element.ALIGN_MIDDLE;

//get column names

foreach (DataColumn col in objDS.Tables[0].Columns)
{
Phrase phrase = new Phrase(col.ColumnName.ToString(), FontFactory.GetFont("Verdana", 9, iTextSharp.text.Font.BOLD));
datatable.AddCell(phrase);
}
datatable.HeaderRows = 1; // this is the end of the table header

for (int i = 0; i < objDS.Tables[0].Rows.Count; i++)
{
for (int j = 0; j < objDS.Tables[0].Columns.Count; j++)
{
//if (numColumns == 4 && j == 1)
//{
datatable.DefaultCell.BackgroundColor = iTextSharp.text.Color.WHITE;
datatable.DefaultCell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_LEFT;
Phrase phrase = new Phrase(objDS.Tables[0].Rows[i][j].ToString(), FontFactory.GetFont("Verdana", 9));
datatable.AddCell(phrase);
//}
//else
//{
// datatable.DefaultCell.BackgroundColor = iTextSharp.text.Color.WHITE;
// datatable.DefaultCell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_RIGHT;
// Phrase phrase = new Phrase(objDS.Tables[0].Rows[i][j].ToString(), FontFactory.GetFont("Verdana", 9));
// datatable.AddCell(phrase);
//}
}
}

objDoc.Add(datatable);
objDoc.Add(new Paragraph(" "));
objDoc.Add(new Paragraph("Thank You. "));
objDoc.Close();
//ShowPdf(fPath);
DownLoadFile(fPath, true);
}
catch (System.Exception objEx)
{
string strEx = objEx.Message;
}
}
private void DownLoadFile(string fPath, bool isDownLoad) //Function for downloading report document
{
string fullPath = Path.GetFullPath(fPath);
string fileName = Path.GetFileName(fullPath);
string ext = Path.GetExtension(fullPath);
string type = "";
if (!(ext == null))
{
ext = ext.ToLower();
}
switch (ext)
{
case ".pdf":
type = "Application/pdf";
break;
case ".csv":
type = "Application/x-msexcel";
break;
case ".rtf":
type = "Application/msword";
break;
case ".xls":
type = "Application/x-msexcel";
break;

}

if ((isDownLoad))
{
Response.AppendHeader("content-disposition", "attachment; filename=" + fileName);
}
if (type != "")
{
Response.ContentType = type;
}
Response.WriteFile(fullPath);
Response.End();
}
 
Share this answer
 
Comments
Dipak V Nakum 17-Apr-12 5:00am    
i am working with the DESKTOP application not Web Application dear.
 
Share this answer
 
Comments
Dipak V Nakum 17-Apr-12 5:15am    
i had written clearly i am not using crystal report i am using DATAGRIDVIEW.
Use th below code.
VB
Dim rpt As New ReportDocument
     rpt.Load(Server.MapPath("~/Reports/rptAll.rpt"))
     rpt.SetDataSource(Session("D"))
     rpt.SummaryInfo.ReportTitle = "IMM Inquiry Capture And Tracking  From Date" & Session("FromDt") & " To " & Session("ToDt")
     rpt.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, False, "IMM Inquiry Capture And Tracking")
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900