exportar gridview a pdf

6
PARA PODER EXPORTAR UN GRIDVIEW A PDF SE NECESITA UNA LIBRERÍA LLAMADA ITEXTSHARP.DLL, PARA PODER UTILIZARLA SE TIENE QUE AGREGAR UNA REFERENCIA HACIA ESTE ARCHIVO CON extensión dll para agregar una referencia hacer los siguiente: 1.-ir a menu proyecto y hacer click en agregar referencia 2.-en la ventana que se visualiza dar click en el botón examinar y buscar la librería itextsharp.dll

Upload: babaju264899

Post on 10-Feb-2016

43 views

Category:

Documents


2 download

DESCRIPTION

programacion en vb.net

TRANSCRIPT

Page 1: Exportar Gridview a PDF

PARA PODER EXPORTAR UN GRIDVIEW A PDF SE NECESITA UNA LIBRERÍA LLAMADA

ITEXTSHARP.DLL, PARA PODER UTILIZARLA SE TIENE QUE AGREGAR UNA REFERENCIA HACIA ESTE

ARCHIVO CON extensión dll para agregar una referencia hacer los siguiente:

1.-ir a menu proyecto y hacer click en agregar referencia

2.-en la ventana que se visualiza dar click en el botón examinar y buscar la librería itextsharp.dll

Page 2: Exportar Gridview a PDF

El archivo a referenciar se vera de la siguiente manera

Después de dar click en aceptar apareceré en el administrador de referencias y volver a dar click

en aceptar

Ya referenciado podremos utilizar la librería de la siguiente manera en nuestra aplicación lo

primero que tenemos que hacer es importar de la siguiente manera

Imports iTextSharp Imports iTextSharp.text Imports iTextSharp.text.pdf Imports System.IO Imports iTextSharp.text.html Imports iTextSharp.text.html.simpleparser

Page 3: Exportar Gridview a PDF

Imports System.Xml Imports System.Web.UI.HtmlControls

Y vamos a utilizar un componente asp llamado ImageButton

Y en su propiedad ImageUrl buscar una imagen para que aparesca.

Por ejemplo una imagen que haga referencia a que se puede descargar un pdf

Page 4: Exportar Gridview a PDF

Ahora en el evento click del ImageButton (para que aparezca el evento dar doble click sobre el

componente) vamos a crear los siguiente para que exporte a pdf un gridview

Protected Sub ImageButton1_Click(sender As Object, e As ImageClickEventArgs) Handles ImageButton1.Click Response.ClearContent() Response.ContentType = "application/pdf" Response.AddHeader("content-disposition", "attachment; filename=PDFDoc.pdf") Response.Cache.SetCacheability(HttpCacheability.NoCache) Dim StringWriter As New StringWriter() Dim htmlTextWriter As New HtmlTextWriter(StringWriter) GridView1.AllowPaging = False 'GridView1.AllowCustomPaging = False 'GridView1.AllowSorting = False GridView1.Style.Add("font-size", "8px") GridView1.DataBind() GridView1.RenderControl(htmlTextWriter) Dim stringReader As New StringReader(StringWriter.ToString()) Dim pdfDoc As New Document(PageSize.LETTER.Rotate, 10, 10, 20, 100) Dim htmlWorker As New HTMLWorker(pdfDoc) PdfWriter.GetInstance(pdfDoc, Response.OutputStream) pdfDoc.Open() '-------- Dim fonteTitulo As New Font(FontFactory.GetFont("Verdana", 12, iTextSharp.text.Font.BOLD)) fonteTitulo.SetColor(0, 0, 0) Dim titulo As New Chunk("UNIVERSIDAD DEL SURESTE DE MEXICO", fonteTitulo) Dim frase As New Phrase(titulo) Dim p As New Paragraph() p.Alignment = Element.ALIGN_CENTER p.Add(frase) pdfDoc.Add(p) Dim ciclo As New Chunk("CICLO ESCOLAR: 2011-2012", fonteTitulo) Dim ciclofrase As New Phrase(ciclo) Dim p4 As New Paragraph() p4.Alignment = Element.ALIGN_CENTER p4.Add(ciclofrase) pdfDoc.Add(p4) Dim periodo As New Chunk("PERIODO ESCOLAR: AGOSTO - DICIEMBRE 2013", fonteTitulo) Dim periodofrase As New Phrase(periodo) Dim p5 As New Paragraph() p5.Alignment = Element.ALIGN_CENTER p5.Add(periodofrase) pdfDoc.Add(p5)

Page 5: Exportar Gridview a PDF

Dim titulo2 As New Chunk("BOLETA DE CALIFICACIONES", fonteTitulo) Dim frase2 As New Phrase(titulo2) Dim p2 As New Paragraph() p2.Alignment = Element.ALIGN_CENTER p2.Add(frase2) agregarLineasEnBlanco(p2, 0.5) pdfDoc.Add(p2) '-------- Dim grupo As New Chunk(Label3.Text, fonteTitulo) Dim grupofrase As New Phrase(grupo) Dim grupo1 As New Chunk(Label4.Text, fonteTitulo) Dim grupofrase1 As New Phrase(grupo1) Dim semestre As New Chunk(Label7.Text, fonteTitulo) Dim semestrefrase As New Phrase(semestre) Dim semestre1 As New Chunk(Label8.Text, fonteTitulo) Dim semestrefrase1 As New Phrase(semestre1) Dim matricula As New Chunk(Label2.Text, fonteTitulo) Dim matriculafrase As New Phrase(matricula) Dim matricula1 As New Chunk(TextBox1.Text, fonteTitulo) Dim matriculafrase1 As New Phrase(matricula1) Dim nombre As New Chunk(Label9.Text, fonteTitulo) Dim nombrefrase As New Phrase(nombre) Dim nombre1 As New Chunk(Label10.Text, fonteTitulo) Dim nombrefrase1 As New Phrase(nombre1) Dim p3 As New Paragraph() p3.Add(grupofrase) p3.Add(grupofrase1) p3.Add(" ") p3.Add(semestrefrase) p3.Add(semestrefrase1) p3.Add(" ") p3.Add(matricula) p3.Add(matricula1) p3.Add(" ") p3.Add(nombre) p3.Add(nombre1) agregarLineasEnBlanco(p3, 0.5) pdfDoc.Add(p3) '-------- htmlWorker.Parse(stringReader) pdfDoc.Close() Response.Write(pdfDoc) Response.End()

Page 6: Exportar Gridview a PDF

End Sub Crear el siguiente procedimiento Public Overloads Overrides Sub VerifyRenderingInServerForm(ByVal control As Control) Return End Sub Tambier crear el siguiente procedimiento Private Sub agregarLineasEnBlanco(parrafo As Paragraph, nLineas As Integer) 'Dim parrafo As New Paragraph Dim i As Integer For i = 0 To nLineas parrafo.Add(New Paragraph(" ")) Next End Sub