All Categories - easy coding
To include automated mail sending in your code, simply import System.Net and System.Net.Mail in your code:

Imports System.Net
Imports System.Net.Mail
and on your calling function simply make a call to SmtpClient.Send()

Dim yourMail As SmtpClient = New SmtpClient("")

SmtpClient.Send() have two overloaded function calls

1. SmtpClient.Send(message as System.Net.MailMessage)
2. SmtpClient.Send(from as String, recipients as String, subject as String, Body as String)

To customize your message you may use System.Net.Mail.MailMessage(). Example:

            Dim theMessage As MailMessage = New MailMessage

            theMessage.From = New MailAddress("")
            theMessage.Subject = "The Subject"
            theMessage.Body = "The body bla bla bla."

To send message as html just assign MailMessage.isBodyHtml to true:

            theMessage.IsBodyHtml = True 
            theMessage.Body = "<HTML><HEAD>Subject</HEAD>" + _
                                            "<BODY>your body bla bla bla</BODY></HTML>"

            Dim theMail As SmtpClient = New SmtpClient("yourmailserver.local.domain")

            theMail.Credentials = New NetworkCredential("username", "password")
We can use iTextSharp's IPdfPageEvent to display page numbers on your generated PDF file without manually computing the number of pages (or manually rendering some page number headers using GDI). In your class just add a code to import iTextSharp.text.pdf and make sure your class implements IPdfPageEVent.

Imports iTextSharp.text
Imports iTextSharp.text.pdf

Public Class IPDFEvents
     Implements IPdfPageEvent

This automatically adds default methods from the implement, but following are the most useful ones:

1. OnCloseDocument()
2. OnEndPage()
3. OnOpenDocument()
4. OnStartPage()
5. yourHeaderSetterFunction()
6. orYourFooterSetterFunction()

How it works?  First off, let's define your header and footer setters:

Private Sub yourHeaderSetterFunction(ByVal oWriter As iTextSharp.text.pdf.PdfWriter, ByVal oDocument As Document)
    Dim oTable As New PdfPTable(1)
        Dim oCell As PdfPCell
        With oTable
            '---Column 1:  some title
            oCell = New PdfPCell( _
                            New iTextSharp.text.Phrase( _
                                    rptHeader, _
                                    FontFactory.GetFont("Arial", 8, iTextSharp.text.Font.BOLD)))
            oCell.Border = 0
            oCell.BackgroundColor = dColor
            oCell.HorizontalAlignment = Element.ALIGN_LEFT

            '---Column 2: PageNumber
            Dim iPageNumber As Integer = oWriter.PageNumber
            Dim sText As String = "Page " & iPageNumber & " of "

            oTable.TotalWidth = oDocument.PageSize.Width - 70
            oTable.WriteSelectedRows(0, -1, 36, oDocument.PageSize.Height - 15, oWriter.DirectContent)

            Dim fLen As Single = moBF.GetWidthPoint(sText, 10)
            moCB.SetFontAndSize(moBF, 8)
            moCB.SetTextMatrix(oDocument.PageSize.Width - 113, oDocument.PageSize.Height - 25)

            moCB.AddTemplate(moTemplate, oDocument.PageSize.Width - 120 + fLen, oDocument.PageSize.Height - 25)
            moCB.SetFontAndSize(moBF, 14)
            moCB.SetTextMatrix(300, 300)
        End With
    End Sub

    Private Sub YourFooterSetterFunctionr(  ByVal oWriter As iTextSharp.text.pdf.PdfWriter,  ByVal oDocument As iTextSharp.text.Document)
            Dim fLen As Single

          Dim sText As String = "Your footer text"
        Dim oCell As New PdfPCell
        fLen = moBF.GetWidthPoint(sText, sText.Length())
        moCB.SetFontAndSize(moBF, 8)
        moCB.SetTextMatrix((oDocument.PageSize.Width - fLen) + (oDocument.PageSize.Width / 3) + _
                                (oDocument.PageSize.Width / 3), 26)
     End Sub

Then we need to declare these variables inside our class:

 Private moTemplate As PdfTemplate
    Private moCB As PdfContentByte
    Private moBF As BaseFont = Nothing

    Dim dColor As iTextSharp.text.Color = New iTextSharp.text.Color(0, 255, 255)
    Dim dFont As iTextSharp.text.Font = New iTextSharp.text.Font(iTextSharp.text.Font.TIMES_ROMAN, 8, iTextSharp.text.Font.BOLD)

And then issue calls to the necessary function derivatives such that:

Public Sub OnCloseDocument(ByVal writer As iTextSharp.text.pdf.PdfWriter, ByVal document As iTextSharp.text.Document) Implements iTextSharp.text.pdf.IPdfPageEvent.OnCloseDocument
        moTemplate.SetFontAndSize(moBF, 8)
        moTemplate.ShowText((writer.PageNumber - 1).ToString)
    End Sub

    Public Sub OnEndPage(ByVal writer As iTextSharp.text.pdf.PdfWriter, ByVal document As iTextSharp.text.Document) Implements iTextSharp.text.pdf.IPdfPageEvent.OnEndPage
        yourHeaderSetterFunction(writer, document)
        yourFooterSetterFunction(writer, document)
    End Sub

 Public Sub OnOpenDocument(ByVal writer As iTextSharp.text.pdf.PdfWriter, ByVal document As iTextSharp.text.Document) Implements iTextSharp.text.pdf.IPdfPageEvent.OnOpenDocument
            moBF = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED)
            moCB = writer.DirectContent
            moTemplate = moCB.CreateTemplate(50, 50)

        Catch de As DocumentException
        Catch ioe As IOException
        End Try
    End Sub

    Public Sub OnStartPage(ByVal writer As iTextSharp.text.pdf.PdfWriter, ByVal document As iTextSharp.text.Document) Implements iTextSharp.text.pdf.IPdfPageEvent.OnStartPage
        pagecount = pagecount + 1
    End Sub

andn viola! page number is displayed after implementing your class.

    About the author

    The author have 12+ years experience in IT Industry with varying job roles from system developer/analyst/consultant positions. Majority of her development, implementation and systems support background focus on Financial/Banking/Credit Solutions.


    October 2010


    VB .Net

    RSS Feed