Stamp And Watermark

This example demonstrates how to draw Stamp and Watermark.

PDF file to stamp:
Watermark image file:
Watermark Text:

Often we wanted to make a watermark on a pdf document which can be easily recognises

We choose a pdf which we need to add watermark to it

Stamping text is a text which we set in textbox will appear as text in watermark

Watermark Image is the image which will appear as background image in watermark

Clicking generate button will create a pdf with above application watermark

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;

using OfficeComponent.Pdf;
using OfficeComponent.Pdf.Graphics;

namespace OfficeComponent.Samples
{
    class StampAndWatermarkExample : ExampleBase
#if WEB
, IUIExample
#endif
    {
        public string SourcePdf { get; set; }
        public string WatermarkImagePath { get; set; }
        public string WatermarkText { get; set; }

        public StampAndWatermarkExample()
            : base(null, null)
        {

        }

        public StampAndWatermarkExample(string commonDataPath, string outputDir)
            : base(commonDataPath, outputDir)
        {

        }

        public StampAndWatermarkExample(string commonDataPath, string outputDir, string xmlFile) : base(commonDataPath, outputDir, xmlFile)
        {

        }

        public override string Execute()
        {
#if WEB
            ProcessForm();
#endif
            if (string.IsNullOrWhiteSpace(SourcePdf))
            {
                ShowError("Please specify the source PDF file.");
                return null;
            }

            if (string.IsNullOrWhiteSpace(WatermarkText))
            {
                ShowError("Please specify the watermark text.");
                return null;
            }

            // Create a new instance of PdfDocument class.
            PdfDocument doc = new PdfDocument();
            using (PdfImportedDocument importedDoc = new PdfImportedDocument(SourcePdf))
            {
                PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 36f);

                foreach (PdfPageBase lPage in importedDoc.Pages)
                {
                    PdfGraphics g = lPage.Graphics;
                    PdfGraphicsState state = g.Save();

                    g.SetTransparency(0.25f);
                    g.RotateTransform(-40);
                    g.DrawString(WatermarkText, font, PdfPens.Red, PdfBrushes.Red, new PointF(-200, 450));

                    if (!string.IsNullOrWhiteSpace(WatermarkImagePath))
                    {
                        PdfImage image = new PdfBitmap(WatermarkImagePath);
                        g.Restore(state);
                        g.SetTransparency(0.25f);
                        g.DrawImage(image, 0, 0, lPage.Graphics.ClientSize.Width, lPage.Graphics.ClientSize.Height);
                    }
                }

                var outputPath = Path.Combine(OutputDir, this.GetType().Name + "_" + Guid.NewGuid().ToString() + ".pdf");
                importedDoc.Save(outputPath);
                importedDoc.Close();
                return outputPath;
            }
        }

        public override string ActionTitle
        {
            get { return "Stamp Document"; }
        }

#if WEB
        void ProcessForm()
        {
            SourcePdf = GetPostFile("SourcePdf");
            WatermarkImagePath = GetPostFile("ImageFile");
        }
#endif
    }
}
Imports System.Drawing.Imaging
Imports System.IO
Imports System.Text

Imports OfficeComponent.Pdf
Imports OfficeComponent.Pdf.Graphics

Namespace OfficeComponent.Samples
#If WEB Then
	Friend Class StampAndWatermarkExample
		Inherits ExampleBase
		Implements IUIExample
#Else
	Friend Class StampAndWatermarkExample
		Inherits ExampleBase
#End If
		Private privateSourcePdf As String
		Public Property SourcePdf() As String
			Get
				Return privateSourcePdf
			End Get
			Set(ByVal value As String)
				privateSourcePdf = value
			End Set
		End Property
		Private privateWatermarkImagePath As String
		Public Property WatermarkImagePath() As String
			Get
				Return privateWatermarkImagePath
			End Get
			Set(ByVal value As String)
				privateWatermarkImagePath = value
			End Set
		End Property
		Private privateWatermarkText As String
		Public Property WatermarkText() As String
			Get
				Return privateWatermarkText
			End Get
			Set(ByVal value As String)
				privateWatermarkText = value
			End Set
		End Property

		Public Sub New()
			MyBase.New(Nothing, Nothing)

		End Sub

		Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String)
			MyBase.New(commonDataPath, outputDir)

		End Sub

		Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String, ByVal xmlFile As String)
			MyBase.New(commonDataPath, outputDir, xmlFile)

		End Sub

		Public Overrides Function Execute() As String
#If WEB Then
			ProcessForm()
#End If
			If String.IsNullOrWhiteSpace(SourcePdf) Then
				ShowError("Please specify the source PDF file.")
				Return Nothing
			End If

			If String.IsNullOrWhiteSpace(WatermarkText) Then
				ShowError("Please specify the watermark text.")
				Return Nothing
			End If

			' Create a new instance of PdfDocument class.
			Dim doc As New PdfDocument()
			Using importedDoc As New PdfImportedDocument(SourcePdf)
				Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 36F)

				For Each lPage As PdfPageBase In importedDoc.Pages
					Dim g As PdfGraphics = lPage.Graphics
					Dim state As PdfGraphicsState = g.Save()

					g.SetTransparency(0.25F)
					g.RotateTransform(-40)
					g.DrawString(WatermarkText, font, PdfPens.Red, PdfBrushes.Red, New PointF(-200, 450))

					If Not String.IsNullOrWhiteSpace(WatermarkImagePath) Then
						Dim image As PdfImage = New PdfBitmap(WatermarkImagePath)
						g.Restore(state)
						g.SetTransparency(0.25F)
						g.DrawImage(image, 0, 0, lPage.Graphics.ClientSize.Width, lPage.Graphics.ClientSize.Height)
					End If
				Next lPage

				Dim outputPath = Path.Combine(OutputDir, Me.GetType().Name & "_" & Guid.NewGuid().ToString() & ".pdf")
				importedDoc.Save(outputPath)
				importedDoc.Close()
				Return outputPath
			End Using
		End Function

		Public Overrides ReadOnly Property ActionTitle() As String
			Get
				Return "Stamp Document"
			End Get
		End Property

#If WEB Then
		Private Sub ProcessForm()
			SourcePdf = GetPostFile("SourcePdf")
			WatermarkImagePath = GetPostFile("ImageFile")
		End Sub
#End If
	End Class
End Namespace