This tutorial shows how to create a C# Windows Console application that adds a watermark to each page of a multi-page file using the LEADTOOLS SDK.
Overview | |
---|---|
Summary | This tutorial covers how to burn a watermark to a multi-page file in a Console C# Application. |
Completion Time | 30 minutes |
Visual Studio Project | Download tutorial project (3 KB) |
Platform | C# Windows Console Application |
IDE | Visual Studio 2017, 2019 |
Development License | Download LEADTOOLS |
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.
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:
Leadtools.Annotations.WinForms
If local DLL references are used, the following DLLs are needed. The DLLs are located at <INSTALL_DIR>\LEADTOOLS21\Bin\Dotnet4\x64
:
Leadtools.dll
Leadtools.Annotations.BatesStamp.dll
Leadtools.Annotations.Engine.dll
Leadtools.Annotations.Rendering.WinForms.dll
Leadtools.Codecs.dll
Leadtools.Codecs.Cmp
Leadtools.Codecs.Tif
Leadtools.Pdf.dll
For a complete list of which Codec DLLs are required for specific formats, refer to File Format Support.
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:
Note
For details on how to properly add LEADTOOLS NuGet and local references, refer to the Add References and Set a License tutorial.
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 multi-page file.
static void AddWatermarktoFile()
{
string _sourceFile = @"C:\LEADTOOLS21\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));
batesStampComposer.Stamps.Add(batesStamp);
//Apply Bates Stamp to our container
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:\Temp\watermarkedImage.tif", RasterImageFormat.Tif, _image.BitsPerPixel, 1,-1,1,CodecsSavePageMode.Append);
}
}
}
}
}
}
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:\LEADTOOLS21\Resources\Images\leadtools.pdf
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.