Monday 16 June 2014

How To Add Circular Text Like a stamp in a Pdf Document


Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Imports System.Web.Hosting
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports System.Drawing

 Public Sub Maincall()

        Dim doc As New Document()
        Dim filePath As String = "Path where pdf will store"
        Dim wri As PdfWriter = PdfWriter.GetInstance(doc, New FileStream(filePath & "\TEST.PDF", FileMode.Create))
        doc.Open()
        Dim cb As PdfContentByte = wri.DirectContent
        Dim fontSize As Single = 18
        Dim xPosition As Integer = CInt(wri.PageSize.Width) \ 2
        Dim yPosition As Integer = CInt(wri.PageSize.Height) \ 2
        Dim radius As Double = 100
        Dim angle As Single = 180
        Dim baseFont__1 As BaseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED)
        Dim testString As String = "Hello to the world of text in a Circle."
        Dim CircleCenter As New Point(xPosition, yPosition)
        For i As Integer = 0 To testString.Length - 1


            Dim p As Point = CalculatePointOnCircle(CircleCenter, radius, CInt(Math.Truncate(angle)))

            Dim textAngle As Single = (-90 + CSng(angle) + 180 * ((2 * xPosition \ yPosition) \ testString.Length) * i / CSng(Math.PI))



            cb.BeginText()
            cb.SetFontAndSize(baseFont__1, fontSize)

            cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, testString(i).ToString(), CSng(p.X), CSng(p.Y), textAngle)

            cb.EndText()

            angle -= fontSize - CSng(fontSize / 1.8)
        Next



        doc.NewPage()

        doc.Close()

    End Sub

    Private Shared Function CalculatePointOnCircle(center As Point, radius As Double, rotationAngle As Double) As Point


        Dim angleRadians As Double = rotationAngle * Math.PI / 180

        Dim x As Double = center.X + radius * Math.Cos(angleRadians)

        Dim y As Double = center.Y + radius * Math.Sin(angleRadians)



        Return New Point(CInt(Math.Truncate(x)), CInt(Math.Truncate(y)))

    End Function