Hi
I can add watermark text on a .pdf document to an absolute position.
This time I want to add water text of small font size to cover all of the page including margins.
I know that I need relative positioning of text, fitting text to text lines depending on pagesize (A4 paper)
and some padding between textlines.
Can some one help with a suitable method?
Sample is attached
Thanks

    Private Sub btnCreate_Click(sender As System.Object, e As System.EventArgs) Handles btnCreate.Click
            Dim doc As New Document(iTextSharp.text.PageSize.A4, 20, 20, 20, 20)
            Dim wri As PdfWriter = PdfWriter.GetInstance(doc, New FileStream(Application.StartupPath + "\AttendanceSheet.pdf", FileMode.Create))
            doc.Open()
            Dim font11Bold As New iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 11.0F, iTextSharp.text.Font.BOLD, BaseColor.BLACK)
            Dim font11BoldU As New iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 11.0F, iTextSharp.text.Font.BOLD Or iTextSharp.text.Font.UNDERLINE, BaseColor.BLACK)
            Dim font10Bold As New iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 10.0F, iTextSharp.text.Font.BOLD, BaseColor.BLACK)
            Dim phrase As Phrase = Nothing
            Dim cell As PdfPCell = Nothing


            Dim pdfHeaderTable As New PdfPTable(3)
            pdfHeaderTable.WidthPercentage = 100

            pdfHeaderTable.HorizontalAlignment = 1
            Dim wid(2) As Single
            wid(0) = 100
            wid(1) = 100
            wid(2) = 100
            pdfHeaderTable.SetWidths(wid)

            Phrase = New Phrase()
            phrase.Add(New Chunk("blah Blah" & vbLf & "------------" & vbLf, font11Bold))
            cell = PhraseCell(Phrase, PdfPCell.ALIGN_CENTER)
            cell.NoWrap = False
            pdfHeaderTable.AddCell(cell)

            phrase = New Phrase()
            phrase.Add(New Chunk("blah Blah" & vbLf & "------------" & vbLf, font11Bold))
            cell = PhraseCell(phrase, PdfPCell.ALIGN_CENTER)
            cell.NoWrap = False
            pdfHeaderTable.AddCell(cell)

            phrase = New Phrase()
            phrase.Add(New Chunk("blah Blah" & vbLf & "------------" & vbLf, font11Bold))
            cell = PhraseCell(phrase, PdfPCell.ALIGN_CENTER)
            cell.NoWrap = False
            pdfHeaderTable.AddCell(cell)

            doc.Add(pdfHeaderTable)
            doc.Close()

            AddWatermark()

            Process.Start(Application.StartupPath + "\AttendanceSheet1.pdf")

        End Sub



          Public Sub AddWatermark()
                Dim bytes As Byte() = file.ReadAllBytes(Application.StartupPath + "\AttendanceSheet.pdf")
                'Dim blackFont As Font = FontFactory.GetFont("Arial", 12, Font.NORMAL, BaseColor.BLACK)
                Using stream As New MemoryStream()
                    Dim reader As New PdfReader(bytes)
                    Using stamper As New PdfStamper(reader, stream)
                        Dim pages As Integer = reader.NumberOfPages
                        For i As Integer = 1 To pages
                            '    ColumnText.ShowTextAligned(stamper.GetUnderContent(i), Element.ALIGN_RIGHT, New Phrase(i.ToString(), blackFont), 568.0F, 15.0F, 0)
                            'Next
                            Dim pageRectangle As iTextSharp.text.Rectangle = reader.GetPageSize(i)
                            Dim pdfData As PdfContentByte = stamper.GetUnderContent(i)

                            'Create Font and Size for the Watermark
                            pdfData.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 10)

                            'Set the Watermark, Get the Width of a Single Watermark element and finally Get the Height of the Font being used
                            Dim myWatermark As String = "Some Watermark"
                            Dim watermarkList As New List(Of String)
                            Dim singleWaterMarkWidth As Single = pdfData.GetEffectiveStringWidth(myWatermark, False)
                            Dim fontHeight As Single = 10
                            'Dim fontHeight As Single = New Font("HELVETICA", 10, FontStyle.Bold).Height

                            'Work out the Watermark for a Single Line on the Page based on the Page Width
                            Dim currentWaterMarkWidth As Single
                            While currentWaterMarkWidth + singleWaterMarkWidth < pageRectangle.Width
                                watermarkList.Add(myWatermark)
                                currentWaterMarkWidth = pdfData.GetEffectiveStringWidth(String.Join(" "c, watermarkList), False)
                            End While

                            'Fill the Page with Lines of Watermarks
                            Dim currentYPos As Single = pageRectangle.Height
                            While currentYPos > 0
                                pdfData.ShowTextAligned(iTextSharp.text.Element.ALIGN_CENTER, String.Join(" "c, watermarkList), pageRectangle.Width / 2, currentYPos, 0)
                                currentYPos -= fontHeight
                            End While
                        Next
                    End Using
                    bytes = stream.ToArray()
                End Using
                file.WriteAllBytes(Application.StartupPath + "\AttendanceSheet1.pdf", bytes)

            End Sub

        Public Function PhraseCell(phrase As Phrase, align As Integer) As PdfPCell
                Dim cell As New PdfPCell(phrase)
                cell.BorderColor = BaseColor.WHITE
                cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE
                cell.HorizontalAlignment = align
                cell.PaddingBottom = 0.0F
                cell.PaddingTop = 0.0F
                Return cell
            End Function

The following sample demonstrates how to add lines of text watermarks to PDF which might helps, but this sample did not use itext, it's based on another free .net pdf lib. You can try to specify the location as per your needs. it's in C#, but you can convert it to vb. Check:

https://code.msdn.microsoft.com/Add-a-text-watermark-on-e56799cf

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.