Click here to Skip to main content
15,903,724 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Imports System.IO
Imports System.Data
Imports System.Reflection
Imports iTextSharp.text
Imports iTextSharp.text.pdf

Public Class Form1

    Public Sub New()
        InitializeComponent()
        Me.BindDataGridView()
    End Sub

    Private Sub BindDataGridView()
        Dim dt As New DataTable()
        dt.Columns.AddRange(New DataColumn() {New DataColumn("Id", GetType(Integer)),
                                               New DataColumn("Name", GetType(String)),
                                               New DataColumn("Country", GetType(String))})
        For i As Integer = 0 To 7 Step 1
            dt.Rows.Add(1, "John", "United States")
            dt.Rows.Add(2, "kumar", "India")
            dt.Rows.Add(3, "Mathews", "France")
            dt.Rows.Add(4, "Robert", "Russia")
        Next
        Me.dataGridView1.DataSource = dt
    End Sub

    Private Sub btnExportPDF_Click(sender As System.Object, e As System.EventArgs) Handles btnExportPDF.Click
        'Creating iTextSharp Table from the DataTable data
        Dim pdfTable As New PdfPTable(3)
        pdfTable.DefaultCell.Padding = 3
        pdfTable.WidthPercentage = 30
        pdfTable.HorizontalAlignment = Element.ALIGN_CENTER
        pdfTable.DefaultCell.BorderWidth = 0

        'Adding Header row
        For Each column As DataGridViewColumn In dataGridView1.Columns
            Dim cell As New PdfPCell(New Phrase(column.HeaderText))
            cell.BackgroundColor = New iTextSharp.text.Color(240, 240, 240)
            pdfTable.AddCell(cell)
        Next

        'Adding DataRow
        For Each row As DataGridViewRow In dataGridView1.Rows
            For Each cell As DataGridViewCell In row.Cells
                pdfTable.AddCell(cell.Value.ToString())
            Next
        Next

        'Exporting to PDF
        Dim folderPath As String = "C:\PDFs\"

        If Not Directory.Exists(folderPath) Then
            Directory.CreateDirectory(folderPath)
        End If

        'Document itextDoc = New Document();
        'PdfWriter pdfDoc = PdfWriter.GetInstance(itextDoc, Response.OutputStream);
        'pdfDoc.PageEvent = New itextEvents();

        'Dim pdfDoc As New Document()
        'Dim pdfWrite As PdfWriter = PdfWriter.GetInstance(pdfDoc, New FileStream("tryme2.pdf", FileMode.Create))

        'assigning pdf file name 
        Dim myUniqueFileName As String = String.Format("_{0}.pdf", DateTime.Now.[Date].Day.ToString() + "-" & DateTime.Now.[Date].Month.ToString() + "-" & DateTime.Now.[Date].Year.ToString() + "@" & DateTime.Now.TimeOfDay.Hours.ToString() + "." & DateTime.Now.TimeOfDay.Minutes.ToString() + "." & DateTime.Now.TimeOfDay.Milliseconds.ToString())


        Using stream As New FileStream(folderPath & "Invoice" & myUniqueFileName, FileMode.Create)
            Dim pdfDoc As New Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 0.0F)
            Dim pdfWrite As PdfWriter = PdfWriter.GetInstance(pdfDoc, stream)
            Dim ev As New itextEvents
            pdfWrite.PageEvent = ev
            pdfDoc.Open()

            'pdfDoc.AddHeader()

            Dim PdfTable2 As New PdfPTable(2)
            Dim Header As New PdfPCell(FormatHeader1("Trident Express Courier Service"))
            Header.Colspan = 2
            Header.HorizontalAlignment = 1
            Header.Border = 0
            PdfTable2.AddCell(Header)
            pdfDoc.Add(PdfTable2)

            Dim PdfTable3 As New PdfPTable(2)
            Dim address As New PdfPCell(FormatHeader2("No. 1, dummy House, 1, dummy  Road"))
            address.Colspan = 2
            address.HorizontalAlignment = 1
            address.Border = 0
            PdfTable3.AddCell(address)
            pdfDoc.Add(PdfTable3)

            Dim PdfCity As New PdfPTable(2)
            Dim CityValue As New PdfPCell(FormatHeader2("dummycity - 100000, Ph : 000-0000000"))
            CityValue.Colspan = 2
            CityValue.HorizontalAlignment = 1
            CityValue.BorderWidthLeft = 0
            CityValue.BorderWidthRight = 0
            CityValue.BorderWidthTop = 0
            CityValue.BorderWidthBottom = 2
            CityValue.PaddingBottom = 20
            PdfCity.AddCell(CityValue)
            pdfDoc.Add(PdfCity)


            Dim Pdfhearedline As New PdfPTable(2)
            Dim headerline As New PdfPCell(New Phrase(""))
            headerline.Colspan = 2
            headerline.HorizontalAlignment = 1
            headerline.Border = 0
            headerline.Padding = 4
            Pdfhearedline.AddCell(headerline)
            pdfDoc.Add(Pdfhearedline)

            '1st row creation
            Dim customerDetailsRow1 As New PdfPTable(8)
            Dim CustomerAddress1 As New PdfPCell(NormalText("ADDRESS 1 :"))
            CustomerAddress1.VerticalAlignment = 2
            CustomerAddress1.HorizontalAlignment = 0 ' //0=Left, 1=Centre, 2=Right
            CustomerAddress1.Colspan = 4
            customerDetailsRow1.ExtendLastRow.ToString()

            Dim CustomerGstNo As New PdfPCell(NormalText("GST NO :"))
            CustomerGstNo.HorizontalAlignment = 0 ' //0=Left, 1=Centre, 2=Right
            CustomerGstNo.Colspan = 4


            customerDetailsRow1.AddCell(CustomerAddress1)
            customerDetailsRow1.AddCell(CustomerGstNo)
            pdfDoc.Add(customerDetailsRow1)

            '2nd row creation
            Dim customerDetailsrow2 As New PdfPTable(8)
            Dim CustomerAddress2 As New PdfPCell(NormalText("ADDRESS 2 :"))
            CustomerAddress2.HorizontalAlignment = 0 ' //0=Left, 1=Centre, 2=Right
            CustomerAddress2.Colspan = 4

            Dim CustomerPan As New PdfPCell(NormalText("PAN NO :"))
            CustomerPan.HorizontalAlignment = 0 ' //0=Left, 1=Centre, 2=Right
            CustomerPan.Colspan = 4

            customerDetailsrow2.AddCell(CustomerAddress2)
            customerDetailsrow2.AddCell(CustomerPan)

            pdfDoc.Add(customerDetailsrow2)

            '3RD ROW CREATION
            Dim customerDetailsrow3 As New PdfPTable(8)

            Dim CustomerAddress3 As New PdfPCell(NormalText("ADDRESS 3 :"))
            CustomerAddress3.HorizontalAlignment = 0 ' //0=Left, 1=Centre, 2=Right
            CustomerAddress3.Colspan = 4

            Dim vendaorCode As New PdfPCell(NormalText("VENDOR CODE :"))
            vendaorCode.HorizontalAlignment = 0 ' //0=Left, 1=Centre, 2=Right
            vendaorCode.Colspan = 4

            customerDetailsrow3.AddCell(CustomerAddress3)
            customerDetailsrow3.AddCell(vendaorCode)

            pdfDoc.Add(customerDetailsrow3)

            '4TH ROW CREATION
            Dim customerDetailsrow4 As New PdfPTable(8)

            Dim CustomerADDempty1 As New PdfPCell(NormalText(""))
            CustomerADDempty1.HorizontalAlignment = 0 ' //0=Left, 1=Centre, 2=Right
            CustomerADDempty1.Colspan = 4

            Dim contractno As New PdfPCell(NormalText("dummy NO :"))
            contractno.HorizontalAlignment = 0 ' //0=Left, 1=Centre, 2=Right
            contractno.Colspan = 4

            customerDetailsrow4.AddCell(CustomerADDempty1)
            customerDetailsrow4.AddCell(contractno)

            pdfDoc.Add(customerDetailsrow4)

            '5TH ROW CREATION
            Dim customerDetailsrow5 As New PdfPTable(4)

            Dim CustomerADDempty2 As New PdfPCell(NormalText(""))
            CustomerADDempty2.HorizontalAlignment = 0 ' //0=Left, 1=Centre, 2=Right
            CustomerADDempty2.Colspan = 4

            Dim invoiceNo As New PdfPCell(NormalText("INVOICE NO :"))
            invoiceNo.HorizontalAlignment = 0 ' //0=Left, 1=Centre, 2=Right
            invoiceNo.Colspan = 4


            customerDetailsrow5.AddCell(CustomerADDempty2)
            customerDetailsrow5.AddCell(invoiceNo)

            pdfDoc.Add(customerDetailsrow5)

            '6TH ROW CREATION



            'footer





            pdfDoc.Add(New Phrase(address1.Text.Trim()))
            pdfDoc.Add(New Phrase(TextBox1.Text.Trim()))

            pdfDoc.Add(New Paragraph(address1.Text.Trim()))
            pdfDoc.Add(New Phrase(TextBox2.Text.Trim()))

            pdfDoc.Add(pdfTable)

            pdfDoc.Close()
            stream.Close()
            MessageBox.Show("PDF Flie Saved")

        End Using
    End Sub

    Private Sub dataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles dataGridView1.CellContentClick

    End Sub

    Private Function NormalText(value As String) As Phrase
        Return New Phrase(value, FontFactory.GetFont(FontFactory.TIMES_ROMAN, 10))
    End Function
    Private Function FormatHeader1(value As String) As Phrase
        Return New Phrase(value, FontFactory.GetFont(FontFactory.TIMES_BOLD, 15))
    End Function
    Private Function FormatHeader2(value As String) As Phrase
        Return New Phrase(value, FontFactory.GetFont(FontFactory.TIMES_BOLD, 13))
    End Function

    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged

    End Sub
End Class


Public Class itextEvents
    Implements IPdfPageEvent

    Public Sub OnChapter(writer As PdfWriter, document As Document, paragraphPosition As Single, title As Paragraph) Implements IPdfPageEvent.OnChapter
        ''Throw New NotImplementedException()
    End Sub

    Public Sub OnChapterEnd(writer As PdfWriter, document As Document, paragraphPosition As Single) Implements IPdfPageEvent.OnChapterEnd
        ' 'Throw New NotImplementedException()
    End Sub

    Public Sub OnCloseDocument(writer As PdfWriter, document As Document) Implements IPdfPageEvent.OnCloseDocument
        ' 'Throw New NotImplementedException()
    End Sub

    Public Sub OnEndPage(writer As PdfWriter, document As Document) Implements IPdfPageEvent.OnEndPage
        ''Throw New NotImplementedException()
        Dim pdfEndOfPageTable As New PdfPTable(2)
        Dim Customerfooter As New PdfPCell(NormalText("footer content bottom"))
        Customerfooter.HorizontalAlignment = 1 ' //0=Left, 1=Centre, 2=Right
        Customerfooter.Colspan = 2
        pdfEndOfPageTable.AddCell(Customerfooter)
        'document.Add(pdfEndOfPageTable)

    End Sub

    Public Sub OnGenericTag(writer As PdfWriter, document As Document, rect As Rectangle, text As String) Implements IPdfPageEvent.OnGenericTag
        ''Throw New NotImplementedException()
    End Sub

    Public Sub OnOpenDocument(writer As PdfWriter, document As Document) Implements IPdfPageEvent.OnOpenDocument
        ''Throw New NotImplementedException()
    End Sub

    Public Sub OnParagraph(writer As PdfWriter, document As Document, paragraphPosition As Single) Implements IPdfPageEvent.OnParagraph
        'Throw New NotImplementedException()
    End Sub

    Public Sub OnParagraphEnd(writer As PdfWriter, document As Document, paragraphPosition As Single) Implements IPdfPageEvent.OnParagraphEnd
        'Throw New NotImplementedException()
    End Sub

    Public Sub OnSection(writer As PdfWriter, document As Document, paragraphPosition As Single, depth As Integer, title As Paragraph) Implements IPdfPageEvent.OnSection
        'Throw New NotImplementedException()
    End Sub

    Public Sub OnSectionEnd(writer As PdfWriter, document As Document, paragraphPosition As Single) Implements IPdfPageEvent.OnSectionEnd
        'Throw New NotImplementedException()
    End Sub

    Public Sub OnStartPage(writer As PdfWriter, document As Document) Implements IPdfPageEvent.OnStartPage
        'Throw New NotImplementedException()
    End Sub
    Private Function NormalText(value As String) As Phrase
        Return New Phrase(value, FontFactory.GetFont(FontFactory.TIMES_ROMAN, 10))
    End Function
End Class


What I have tried:

please help me out,i try many.
Posted
Updated 6-Feb-18 22:53pm
v2

1 solution

this may help

Create/Read Advance PDF Report using iTextSharp in C# .NET: Part I[^]

I see this code to add header and footer
doc.AddTitle("Hello World example");
doc.AddSubject("This is an Example 4 of Chapter 1 of Book 'iText in Action'");
doc.AddKeywords("Metadata, iTextSharp 5.4.4, Chapter 1, Tutorial");
doc.AddCreator("iTextSharp 5.4.4");
doc.AddAuthor("Debopam Pal");
doc.AddHeader("Nothing", "No Header");


read this as well

Creating PDF documents with iTextSharp[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900