In this example i'm explaining how to Create Generate Or Export DetailsView To Pdf In Asp.Net Using C# And VB.Net.
I have used iTextsharp for this sample, populate detailsview from database and write code to create pdf from Detailsview Or GridView in Click Event of button.
HTML SOURCE
C#
I have used iTextsharp for this sample, populate detailsview from database and write code to create pdf from Detailsview Or GridView in Click Event of button.
HTML SOURCE
1: <asp:DetailsView ID="dvExport" runat="server"
2: AutoGenerateRows="False"
3: DataSourceID="SqlDataSource1"
4: AllowPaging="True">
5: <Fields>
6: <asp:BoundField DataField="ID" HeaderText="ID"/>
7: <asp:BoundField DataField="Name" HeaderText="Name"/>
8: <asp:BoundField DataField="Location"
9: HeaderText="Location"/>
10: </Fields>
11: </asp:DetailsView>
12:
13: <asp:SqlDataSource ID="SqlDataSource1"
14: runat="server"
15: ConnectionString
16: ="<%$ ConnectionStrings:ConnectionString %>"
17: SelectCommand="SELECT [ID], [Name], [Location]
18: FROM [Test]">
19: </asp:SqlDataSource>
20:
21: <asp:Button ID="btnCreatePdf" runat="server"
22: Text="Create PDF From DetailsView"
23: onclick="btnCreatePdf_Click"/>
C#
using System;
using iTextSharp.text;
using iTextSharp.text.pdf;
protected void btnCreatePdf_Click(object sender, EventArgs e)
{
int rows = dvExport.Rows.Count;
int columns = dvExport.Rows[0].Cells.Count;
int pdfTableRows = rows + 3;
iTextSharp.text.Table PdfTable = new iTextSharp.text.Table(2, pdfTableRows);
PdfTable.BorderWidth = 1;
PdfTable.BorderColor = new Color(0, 0, 255);
PdfTable.Cellpadding = 5;
PdfTable.Cellspacing = 5;
Cell c1 = new Cell("Export Or Create PDF From DetailsView In Asp.Net");
c1.Header = true;
c1.Colspan = 2;
PdfTable.AddCell(c1);
Cell c2 = new Cell("By CsharpAspNetArticles.com");
c2.Colspan = 2;
PdfTable.AddCell(c2);
for (int rowCounter = 0; rowCounter < rows; rowCounter++)
{
for (int columnCounter = 0; columnCounter < columns; columnCounter++)
{
string strValue = dvExport.Rows[rowCounter].Cells[columnCounter].Text;
PdfTable.AddCell(strValue);
}
}
Document Doc = new Document();
PdfWriter.GetInstance(Doc, Response.OutputStream);
Doc.Open();
Doc.Add(PdfTable);
Doc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment; filename=CsharpAspNetArticles.pdf");
Response.End();
}
VB.NET
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Protected Sub btnCreatePdf_Click(sender As Object, e As EventArgs)
Dim rows As Integer = dvExport.Rows.Count
Dim columns As Integer = dvExport.Rows(0).Cells.Count
Dim pdfTableRows As Integer = rows + 3
Dim PdfTable As New iTextSharp.text.Table(2, pdfTableRows)
PdfTable.BorderWidth = 1
PdfTable.BorderColor = New Color(0, 0, 255)
PdfTable.Cellpadding = 5
PdfTable.Cellspacing = 5
Dim c1 As New Cell("Export Or Create PDF From DetailsView In Asp.Net")
c1.Header = True
c1.Colspan = 2
PdfTable.AddCell(c1)
Dim c2 As New Cell("By CsharpAspNetArticles.com")
c2.Colspan = 2
PdfTable.AddCell(c2)
For rowCounter As Integer = 0 To rows - 1
For columnCounter As Integer = 0 To columns - 1
Dim strValue As String = dvExport.Rows(rowCounter).Cells(columnCounter).Text
PdfTable.AddCell(strValue)
Next
Next
Dim Doc As New Document()
PdfWriter.GetInstance(Doc, Response.OutputStream)
Doc.Open()
Doc.Add(PdfTable)
Doc.Close()
Response.ContentType = "application/pdf"
Response.AddHeader("content-disposition", "attachment; filename=CsharpAspNetArticles.pdf")
Response.[End]()
End Sub
If you like this post than join us or share


0 comments:
Post a Comment