Add Watermark to Multipage File - Console C#

This tutorial shows how to create a C# Windows Console application that adds a watermark to each page of a multipage file using the LEADTOOLS SDK.

Summary This tutorial covers how to burn a watermark to a multipage file in a Console C# Application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (4 KB)
Platform C# Windows Console Application
IDE Visual Studio 2017, 2019
Development License Download LEADTOOLS

Required Knowledge

Before working on the Add Watermark to Multipage File - Console C# tutorial, get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial.

Create the Project and Add LEADTOOLS References

Start with a copy of the project created in the Add References and Set a License tutorial. If you do not have that project, follow the steps in that tutorial to create it.

The references needed depend upon the purpose of the project. References can be added by one or the other of the following two methods (but not both). For this project, the following references are needed:

If NuGet references are used, this tutorial requires the following NuGet package:

If local DLL references are used, the following DLLs are needed. The DLLs are located at <INSTALL_DIR>\LEADTOOLS22\Bin\Dotnet4\x64:

For a complete list of which Codec DLLs are required for specific formats, refer to File Format Support.

Set the License File

The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details including tutorials for different platforms, refer to Setting a Runtime License.

There are two types of runtime licenses:

Add the Watermark Code

With the project created, the references added, and the license set, coding can begin.

In the Solution Explorer, open Program.cs. Add a new method called AddWatermarktoFile() and call it inside the Main method.

Add the following statements to the using block at the top of Program.cs:

// Using block at the top 
using System; 
using System.IO; 
using Leadtools; 
using Leadtools.Annotations.BatesStamp; 
using Leadtools.Annotations.Engine; 
using Leadtools.Annotations.Rendering; 
using Leadtools.Codecs; 

Add the code below to the newly created method to add a watermark to each page of a multipage file.

static void AddWatermarktoFile() 
   string _sourceFile = @"C:\LEADTOOLS22\Resources\Images\Leadtools.pdf"; 
   //Create AnnBatesStampComposer instance and add the created Bates stamp to it 
   using (AnnBatesStampComposer batesStampComposer = new AnnBatesStampComposer()) 
      //Set the rendering engine 
      AnnBatesStampComposer.RenderingEngine = new AnnWinFormsRenderingEngine(); 
      //Create Bates stamp container, set its size and mapper and attach it to the composer 
      AnnContainer batesStampContainer = new AnnContainer(); 
      //Create AnnBatesStamp and set its properties 
      AnnBatesStamp batesStamp = new AnnBatesStamp(); 
      batesStamp.Font = new AnnFont("Arial", 12); 
      batesStamp.Foreground = AnnSolidColorBrush.Create("Red"); 
      batesStamp.HorizontalAlignment = AnnHorizontalAlignment.Center; 
      batesStamp.VerticalAlignment = AnnVerticalAlignment.Center; 
      batesStamp.Logo.Angle = 45; 
      batesStamp.Logo.Font = new AnnFont("Arial", 32); 
      batesStamp.Logo.Opacity = 0.25; 
      batesStamp.Logo.StretchLogo = true; 
      batesStamp.Logo.Text = "Watermark"; 
      using (RasterCodecs _codecs = new RasterCodecs()) 
         using (CodecsImageInfo info = _codecs.GetInformation(_sourceFile, true)) 
            batesStampContainer.Mapper.MapResolutions(info.XResolution, info.YResolution, info.XResolution, info.YResolution); 
            batesStampContainer.Size = batesStampContainer.Mapper.SizeToContainerCoordinates(LeadSizeD.Create(info.Width, info.Height)); 
            //Apply Bates Stamp to our container 
            int pages = _codecs.GetTotalPages(_sourceFile); 
            for (int i = 1; i <= pages; i++) 
               using (RasterImage _image = _codecs.Load(_sourceFile, 24, CodecsLoadByteOrder.BgrOrGray, i, i)) 
                  AnnBatesStampComposer.RenderingEngine.RenderOnImage(batesStampContainer, _image); 
                  _codecs.Save(_image, @"C:\LEADTOOLS22\Resources\Images\watermarkedImage.tif", RasterImageFormat.Tif, _image.BitsPerPixel, 1,-1,1,CodecsSavePageMode.Append); 

Handling Streams

To load the file using MemoryStream, replace the code in the for loop with the following:

byte[] bytes = File.ReadAllBytes(_sourceFile); 
using (MemoryStream ms = new MemoryStream(bytes)) 
    using (RasterImage _image = _codecs.Load(ms)) 
        AnnBatesStampComposer.RenderingEngine.RenderOnImage(batesStampContainer, _image); 
        _codecs.Save(_image, @"C:\LEADTOOLS22\Resources\Images\watermarkedImage.tif", RasterImageFormat.Tif, _image.BitsPerPixel, 1, -1, 1, CodecsSavePageMode.Append); 

Run the Project

Run the project by pressing F5, or by selecting Debug -> Start Debugging.

If the steps were followed correctly, the application burns a custom watermark to each page of the multipage file. For the purposes of this tutorial, the PDF located in the following file path was used: C:\LEADTOOLS22\Resources\Images\Leadtools.pdf

Image showing watermark on first page


This tutorial showed how to add the necessary references to add a watermark to each page of a multipage file. Also, we covered how to work with the AnnContainer, AnnBatesStamp, and AnnBatesStampComposer classes.

See Also

Help Version 22.0.2024.3.20
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.