Watermarks

With PDF .Net, you can add a watermark to PDF from your C# or VB.NET application.

The following example shows how you can easily add a watermark to an existing PDF file.

Complete code

using System;
using System.IO;
using SautinSoft;
using SautinSoft.Pdf;
using SautinSoft.Pdf.Content;

namespace Sample
{
    class Sample
    {
        /// <summary>
        /// Watermarks.
        /// </summary>
        /// <remarks>
        /// Details: http://sautinsoft/products/pdf/help/net/developer-guide/watermarks.php
        /// </remarks>
        static void Main(string[] args)
        {
            string pdfFile = Path.GetFullPath(@"..\..\..\simple text.pdf");

            using (var document = PdfDocument.Load(pdfFile))
            {
                // Load the watermark from a file.
                var image = PdfImage.Load(@"..\..\..\WatermarkImage.png");

                foreach (var page in document.Pages)
                {
                    // Make sure the watermark is correctly transformed even if
                    // the page has a custom crop box origin, is rotated, or has custom units.
                    var transform = page.Transform;
                    transform.Invert();

                    // Center the watermark on the page.
                    var pageSize = page.Size;
                    transform.Translate((pageSize.Width - 1) / 2, (pageSize.Height - 1) / 2);

                    // Calculate the scaling factor so that the watermark fits the page.
                    var cropBox = page.CropBox;
                    var scale = Math.Min(cropBox.Width, cropBox.Height);

                    // Scale the watermark so that it fits the page.
                    transform.Scale(scale, scale, 0.5, 0.5);

                    // Draw the centered and scaled watermark.
                    page.Content.DrawImage(image, transform);
                }

                document.Save("Watermark Images.pdf");
            }
        }
    }
}

Download


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:



Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.