Recursive AI

Add a watermark to a PDF in C#

All onepdf recipes

Open the PDF with editing allowed, call AddTextStamp and save. The program below stamps a diagonal “DRAFT” on every page of the IRS Form W-9, a public PDF you can download to try it.

Terminal
dotnet new console -o Watermark && cd Watermark
dotnet add package RecursiveAI.OnePdf --version 1.14.0
curl -LO https://www.irs.gov/pub/irs-pdf/fw9.pdf

Put this in Program.cs and run dotnet run:

C#
using OnePdf;
 
using PdfDocument doc = PdfDocument.Open("fw9.pdf",
    new PdfOpenOptions { AllowEditing = true });
 
// "DRAFT" across every page: 45 degrees, red, 15% opaque, centered on each page.
PdfCreatedFont font = doc.AddStandardFont("Helvetica-Bold");
doc.AddTextStamp("DRAFT", font, 120, new PdfStampOptions
{
    Opacity = 0.15,
    RotationDegrees = 45,
    Color = (0.7, 0.1, 0.1),
});
 
doc.Save("fw9-draft.pdf");
Console.WriteLine($"Watermarked {doc.PageCount} pages: fw9-draft.pdf");
Text
Watermarked 6 pages: fw9-draft.pdf

Each of the six pages now shows “DRAFT” in light red across its middle, over the text. The stamp is drawn once into a shared form and placed on every page, so it is stored once however many pages show it. The pages’ own content is left exactly as it was.

An image watermark, behind the content

To use a logo instead, add the image to the document and stamp it. Underlay puts it under the page’s text, so the form stays readable:

Terminal
curl -o logo.png https://recursiveai.org/assets/brand/icon-512.png
C#
using OnePdf;
 
using PdfDocument doc = PdfDocument.Open("fw9.pdf",
    new PdfOpenOptions { AllowEditing = true });
 
// transparency is kept
PdfCreatedImage logo = doc.AddPngImage(File.ReadAllBytes("logo.png"));
doc.AddImageStamp(logo, 300, 300, new PdfStampOptions   // 300 x 300 points
{
    Opacity = 0.1,
    Placement = PdfContentPlacement.Underlay,   // under the page's content
}, pageIndexes: [0]);   // page 1 only; leave out for all pages
 
doc.Save("fw9-logo.pdf");
Console.WriteLine("Saved fw9-logo.pdf");
Text
Saved fw9-logo.pdf

Options

PdfStampOptions sets Opacity (0 to 1), RotationDegrees (counterclockwise), Color (RGB, 0 to 1), Placement (Overlay, the default, or Underlay), a BlendMode, and CenterX and CenterY to move the stamp off the page center. Sizes and positions are in points, 72 to the inch. Stamps stay upright on pages that are rotated.

Limits

  • A watermark can be removed. It is ordinary page content: a PDF editor can delete it, and text extraction returns “DRAFT” with the rest of the text. To take information out of a file, redact it.
  • Standard fonts aren’t embedded. Helvetica is supplied by the viewer, which keeps the file small but rules out PDF/A. Embed your own TrueType or OpenType font with doc.AddFont(File.ReadAllBytes("font.ttf")) instead.
  • Stamp before you sign. Stamping a signed PDF changes its pages, and signature verification reports the change.
  • JPEG and PNG only. AddJpegImage embeds a JPEG as it is and AddPngImage keeps a PNG’s transparency. Convert other formats first.

To place an image at an exact spot on one page, see Add an image to a PDF. Every drawing call is in Content creation.