C# code tutorial

PDF Creating & Inserting PDF to Image PDF Digital Signature PDF Extracting PDF Processing & Editing PDF Protecting PDF Chart PDF Barcode

Popular .NET SDK


As we all know, PDF format is widely used at high levels of authority. In general and commercial level, PDF always contains confidential material. In order to prevent PDF content from theft, change, or misuse, adding a watermark to PDF document is widely applied and regarded as a good way to protect your intellectual property. Once a watermark is stamped, it signifies a lot of things on a PDF document for individual and e-commerce purposes, well protected.

XsPDF provides effective .NET APIs for adding image watermark and text watermark in C# projects, including ASP.NET project. Watermark added can be customized, like setting font style, color, location, rotation, etc. Moreover, you can create multiple watermarks and add them to different sections and locations of the same PDF page.

Please see C# examples below and learn how to add watermarks on PDF document.

C# example for adding image watermark to PDF page
//Load PDF document
PdfDocument document = new PdfDocument("sample.pdf");

//Load watermark image from local file
string watermarkFile = "watermark.png";
var watermarkImg = XImage.FromFile(watermarkFile);

//Add watermark to first page
var page = document.Pages[0];
//The new content will be rendered above the existing graphic.
var gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Append);

//Customize image location
var location = new XPoint(100, 100);

//Watermark the PDF page with image
gfx.DrawImage(watermarkImg, location);
//Customize image size
//gfx.DrawImage(watermarkImg, location.X, location.Y, 50, 50);

document.Save("image-watermark.pdf");
C# example for adding text watermark to PDF page
//Load PDF document
PdfDocument document = new PdfDocument("sample.pdf");

string watermark = "watermark";

//Add watermark to first page
var page = document.Pages[0];
//The new content will be rendered above the existing graphic.
var gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Append);

// Set text font, location and rotation
var textFont = new XFont("Times New Roman", 30f, XFontStyle.Regular);
var location = new XPoint(50, 100);
int rotation = -45;

// Create a red brush with transparency
var textColor = XColor.FromArgb(128, 255, 0, 0);
var brush = new XSolidBrush(textColor);

// Set a text format.
var format = new XStringFormat();
format.Alignment = XStringAlignment.Near;
format.LineAlignment = XLineAlignment.Near;

// Get the size (in points) of the text.
var size = gfx.MeasureString(watermark, textFont);

//Change text orientation from left-bottom to top-right          
gfx.TranslateTransform((location.X + size.Width) / 2, (location.Y + size.Height) / 2);
gfx.RotateTransform(rotation);
gfx.TranslateTransform(-(location.X + size.Width) / 2, -(location.Y + size.Height) / 2);

//Draw watermark to page
gfx.DrawString(watermark, textFont, brush, location, format);

// PDF 1.4+ support transparency.
if (document.Version < 14)
    document.Version = 14;

document.Save("text-watermark.pdf");

Please note, you may do as follows to create and add watermarks to PDF in ASP.NET project.

  • For adding image watermark: copy the C# code above to the webform "Page_Load" methed or any other customized methods in the web handler or service Class.
  • For adding text watermark: copy the C# code above to the webform initialization or any other response methods to implement text watermark feature.

More Excel tutorial