Watermark a PDF
A text watermark can identify a document as a draft or label it for a particular audience. Create a PDF containing the text “This is a text watermark.” This example follows WatermarkPdf.cs and saves the result as WatermarkPdf_output.pdf.
What you’ll create
- A single Letter-sized page with 50-point margins.
- A text watermark in 32-point Helvetica.
- A file named
WatermarkPdf_output.pdfin your chosen output directory.
Example code
Add the following class to a console project that references the DynamicPDF Flow API. See the quick start for an overview. The class creates a page, adds a text watermark, and saves the document to an existing output directory. No resource files are required.
using System;
using System.IO;
using DynamicPDF.Flow;
namespace FluentExamples.Examples;
internal static class WatermarkPdf
{
public static void Run(string outputRoot)
{
var path = Path.Combine(
outputRoot, "WatermarkPdf_output.pdf");
DocumentBuilder.Create()
.Pages(page => page
.Size(PageSize.Letter)
.Margins(50, LengthUnit.Point)
.Watermark("This is a text watermark.",
watermark => watermark
.Font("Helvetica")
.FontSize(32)))
.Save(path);
Console.WriteLine(
$"Created {Path.GetFullPath(path)}");
}
}
Imports System
Imports System.IO
Imports DynamicPDF.Flow
Friend Module WatermarkPdf
Public Sub Run(outputRoot As String)
Dim outputPath = Path.Combine(
outputRoot, "WatermarkPdf_output.pdf")
DocumentBuilder.Create().
Pages(Sub(page) page.
Size(PageSize.Letter).
Margins(50, unit:=LengthUnit.Point).
Watermark("This is a text watermark.",
Sub(watermark) watermark.
Font("Helvetica").
FontSize(32))).
Save(outputPath)
Console.WriteLine(
$"Created {Path.GetFullPath(outputPath)}")
End Sub
End Module
How it works
1. Create the document and page
DocumentBuilder.Create() starts a new document. Pages adds a Letter-sized page with 50-point margins.
2. Add the text watermark
Watermark adds “This is a text watermark.” to the page. Its configuration callback selects Helvetica through Font("Helvetica") and sets a visible 32-point size with FontSize(32).
3. Save the PDF
Path.Combine joins outputRoot with WatermarkPdf_output.pdf. Make sure the output directory exists before calling Run. Saving overwrites an existing file with the same name.
Console.WriteLine prints the full output path.
Next steps
See WatermarkAdvanced.cs for more examples, including rotating, transparency, borders, and other formatting examples..
Change the watermark text to suit your document, or explore Add page numbers and browse all examples.