Foros del Web » Programación para mayores de 30 ;) » .NET »

Cambiar la fuente de un PDF generado con itextSharp

Estas en el tema de Cambiar la fuente de un PDF generado con itextSharp en el foro de .NET en Foros del Web. Buenas... Estoy trabajando con un proyecto en vb.net 2010, en el cual muestro en un datagridview todos los registros almacenados para luego generar un reporte ...
  #1 (permalink)  
Antiguo 11/12/2012, 12:04
 
Fecha de Ingreso: noviembre-2012
Ubicación: caracas
Mensajes: 53
Antigüedad: 11 años, 5 meses
Puntos: 0
Cambiar la fuente de un PDF generado con itextSharp

Buenas...

Estoy trabajando con un proyecto en vb.net 2010, en el cual muestro en un datagridview todos los registros almacenados para luego generar un reporte en PDF a traves del itextSharp... Ahora la duda que tengo es: Como puedo cambiar el tamaño de la fuente del PDF generado... Le coloco el codigo que tengo a continuacion:

Código:
Imports System.Data.OleDb 'Importacion necesaria para trabajar con ficheros excel
Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Public Class frmEstadoVenezuela
       
    Private Sub btnRegresar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRegresar.Click
        Me.Visible = False
        Me.Dispose()
        frmMenu.Show()
    End Sub

    Private Sub frmEstado_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim cn As New MySqlConnection(frmConexion.ConexionMySQL)
        cn.Open()
        Dim cmdEstadoop As MySqlCommand
        Dim cmdEstadore As MySqlCommand
        Dim sSqlEstadoop As String = "SELECT  campo1, campo2 FROM operaciones"
        Dim sSqlEstadore As String = "SELECT  campo1, campo2 FROM reversos "
        Dim dtEstadoAs New DataTable
        



        cmdEstadoop = New MySqlCommand()
        cmdEstadoop .Connection = cn
        cmdEstadoop .CommandText = sSqlEstadoop 
        cmdEstadoop .CommandType = CommandType.Text

        cmdEstadore = New MySqlCommand()
        cmdEstadore .Connection = cn
        cmdEstadore .CommandText = sSqlEstadore 
        cmdEstadore .CommandType = CommandType.Text


        Dim daEstadoop As New MySqlDataAdapter(cmdEstadoop )
        Dim daEstadore As New MySqlDataAdapter(cmdEstadore )
        Try
            daEstadoop .Fill(dtEstado)
            dgvEstado.DataSource = dtEstado
            dgvEstado.Enabled = True
            dgvEstado.ReadOnly = True
            dgvEstado.Refresh()
            Try
                daEstadore .Fill(dtEstado)
                dgv1.DataSource = dtEstado
                dgv1.Enabled = True
                dgv1.ReadOnly = True
                dgv1.Refresh()
            Catch ex As Exception
                MessageBox.Show("Error al leer los datos!!" & Environment.NewLine & ex.Message.ToString)
                cn.Close()
                Me.Visible = False
                Me.Dispose()
            End Try
        Catch ex As Exception
            MessageBox.Show("Error al leer los datos!!" & Environment.NewLine & ex.Message.ToString)
            cn.Close()
            Me.Visible = False
            Me.Dispose()
            frmMenu.Show()
        End Try
    End Sub

    Public Sub ExportarDatosPDF(ByVal document As Document)
        'Se crea un objeto PDFTable con el numero de columnas del DataGridView.  
        Dim datatable As New PdfPTable(dgv1.ColumnCount)

        'Se asignan algunas propiedades para el diseño del PDF.
        datatable.DefaultCell.Padding = 0
        Dim headerwidths As Single() = GetColumnasSize()
        datatable.SetWidths(headerwidths)
        datatable.WidthPercentage = 100
        datatable.DefaultCell.BorderWidth = 2
        datatable.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER


        'Se crea el encabezado en el PDF.  
        Dim encabezado As New Paragraph("Reporte", New Font(Font.Name = "Tahoma", 20, Font.Bold))
        'Se crea el texto abajo del encabezado.
        Dim texto As New Phrase("Reporte Ejemplo: " + Now.Date(), New Font(Font.Name = "Tahoma", 14, Font.Bold))
        'Se capturan los nombres de las columnas del DataGridView.
        For i As Integer = 0 To dgv1.ColumnCount - 1
            datatable.AddCell(dgv1.Columns(i).HeaderText)
        Next
        datatable.HeaderRows = 1
        datatable.DefaultCell.BorderWidth = 1
        For i As Integer = 0 To dgv1.Rows.Count - 2
            For j As Integer = 0 To dgv1.Columns.Count - 1
                datatable.AddCell((dgv1(j, i).Value).ToString)
            Next
            datatable.CompleteRow()
        Next
        'da 2 tab entre columnas
        datatable.AddCell("")
        datatable.AddCell("")
        'imprime resultados
        'datatable.AddCell(dgvEstadovenezuela(2, 6).Value)
        'datatable.AddCell(dgvEstadovenezuela(3, 6).Value)
        'datatable.CompleteRow()
        'Se agrega etiquetas
        document.Add(encabezado)
        document.Add(texto)
        document.Add(datatable)
    End Sub

    Public Function GetColumnasSize() As Single()
        Dim values As Single() = New Single(dgv1.ColumnCount - 1) {}
        For i As Integer = 0 To dgv1.ColumnCount - 1
            values(i) = CSng(dgv1.Columns(i).Width)
        Next
        Return values
    End Function

    Private Sub btnReporte_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReporte.Click
        Try
            ' Intentar generar el documento.
            Dim doc As New Document(PageSize.A4.Rotate(), 10, 10, 10, 10)

            ' Path que guarda el reporte en el escritorio de windows (Desktop).
            Dim filename As String = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\Reporte.pdf"
            Dim file As New FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite)
            PdfWriter.GetInstance(doc, file)
            doc.Open()
            ExportarDatosPDF(doc)
            doc.Close()
            Process.Start(filename)
        Catch ex As Exception
            'Si el intento es fallido, mostrar MsgBox.
            MessageBox.Show("No se puede generar el documento PDF.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

End Class

Etiquetas: fuente, itextsharp, net, pdf, sql, vb, windows
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 20:53.