none
Error al crear un PDF con la librería itextsharp RRS feed

  • Pregunta

  • Private Sub btnCrear_Click(sender As Object, e As EventArgs) Handles btnCrear.Click
            Dim doc = New Document(PageSize.LETTER)
            Dim SaveFileDialog As New SaveFileDialog
            Dim ruta As String
            With SaveFileDialog
                .Title = "Guardar"
                .InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
                .Filter = "Archivos pdf (.pdf)|.pdf"
                .FileName = "Calculo"
                .OverwritePrompt = True
                .CheckPathExists = True
            End With
            If SaveFileDialog.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
                ruta = SaveFileDialog.FileName
            Else
                ruta = String.Empty
                Exit Sub
            End If
            Try
    
                Dim document As New iTextSharp.text.Document(PageSize.A4)
    
                document.PageSize.Rotate()
                document.AddTitle("Crear pdf")
                Dim writer As PdfWriter = PdfWriter.GetInstance(document, New System.IO.FileStream(ruta, System.IO.FileMode.Create))
                writer.ViewerPreferences = PdfWriter.PageLayoutSinglePage
                document.Open()
                Dim cb As PdfContentByte = writer.DirectContent
                Dim bf As BaseFont = BaseFont.CreateFont(BaseFont.COURIER_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED)
                cb.SetFontAndSize(bf, 12)
                cb.BeginText()
    
                cb.SetTextMatrix(50, 790)
                Dim myfont As iTextSharp.text.Font = New iTextSharp.text.Font(FontFactory.GetFont(FontFactory.TIMES_ROMAN, 12, iTextSharp.text.Font.NORMAL))
                Dim img As Image = Image.GetInstance("pdf1.jpg")
                img.ScalePercent(50.5)
                img.SetAbsolutePosition(450, 785)
                document.Add(img)
                document.Add(New Paragraph("                                                                   Cálculo de Lámina", myfont))
                document.Add(New Paragraph("Cliente: " + txtCliente.Text, myfont))
                document.Add(New Paragraph("Sistema: " + txtSistema.Text, myfont))
                document.Add(New Paragraph("Área: " + txtArea.Text, myfont))
                document.Add(New Paragraph("Elaboró: " + txtElaboro.Text, myfont))
                document.Add(New Paragraph("Fecha: " + Dtpfecha.Value, myfont))
    
    
                document.Add(New Paragraph(" "))
                document.Add(New Paragraph(" "))
    
    
                Dim pdfTable As New PdfPTable(dgwdatos.ColumnCount)
                Dim Tabletotales As New PdfPTable(dgwdatos.ColumnCount)
                pdfTable.HorizontalAlignment = Element.ALIGN_MIDDLE
    
    
    
                pdfTable.DefaultCell.BorderWidth = 1
    
                'encabezado
                Dim fuenteEncabezado As iTextSharp.text.Font = New iTextSharp.text.Font(FontFactory.GetFont(FontFactory.TIMES_ROMAN, 8, iTextSharp.text.Font.NORMAL))
    
                For Each column As DataGridViewColumn In dgwdatos.Columns
                    Dim cell As New PdfPCell(New Phrase(column.HeaderText, fuenteEncabezado))
                    cell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER
                    cell.VerticalAlignment = iTextSharp.text.Element.ALIGN_TOP
    
                    cell.BackgroundColor = iTextSharp.text.pdf.ExtendedColor.LIGHT_GRAY
                    pdfTable.AddCell(cell)
                Next
    
                'Adding DataRow
                For Each row As DataGridViewRow In dgwdatos.Rows
                    For Each cell As DataGridViewCell In row.Cells
                        Dim celda As New pdf.PdfPCell(New Phrase(cell.Value.ToString(), myfont))
                        celda.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER
                        celda.VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE
                        pdfTable.AddCell(celda)
    
                    Next
    
                Next
                pdfTable.WidthPercentage = 100.0F
                document.Add(pdfTable)
                Tabletotales.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER
                Tabletotales.AddCell(" ")
    
                Dim txt As New pdf.PdfPCell(New Phrase("Total", myfont))
                txt.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER
                txt.VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE
                Tabletotales.AddCell(txt)
    
                Dim txt1 As New pdf.PdfPCell(New Phrase(txtltt.Text, myfont))
                txt1.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER
                txt1.VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE
                Tabletotales.AddCell(txt1)
    
                Dim txt2 As New pdf.PdfPCell(New Phrase(txtc26t.Text, myfont))
                txt2.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER
                txt2.VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE
                Tabletotales.AddCell(txt2)
    
                Dim txt3 As New pdf.PdfPCell(New Phrase(txtc24t.Text, myfont))
                txt3.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER
                txt3.VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE
                Tabletotales.AddCell(txt3)
    
                Dim txt4 As New pdf.PdfPCell(New Phrase(txtc22t.Text, myfont))
                txt4.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER
                txt4.VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE
                Tabletotales.AddCell(txt4)
    
                Dim txt5 As New pdf.PdfPCell(New Phrase(txtc20t.Text, myfont))
                txt5.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER
                txt5.VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE
                Tabletotales.AddCell(txt5)
    
                Dim txt6 As New pdf.PdfPCell(New Phrase(txtc18t.Text, myfont))
                txt6.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER
                txt6.VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE
                Tabletotales.AddCell(txt6)
    
                Dim txt7 As New pdf.PdfPCell(New Phrase(txtc16t.Text, myfont))
                txt7.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER
                txt7.VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE
                Tabletotales.AddCell(txt7)
    
                Dim txt8 As New pdf.PdfPCell(New Phrase(txtaisintt.Text, myfont))
                txt8.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER
                txt8.VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE
                Tabletotales.AddCell(txt8)
    
                Dim txt9 As New pdf.PdfPCell(New Phrase(txtaisextt.Text, myfont))
                txt9.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER
                txt9.VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE
                Tabletotales.AddCell(txt9)
    
                Tabletotales.WidthPercentage = 100.0F
    
    
                document.Add(Tabletotales)
                cb.EndText()
                document.Close()
                MessageBox.Show("El PDF se guardo con éxito", "", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Catch ex As Exception
                MessageBox.Show("Error en la creacion de la nota", "", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
    
    
        End Sub

    Tengo ese código, al compilarlo con Visual studio funciona perfectamente, pero al publicarlo (instalarlo y demás) me tira error. Alguien que tenga idea de que puede ser? gracias!
    martes, 31 de julio de 2018 14:54