Hello, world!

In this article, you will learn how to create a simple C# application that appends a text watermark using the GroupDocs.Watermark API.

Note
We assume that you already have a basic knowledge of Microsoft Visual Studio and C#.

Requirements

  • A compatible system with Microsoft Visual Studio installed. As an individual developer, you can use a free Visual Studio Community Edition.
  • 5 minutes of your spare time.

Preparing

  1. Open Visual Studio and go to File -> New -> Project.
  2. Select the Console App project type.
  3. Install GroupDocs.Watermark NuGet package to the project.

Coding

  1. Create an instance of the Watermarker class for the local document. You can specify an absolute or relative path:
    Watermarker watermarker = new Watermarker("C:\\Docs\\sample.docx")
    
  2. Create an instance of the TextWatermark class and specify the desired text and font for the watermark:
    TextWatermark watermark = new TextWatermark("Hello, world!", new Font("Arial", 36));
    
  3. Apply the watermark:
    watermarker.Add(watermark);
    
  4. Save the resulting document:
    watermarker.Save("C:\\Docs\\watermarked-sample.docx");
    

Full code:

using GroupDocs.Watermark;
using GroupDocs.Watermark.Watermarks;

// Specify an absolute or relative path to your document.
using (Watermarker watermarker = new Watermarker("C:\\Docs\\sample.docx"))
{
   // Specify the desired text and font for the watermark
   TextWatermark watermark = new TextWatermark("Test watermark", 
      new Font("Arial", 36, FontStyle.Bold | FontStyle.Italic));
      
   watermark.HorizontalAlignment = HorizontalAlignment.Center;
   watermark.VerticalAlignment = VerticalAlignment.Center;

   watermark.Opacity = 0.4;
   watermark.RotateAngle = 45;
   watermark.ForegroundColor = Color.Red;

    // Apply the watermark
    watermarker.Add(watermark);
    // Save the resulting document
    watermarker.Save("C:\\Docs\\watermarked-sample.docx");
}

Additionally, if you are certain about your file extension or know the document type in advance, you can specify the FileType through the LoadOptions class. Specifying it eliminates the need for format detection, enabling faster and more efficient document processing:


var filePath = "C:\\Docs\\sample.docx";
var loadOptions = new LoadOptions()
{
    FileType = FileType.FromExtension(Path.GetExtension(filePath))
};

// Or set the FormatFamily property directly when using a stream, for example:
loadOptions.FormatFamily = FormatFamily.WordProcessing;

using (var watermarker = new Watermarker(filePath, loadOptions))
{ .... }

Running

Run the program. A new watermarked document will appear in the specified path.

“Hello, world!” example

What’s next

Congratulations! You have added a simple text watermark to a document. Read the Developer guide and API reference to learn how to customize text watermarks, add images as watermarks, search documents for existing watermarks, and much more.

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.