Generate PDF and insert tables in each page using iTextSharp in ASP.Net

Last Reply 5 months ago By Andrea

Posted 5 months ago

I'm trying to create a pdf document whit several tables (each one in a page of the pdf) but i cannot get it and don't know why.

i need help, please!

My code is (more or less) like this...

 

        Dim PathIn As String = Server.MapPath("Descargables/Plantilla.pdf")
        Dim PathOut As String = Server.MapPath("Descargables/Informe.pdf")
        Dim Reader As PdfReader = New PdfReader(PathIn)
        Dim Stamper As PdfStamper = New PdfStamper(Reader, New System.IO.FileStream(PathOut, System.IO.FileMode.Create))
        Dim docCelda As PdfPCell
        Dim pdfTabla As PdfPTable
        Dim fuenteEncabezado As text.Font = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 12, iTextSharp.text.Font.NORMAL)
        Dim fuenteDatos As text.Font = FontFactory.GetFont(FontFactory.COURIER, 10, iTextSharp.text.Font.NORMAL)
        Dim nFila As Integer, nColu As Integer

        For numPage As Integer = 0 To dtTabla.Rows.Count() - 1
            If numPagina > 0 Then
                Dim Rctangle As iTextSharp.text.Rectangle = Reader.GetPageSize(1)
                Stamper.InsertPage(numPagina, Rctangle)
            End If

            txSql = "SELECT ..." ' Select to get data for page number numPage in the final document
            Dim dtTb As DataTable = capaLogica.negUsuario.Ejecutar_SELECT(txSql)
            pdfTabla = New PdfPTable(dtTb.Columns.Count())

            'Set title in the first row of the table
            docCelda = New PdfPCell(New Phrase("TABLE FOR PAGE NUMBER " & Format(numPage), fuenteEncabezado))
            docCelda.Padding = 5
            docCelda.Colspan = dtTb.Columns.Count()
            docCelda.HorizontalAlignment = PdfPCell.ALIGN_CENTER
            docCelda.VerticalAlignment = PdfPCell.ALIGN_MIDDLE
            docCelda.BackgroundColor = BaseColor.GRAY
            pdfTabla.AddCell(docCelda)

            'Set headers
            nColu = 1
            For Each oColumna As Data.DataColumn In dtTb.Columns
                docCelda = New pdf.PdfPCell(New Phrase(oColumna.Caption, fuenteEncabezado))
                docCelda.Padding = 5
                docCelda.Colspan = 1
                docCelda.HorizontalAlignment = PdfPCell.ALIGN_LEFT
                docCelda.VerticalAlignment = PdfPCell.ALIGN_MIDDLE
                docCelda.BackgroundColor = BaseColor.GRAY
                'docCelda.Width = sDimen(nColu)
                nColu += 1
                pdfTabla.AddCell(docCelda)
            Next oColumna

            'set content
            nFila = 1
            For Each oFila As Data.DataRow In dtTb.Rows
                nColu = 1
                For Each oColumna As Data.DataColumn In dtTb.Columns
                    docCelda = New pdf.PdfPCell(New Phrase(oFila(oColumna), fuenteDatos))
                    docCelda.Padding = 3
                    docCelda.Colspan = 1
                    docCelda.HorizontalAlignment = PdfPCell.ALIGN_LEFT
                    docCelda.VerticalAlignment = PdfPCell.ALIGN_MIDDLE
                    docCelda.BackgroundColor = IIf(nFila Mod 2 = 0, BaseColor.YELLOW, BaseColor.WHITE)
                    'docCelda.Width = sDimen(nColu)  ' i would like to change the width of the columns, but it doesn't work
                    nColu += 1
                    pdfTabla.AddCell(docCelda)
                Next oColumna
                nFila += 1
            Next oFila
 
            ' I'm not sure how to add the table to the pdf page!! I did this...
            Dim Ct As ColumnText = New ColumnText(Stamper.GetOverContent(numPagina + 1))
            Ct.AddElement(pdfTabla)
            Dim Rct As iTextSharp.text.Rectangle = Reader.GetPageSize(1)
            Ct.SetSimpleColumn(36, 36, PageSize.A4.Width - 36, PageSize.A4.Height - 175)
            Ct.Go()
        Next
	
	'Close PDF document
        Stamper.Close()
        Reader.Close()

 

Posted 5 months ago
Hi @calabau,
Please try the following

Create and save PDF to local disk using iTextSharp in ASP.Net

It might help you.

Cheers Andrea.
I agree, here is the link: https://www.e-iceblue.com/Introduce/spire-office-for-net-free.html?aff_id=108