You must Sign In to post a response.
Category: Visual Studio
#741734
Hi,
There are many PDFwriter files, i am giving few links for code examples
http://www.codeproject.com/Articles/570682/PDF-File-Writer-Csharp-Class-Library-Version
http://www.developerfusion.com/code/5682/create-pdf-files-on-fly-in-c/
http://www.c-sharpcorner.com/Forums/Thread/129259/export-datagridview-data-to-pdf.aspx
Regards,
Padmasri
There are many PDFwriter files, i am giving few links for code examples
http://www.codeproject.com/Articles/570682/PDF-File-Writer-Csharp-Class-Library-Version
http://www.developerfusion.com/code/5682/create-pdf-files-on-fly-in-c/
http://www.c-sharpcorner.com/Forums/Thread/129259/export-datagridview-data-to-pdf.aspx
Regards,
Padmasri
#741735
Hi,
You can add the grid values into the temp datatable.
try it bellow code will help you
protected void btnExport_Click(object sender, EventArgs e)
{
DataTable dtExport = new DataTable();
for (int j = 0; j < GridView1.Rows.Count; j++)
{
DataRow dr;
GridViewRow row = GridView1.Rows[j];
dr = dtExport .NewRow();
for (int i = 0; i < row.Cells.Count; i++)
{
dr[i] = row.Cells[i].Text;
}
dtExport .Rows.Add(dr);
}
lblerr.Text = "";
Response.Buffer = true;
//Excel sheet Header Text
string filename = " Summary Report" + DateTime.Now.ToString() + ". pdf ";
Response.AddHeader("content-disposition", "attachment; filename=" + filename + ".pdf");
Response.ContentType = "application/vnd.ms-excel";
string tab = "";
//Display column name
foreach (DataColumn dc in dtExport.Columns)
{
HttpContext.Current.Response.Write(tab + dc.ColumnName.ToUpper());
tab = "\t";
}
HttpContext.Current.Response.Write("\n");
int i;
//get table value
foreach (DataRow dr in dtExport.Rows)
{
tab = "";
for (i = 0; i < dtExport.Columns.Count; i++)
{
HttpContext.Current.Response.Write(tab + dr[i].ToString());
tab = "\t";
}
HttpContext.Current.Response.Write("\n");
}
HttpContext.Current.Response.End();
}
You can add the grid values into the temp datatable.
try it bellow code will help you
protected void btnExport_Click(object sender, EventArgs e)
{
DataTable dtExport = new DataTable();
for (int j = 0; j < GridView1.Rows.Count; j++)
{
DataRow dr;
GridViewRow row = GridView1.Rows[j];
dr = dtExport .NewRow();
for (int i = 0; i < row.Cells.Count; i++)
{
dr[i] = row.Cells[i].Text;
}
dtExport .Rows.Add(dr);
}
lblerr.Text = "";
Response.Buffer = true;
//Excel sheet Header Text
string filename = " Summary Report" + DateTime.Now.ToString() + ". pdf ";
Response.AddHeader("content-disposition", "attachment; filename=" + filename + ".pdf");
Response.ContentType = "application/vnd.ms-excel";
string tab = "";
//Display column name
foreach (DataColumn dc in dtExport.Columns)
{
HttpContext.Current.Response.Write(tab + dc.ColumnName.ToUpper());
tab = "\t";
}
HttpContext.Current.Response.Write("\n");
int i;
//get table value
foreach (DataRow dr in dtExport.Rows)
{
tab = "";
for (i = 0; i < dtExport.Columns.Count; i++)
{
HttpContext.Current.Response.Write(tab + dr[i].ToString());
tab = "\t";
}
HttpContext.Current.Response.Write("\n");
}
HttpContext.Current.Response.End();
}
#741759
Hi,
You can use below code if you have iTextSharp dll. You may download the dll from http://sourceforge.net/projects/itextsharp/
Code for exporting data to Excel/Word/PDF and all will be same except the two lines of code where we specifically mention PDF.
Regards,
Asheej T K
You can use below code if you have iTextSharp dll. You may download the dll from http://sourceforge.net/projects/itextsharp/
Code for exporting data to Excel/Word/PDF and all will be same except the two lines of code where we specifically mention PDF.
private void ExportToExcel()
{
string Excelfilename = "Test_pdf_Export" + DateTime.Now;
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition","attachment;filename=PDFFileName.pdf");
Response.Charset = "";
this.EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
GridView1.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
}
Regards,
Asheej T K
#741762
you can use the third part to create dll like Itextsharp.dll.
You will get many example after some google with this.
Hope this helps you
Regards
Shalini Rathore
You will get many example after some google with this.
Hope this helps you
Regards
Shalini Rathore
#741763
using (FileStream fs = new FileStream(Server.MapPath(@"Files\FileForPdf.pdf"), FileMode.Create))
{
using (iTextSharp.text.Document doc = new iTextSharp.text.Document(PageSize.A4, 10f, 10f, 10f, 0f))
{
PdfWriter writer = PdfWriter.GetInstance(doc, fs);
doc.Open();
doc.Add(new Paragraph("hello I am paragraph"));
doc.Close();
}
fs.Close();
fs.Dispose();
}
This will replace the existing blank file with the new one.
Regards
Shalini Rathore
{
using (iTextSharp.text.Document doc = new iTextSharp.text.Document(PageSize.A4, 10f, 10f, 10f, 0f))
{
PdfWriter writer = PdfWriter.GetInstance(doc, fs);
doc.Open();
doc.Add(new Paragraph("hello I am paragraph"));
doc.Close();
}
fs.Close();
fs.Dispose();
}
This will replace the existing blank file with the new one.
Regards
Shalini Rathore
#741790
There are multiple way to create a PDF file, as said by Asheej you can use Response Obejct for create PDF file.
- Other way is to create Word file export all grdiview content to word and then use SaveAs() method of word document and convert it to PDF file.
- Other way is to use iTextSharp library to create PDF file see below sample link
http://www.codeproject.com/Articles/277065/Creating-PDF-documents-with-iTextSharp
Thanks
Koolprasd2003
Editor, DotNetSpider MVM
Microsoft MVP 2014 [ASP.NET/IIS]
- Other way is to create Word file export all grdiview content to word and then use SaveAs() method of word document and convert it to PDF file.
- Other way is to use iTextSharp library to create PDF file see below sample link
http://www.codeproject.com/Articles/277065/Creating-PDF-documents-with-iTextSharp
Thanks
Koolprasd2003
Editor, DotNetSpider MVM
Microsoft MVP 2014 [ASP.NET/IIS]
#751911
You can use free PDFSharp library for create pdf file from C#
http://csharp.net-informations.com/file/create-pdf.htm
Nathan
http://csharp.net-informations.com/file/create-pdf.htm
Nathan
#769499
In one sentence, use ZetPDF. It's a powerful and the fastest pdf SKD for .NET applications to Develop high performance .NET applications having the ability to Create, Edit, Convert, Protect or Print PDF documents, without requiring Adobe Acrobat. Download it from https://zetpdf.com/
#769502
You can use Free Spire.PDF library to easily create PDF from datagridview. Get Free Spire.PDF dll from NuGet (https://www.nuget.org/packages/FreeSpire.PDF/) and refer to the following code:
PdfDocument pdf = new PdfDocument();
PdfPageBase page =pdf.Pages.Add();
PdfTable table = new PdfTable();
table.DataSource = dataGridView1.DataSource;
table.Style.ShowHeader = true;
table.Style.CellPadding = 2;
PdfTableLayoutFormat tableLayout = new PdfTableLayoutFormat();
tableLayout.Break = PdfLayoutBreakType.FitElement;
tableLayout.Layout = PdfLayoutType.Paginate;
table.BeginRowLayout += newBeginRowLayoutEventHandler(table_BeginRowLayout);
table.Draw(page, new RectangleF(10, 50, 300, 300), tableLayout);
pdf.SaveToFile("Output.pdf");
}
private static void table_BeginRowLayout(objectsender, BeginRowLayoutEventArgs args)
{
PdfCellStyle cellstyle = new PdfCellStyle();
cellstyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
cellstyle.Font = new PdfTrueTypeFont(new Font("Arial Unicode MS", 9f), true);
args.CellStyle = cellstyle;
}
PdfDocument pdf = new PdfDocument();
PdfPageBase page =pdf.Pages.Add();
PdfTable table = new PdfTable();
table.DataSource = dataGridView1.DataSource;
table.Style.ShowHeader = true;
table.Style.CellPadding = 2;
PdfTableLayoutFormat tableLayout = new PdfTableLayoutFormat();
tableLayout.Break = PdfLayoutBreakType.FitElement;
tableLayout.Layout = PdfLayoutType.Paginate;
table.BeginRowLayout += newBeginRowLayoutEventHandler(table_BeginRowLayout);
table.Draw(page, new RectangleF(10, 50, 300, 300), tableLayout);
pdf.SaveToFile("Output.pdf");
}
private static void table_BeginRowLayout(objectsender, BeginRowLayoutEventArgs args)
{
PdfCellStyle cellstyle = new PdfCellStyle();
cellstyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
cellstyle.Font = new PdfTrueTypeFont(new Font("Arial Unicode MS", 9f), true);
args.CellStyle = cellstyle;
}
#769714
if you are interested to go for thirdparty components then use epdfcreator and it has lot of features to generate PDF.
so, you simple pass the HTML of gridview then it will generate PDF file or just pass the url then it will convert the html into PDF.
Thanks!
B.Ramana Reddy
so, you simple pass the HTML of gridview then it will generate PDF file or just pass the url then it will convert the html into PDF.
Thanks!
B.Ramana Reddy
Return to Return to Discussion Forum