How to Draw Text Watermark in PDF – VB.NET

Generally speaking, watermark can be text or image. Before, I introduce a method to add image watermark in PDF with C#. And in this post, I will show how to insert text watermark in PDF with VB.Net.

As image watermark, text watermark can protect authors’ copyright. Besides, text watermark can tell others properties of the document. For example, company name will be taken as text watermark to insert in materials.

In my example, I prepare a document about Shelley. I will insert “Shelley Biography” as watermark in it. Because I use a .NET PDF component, Spire.PDF for .NET to realize this function, I add its dll file as reference in project.

The following steps show details.

  1. Load file we want to insert watermark from computer.
  2. Draw text watermark in every page of this document. When drawing, we need to set format for text watermark, including brush width & height, transparency, rotation angle. Then, drawing text, including text font style, font color, location and alignment. Finally, draw rectangle which saves text watermark.
  3. Save and launch file.

Main Coding:

Imports System.Drawing
Imports Spire.Pdf
Imports Spire.Pdf.Graphics

Module Text
    Sub Main()
        ‘Load
        Dim doc As New PdfDocument()
        doc.LoadFromFile(“D:\work\My Documents\shelley.pdf”)

        ‘Draw Watermark
        Dim page As PdfPageBase = doc.Pages(0)
        For Each page In doc.Pages
            Dim brush As New PdfTilingBrush(New SizeF(page.Canvas.ClientSize.Width \ 2, page.Canvas.ClientSize.Height \ 2))
            brush.Graphics.SetTransparency(0.6F)
            brush.Graphics.Save()
            brush.Graphics.TranslateTransform(brush.Size.Width \ 2, brush.Size.Height \ 2)
            brush.Graphics.RotateTransform(-35)
            brush.Graphics.DrawString(“Shelley Biography”, New PdfFont(PdfFontFamily.TimesRoman, 22), _
                                      PdfBrushes.LimeGreen, 0, 0, New PdfStringFormat(PdfTextAlignment.Center))
            brush.Graphics.Restore()
            page.Canvas.DrawRectangle(brush, New RectangleF(New PointF(0, 0), page.Canvas.ClientSize))
        Next

        ‘Save and Launch
        doc.SaveToFile(“TextWaterMark.pdf”)
        doc.Close()
        Process.Start(“TextWaterMark.pdf”)
    End Sub
End Module

Result Shown by Following:

Download Spire.PDF for .NET Here

Related Post:

Add Image Watermark in PDF Document

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s