DEV Community

Usman Aziz
Usman Aziz

Posted on • Updated on • Originally published at blog.groupdocs.com

Insert watermark in multiple documents and images in C# or Java

Watermarking is a popular technique to indicate that the content in a document is confidential or information/data the document contains is copyrighted to the creator. Similarly, in many instances, the watermarked documents are shared among all the stakeholders and partners within a company. However, it is quite hard to manually apply the watermark to a bunch of documents one by one. So in order to make this process easier, we need some automation.

In this article, I will show you how you can apply the watermark to a batch of files including multiple documents or images in one go with the minimum efforts.

Many software components are available that allow you to add watermark. I am going to use GroupDocs.Watermark API which is available for both .NET and Java platforms. The plus point of using this API is that you can control the appearance and placement of the watermark using a variety of options. So let’s begin.

Steps

You need to follow the below steps to apply the watermark to a document:

  • Load the document
  • Create and initialize watermark object
  • Set watermark properties such as position, size, opacity, font, etc.
  • Add watermark to the document
  • Save the document

This is how we would transform these steps into the code.

Code

  • C#
DirectoryInfo dir = new DirectoryInfo(@"../../Documents/");
FileInfo[] files = dir.GetFiles();
// Iterate through the files
foreach (FileInfo file in files)
{
    // Load document
    using (Document doc = Document.Load(file.FullName))
    {
        // Initialize the font to be used for watermark
        Font font = new Font("Calibre", 50, FontStyle.Bold | FontStyle.Italic);
        // Create watermark
        TextWatermark watermark = new TextWatermark("Protected", font);
        // Set watermark properties
        watermark.ForegroundColor = Color.Red; 
        watermark.TextAlignment = TextAlignment.Right;
        watermark.Opacity = 0.5;
        watermark.HorizontalAlignment = HorizontalAlignment.Center;
        watermark.VerticalAlignment = VerticalAlignment.Center;
        watermark.RotateAngle = -45;
        // Apply watermark
        doc.AddWatermark(watermark);
        // Save document
        doc.Save(Path.Combine("../../Output",file.Name));
    }
}
Enter fullscreen mode Exit fullscreen mode
  • Java
 // Get files in the Documents folder
File folder = new File("./Documents/");
File[] listOfFiles = folder.listFiles();

// Iterate through the files
for (int i = 0; i < listOfFiles.length; i++) {
    if (listOfFiles[i].isFile()) {
        Document doc = Document.load(listOfFiles[i].getPath());
        // Create watermark
        Font font = new Font("Calibre", 50, FontStyle.Bold | FontStyle.Italic);
        TextWatermark watermark = new TextWatermark("Protected", font);
        // Set watermark properties
        watermark.setForegroundColor(Color.getRed());
        watermark.setTextAlignment(TextAlignment.Right);
        watermark.setOpacity(0.5);
        watermark.setHorizontalAlignment(HorizontalAlignment.Center);
        watermark.setVerticalAlignment(VerticalAlignment.Center);
        watermark.setRotateAngle(-45);
        // Apply watermark to the document
        doc.addWatermark(watermark);
        // Save document
        doc.save("./Output/" + listOfFiles[i].getName());

        doc.close();
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

watermark

Source Code

Download the complete ready-to-run source code from GitHub.

Cheers!

 

Top comments (1)

Collapse
 
sarmad99 profile image
sarmad99

Hi nice article. Can you do it for mvc 5 ?