Add Watermark to Multipage File - C# .NET 6

This tutorial shows how to add a watermark to each page of a multipage file in a C# .NET 6 Console application using the LEADTOOLS SDK.

Overview  
Summary This tutorial covers how to burn a watermark to a multipage file in a C# .NET 6 Console application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (1 KB)
Platform C# .NET 6 Console Application
IDE Visual Studio 2022
Runtime Target .NET 6 or Higher
Development License Download LEADTOOLS

Required Knowledge

Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial, before working on this 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>\LEADTOOLS23\Bin\net:

If using DLLs, the System.Drawing.Common NuGet package is also needed.

For a complete list of which DLL files are required for your application, refer to Files to be Included in your Application.

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:

C#
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.

C#
static void AddWatermarktoFile() 
{ 
   string _sourceFile = @"C:\LEADTOOLS23\Resources\Images\Leadtools.pdf"; 
 
   using AnnBatesStampComposer batesStampComposer = new(); 
   AnnBatesStampComposer.RenderingEngine = new AnnWinFormsRenderingEngine(); 
   AnnContainer batesStampContainer = new(); 
 
   AnnBatesStamp batesStamp = new() 
   { 
      Font = new AnnFont("Arial", 12), 
      Foreground = AnnSolidColorBrush.Create("Red"), 
      HorizontalAlignment = AnnHorizontalAlignment.Center, 
      VerticalAlignment = AnnVerticalAlignment.Center 
   }; 
   batesStamp.Logo.Angle = 45; 
   batesStamp.Logo.Font = new AnnFont("Arial", 32); 
   batesStamp.Logo.Opacity = .25; 
   batesStamp.Logo.StretchLogo = true; 
   batesStamp.Logo.Text = "Watermark"; 
 
   using RasterCodecs _codecs = new(); 
   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)); 
   batesStampComposer.Stamps.Add(batesStamp); 
 
   batesStampComposer.TargetContainers.Add(batesStampContainer); 
   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:\LEADTOOLS23\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:

C#
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:\LEADTOOLS23\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:\LEADTOOLS23\Resources\Images\Leadtools.pdf

Image showing watermark on first page

Wrap-up

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 23.0.2024.5.22
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.

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