To protect your files from any illegal use or to apply branding to your documents, watermarks can be used. In this article, you will learn to programmatically add the watermarks to PDF files using C#. We will separately look into adding watermark text and image watermarks.
The following topics are covered below:
.NET Watermarking API for PDF files
GroupDocs.Watermark provides .NET watermarking API that allows working with text as well as image watermarks within the PDF files. Along with the PDF files, the API allows adding, removing, and extraction of watermarks for word-processing documents, spreadsheets, presentations, email messages, images, Visio drawings, and many other formats. From the documentation, you may further check the features and supported file formats.
You can download the DLLs or MSI installer from the downloads section or install the API in your .NET application via NuGet.
PM> Install-Package GroupDocs.Watermark
Add Text Watermark to PDF using C#
The watermark text can be applied to PDF files on all the pages or any selective page. It can be added by inserting the formatted text on the required position.
The following steps show how to add watermark text to PDF files.
- Load the PDF document using Watermarker class.
- Initialize the text watermark using TextWatermark class.
- Set the appearance by adding rotation angle, alignment, opacity, foreground and background colors, etc.
- Set the targetted page index (Optional). If you do not set the index, the watermark will be applied to all pages by default.
- Add the text watermark to the loaded PDF file.
- Save the update file with watermark using the appropriate Save method.
The source code shows how to add text watermark to PDF files using C#.
The output of the above source code shows the text watermark on both the pages of the given PDF file.
Add Image Watermark to PDF using C#
Similarly, you can add images to the PDF file as we just added the text watermark.
The following steps show how to add an image to PDF files as watermarks.
- Load the PDF document using Watermarker class.
- Initialize the image watermark using ImageWatermark class.
- Set the appearance by adjusting the alignment, rotation, opacity, and other options.
- Set the targetted page index. (Optional)
- Add the image watermark to the PDF file.
- Save the watermarked file using the appropriate Save method.
The source code shows how to add image watermark to PDF files using C#.
The output of the above source code shows the image watermark on the second page of the given PDF file.
Get a Free API License
You can get a free temporary license to use the API without the evaluation limitations.
Conclusion
To conclude, you learned how to add watermarks to PDF files using C#. We have seen adding watermark text as well as images on PDF files as watermarks. For more details or learning about the API, visit documentation. For queries, contact us via the forum.