Friday 20 September 2013

Create Export DetailsView To PDF In Asp.Net


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 tocreate pdf from Detailsview Or GridView in Click Event of button.

Create Export DetailsView To PDF


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#


01using System;
02using iTextSharp.text;
03using iTextSharp.text.pdf;
04protected void btnCreatePdf_Click(object sender, EventArgs e)
05    {
06        int rows = dvExport.Rows.Count;
07        int columns = dvExport.Rows[0].Cells.Count;
08        int pdfTableRows = rows + 3;
09        iTextSharp.text.Table PdfTable = new iTextSharp.text.Table(2, pdfTableRows);
10        PdfTable.BorderWidth = 1;
11        PdfTable.BorderColor = new Color(0, 0, 255);
12        PdfTable.Cellpadding = 5;
13        PdfTable.Cellspacing = 5;
14        Cell c1 = new Cell("Export Or Create PDF From DetailsView In Asp.Net");
15        c1.Header = true;
16        c1.Colspan = 2;
17        PdfTable.AddCell(c1);
18        Cell c2 = new Cell("By CsharpAspNetArticles.com");
19        c2.Colspan = 2;
20        PdfTable.AddCell(c2);
21 
22        for (int rowCounter = 0; rowCounter < rows; rowCounter++)
23        {
24            for (int columnCounter = 0; columnCounter < columns; columnCounter++)
25            {
26                string strValue = dvExport.Rows[rowCounter].Cells[columnCounter].Text;
27                PdfTable.AddCell(strValue);
28            }
29        }
30        Document Doc = new Document();
31        PdfWriter.GetInstance(Doc, Response.OutputStream);
32        Doc.Open();
33        Doc.Add(PdfTable);
34        Doc.Close();
35        Response.ContentType = "application/pdf";
36        Response.AddHeader("content-disposition", "attachment; filename=CsharpAspNetArticles.pdf");
37        Response.End();
38    }
VB.NET


01Imports iTextSharp.text
02Imports iTextSharp.text.pdf
03 
04Protected Sub btnCreatePdf_Click(sender As Object, e As EventArgs)
05 Dim rows As Integer = dvExport.Rows.Count
06 Dim columns As Integer = dvExport.Rows(0).Cells.Count
07 Dim pdfTableRows As Integer = rows + 3
08 Dim PdfTable As New iTextSharp.text.Table(2, pdfTableRows)
09 PdfTable.BorderWidth = 1
10 PdfTable.BorderColor = New Color(0, 0, 255)
11 PdfTable.Cellpadding = 5
12 PdfTable.Cellspacing = 5
13 Dim c1 As New Cell("Export Or Create PDF From DetailsView In Asp.Net")
14 c1.Header = True
15 c1.Colspan = 2
16 PdfTable.AddCell(c1)
17 Dim c2 As New Cell("By CsharpAspNetArticles.com")
18 c2.Colspan = 2
19 PdfTable.AddCell(c2)
20 
21 For rowCounter As Integer = 0 To rows - 1
22  For columnCounter As Integer = 0 To columns - 1
23   Dim strValue As String = dvExport.Rows(rowCounter).Cells(columnCounter).Text
24   PdfTable.AddCell(strValue)
25  Next
26 Next
27 Dim Doc As New Document()
28 PdfWriter.GetInstance(Doc, Response.OutputStream)
29 Doc.Open()
30 Doc.Add(PdfTable)
31 Doc.Close()
32 Response.ContentType = "application/pdf"
33 Response.AddHeader("content-disposition", "attachment; filename=CsharpAspNetArticles.pdf")
34 Response.[End]()
35End Sub

No comments:

Post a Comment