Add watermarks to PDF documents

Adding watermark to a particular page 

GroupDocs.Watermark API allows you to add watermark to a particular page of a PDF document. Adding watermark to a PDF document using GroupDocs.Watermark consists of following steps.

  1. Load the document 
  2. Create and initialize watermark object 
  3. Set watermark properties 
  4. Add watermark to the page of the document using property PageIndex of PdfArtifactWatermarkOptions
  5. Save the document

Following code performs this functionality.

AdvancedUsage.AddingWatermarks.AddWatermarksToPdf.PdfAddWatermarks

PdfLoadOptions loadOptions = new PdfLoadOptions();
// Constants.InDocumentPdf is an absolute or relative path to your document. Ex: @"C:\Docs\document.pdf"
using (Watermarker watermarker = new Watermarker(Constants.InDocumentPdf, loadOptions))
{
    // Add text watermark to the first page
    TextWatermark textWatermark = new TextWatermark("This is a test watermark", new Font("Arial", 8));
    PdfArtifactWatermarkOptions textWatermarkOptions = new PdfArtifactWatermarkOptions();
    textWatermarkOptions.PageIndex = 0;

    watermarker.Add(textWatermark, textWatermarkOptions);

    // Add image watermark to the second page
    using (ImageWatermark imageWatermark = new ImageWatermark(Constants.ProtectJpg))
    {
        PdfArtifactWatermarkOptions imageWatermarkOptions = new PdfArtifactWatermarkOptions();
        imageWatermarkOptions.PageIndex = 1;
        watermarker.Add(imageWatermark, imageWatermarkOptions);
    }

    watermarker.Save(Constants.OutDocumentPdf);
}

Adding watermark to all images of a particular page

The API also allows you to add watermark to the images inside a particular page of the PDF document. Following code snippet shows how to add watermark to all images in a particular page of a PDF document.

AdvancedUsage.AddingWatermarks.AddWatermarksToPdf.PdfAddWatermarkToImages

PdfLoadOptions loadOptions = new PdfLoadOptions();
// Constants.InDocumentPdf is an absolute or relative path to your document. Ex: @"C:\Docs\document.pdf"
using (Watermarker watermarker = new Watermarker(Constants.InDocumentPdf, loadOptions))
{
    // Initialize image or text watermark
    TextWatermark watermark = new TextWatermark("Protected image", new Font("Arial", 8));
    watermark.HorizontalAlignment = HorizontalAlignment.Center;
    watermark.VerticalAlignment = VerticalAlignment.Center;
    watermark.RotateAngle = 45;
    watermark.SizingType = SizingType.ScaleToParentDimensions;
    watermark.ScaleFactor = 1;

    PdfContent pdfContent = watermarker.GetContent<PdfContent>();

    // Get all images from the first page
    WatermarkableImageCollection images = pdfContent.Pages[0].FindImages();

    // Add watermark to all found images
    foreach (WatermarkableImage image in images)
    {
        image.Add(watermark);
    }

    watermarker.Save(Constants.OutDocumentPdf);
}

Getting page size 

If for some reasons you want to use absolute sizing and positioning, you may also need to determine page size. GroupDocs.Watermark also provides the feature to get the dimensions of the page in a PDF document. Below example shows how to get width and height of a particular page.

AdvancedUsage.AddingWatermarks.AddWatermarksToPdf.PdfGetDimensions

PdfLoadOptions loadOptions = new PdfLoadOptions();
// Constants.InDocumentPdf is an absolute or relative path to your document. Ex: @"C:\Docs\document.pdf"
using (Watermarker watermarker = new Watermarker(Constants.InDocumentPdf, loadOptions))
{
    PdfContent pdfContent = watermarker.GetContent<PdfContent>();
    Console.WriteLine(pdfContent.Pages[0].Width);
    Console.WriteLine(pdfContent.Pages[0].Height);
}

Page margins in PDF document

If a PDF document contains a crop box definition, Adobe Acrobat uses it for screen display and printing. That’s why GroupDocs.Watermark uses crop box by default to calculate relative watermark size and position. When you set Watermark.ConsiderParentMargins property to true, trim box rectangle is used instead. But what if you want to get watermark aligned to bleed box or art box? In this case, you need to set the appropriate value to PdfContent.PageMarginType property as shown in the below example. AdvancedUsage.AddingWatermarks.AddWatermarksToPdf.PdfAddWatermarkWithPageMarginType

PdfLoadOptions loadOptions = new PdfLoadOptions();
// Constants.InDocumentPdf is an absolute or relative path to your document. Ex: @"C:\Docs\document.pdf"
using (Watermarker watermarker = new Watermarker(Constants.InDocumentPdf, loadOptions))
{
    TextWatermark watermark = new TextWatermark("Test watermark", new Font("Arial", 42));
    watermark.HorizontalAlignment = HorizontalAlignment.Right;
    watermark.VerticalAlignment = VerticalAlignment.Top;
    watermark.SizingType = SizingType.ScaleToParentDimensions;
    watermark.ScaleFactor = 1;

    PdfContent pdfContent = watermarker.GetContent<PdfContent>();
    pdfContent.PageMarginType = PdfPageMarginType.BleedBox;
    watermark.ConsiderParentMargins = true;

    watermarker.Add(watermark);
    watermarker.Save(Constants.OutDocumentPdf);
}

Add watermark to all attachments

GroupDocs.Watermark also provides the feature to add watermark to supported files in all attachments in a PDF document. Below example shows how to add watermark to all supported attachments.

AdvancedUsage.AddingWatermarks.AddWatermarksToPdf.PdfAddWatermarkToAllAttachments

TextWatermark watermark = new TextWatermark("This is WaterMark on Attachment", new Font("Arial", 19));
PdfLoadOptions loadOptions = new PdfLoadOptions();
// Constants.InDocumentPdf is an absolute or relative path to your document. Ex: @"C:\Docs\document.pdf"
using (Watermarker watermarker = new Watermarker(Constants.InDocumentPdf, loadOptions))
{
    PdfContent pdfContent = watermarker.GetContent<PdfContent>();
    foreach (PdfAttachment attachment in pdfContent.Attachments)
    {
        // Check if the attached file is supported by GroupDocs.Watermark
        IDocumentInfo info = attachment.GetDocumentInfo();
        if (info.FileType != FileType.Unknown && !info.IsEncrypted)
        {
            // Load the attached document
            using (Watermarker attachedWatermarker = attachment.CreateWatermarker())
            {
                // Add wateramrk
                attachedWatermarker.Add(watermark);

                // Save changes in the attached file
                attachedWatermarker.Save();
            }
        }
    }

    watermarker.Save(Constants.OutDocumentPdf);
}

Advanced use cases

More resources

GitHub examples

You may easily run the code above and see the feature in action in our GitHub examples:

Free online document watermarking App

Along with full featured .NET library we provide simple, but powerful free Apps.

You are welcome to add watermark to PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX, Emails and more with our free online Free Online Document Watermarking App.