Free Support Forum - aspose.com

Add Watermark to PDF in C#

It’s a common practice to add watermarks to PDF files in order to make sure that the original file is intact and has not been compromised. A watermark can be easily added to a PDF file programmatically in C# using Aspose.PDF for .NET. In this topic, we’ll see how we can add image or text watermark to a PDF in .NET.

Steps to Add Watermark to PDF in C#

  1. Create a Visual Studio Project using C# template
  2. Install Aspose.PDF for .NET latest NuGet package
  3. Set license for Aspose.PDF for .NET. A temporary license can be availed for testing
  4. Create a Document object to hold the PDF file
  5. Load the PDF which is to be watermarked to the Document object
  6. Create a Stamp object with required attributes
    6.1. Create Image Stamp to add image watermark
    6.2. Create Text Stamp to add text watermark
  7. Add Stamp object to a particular Page of the PDF document
  8. Save the output PDF with the watermark(s) on it

Code to Add Text Watermark to PDF in C#

//set Aspose.PDF for .NET API license
Aspose.Pdf.License license = new Aspose.Pdf.License();
license.SetLicense(@"C:\licenses\license.lic");

//load a PDF document into Aspose.PDF Document object
Aspose.Pdf.Document document = new Aspose.Pdf.Document(@"C:\files\sample.pdf");

//create a text stamp object to setup text watermark
Aspose.Pdf.TextStamp textStamp = new Aspose.Pdf.TextStamp("Text Stamp");
        
//specify location for the text stamp as text watermark
textStamp.XIndent = 100;
textStamp.YIndent = 100;

//set text attributes of the text watermark
textStamp.TextState.Font = Aspose.Pdf.Text.FontRepository.FindFont("Calibri");
textStamp.TextState.FontStyle = Aspose.Pdf.Text.FontStyles.Bold;
textStamp.TextState.FontSize = 24.0F;
textStamp.TextState.ForegroundColor = Aspose.Pdf.Color.Red;
textStamp.TextState.BackgroundColor = Aspose.Pdf.Color.Silver; 

//add text stamp as watermark on a particular page of your choice
document.Pages[1].AddStamp(textStamp);

//save the output file having the text watermark on it
document.Save(@"C:\files\output.pdf");

Code to Add Image Watermark to PDF in C#

 //set Aspose.PDF for .NET API license
Aspose.Pdf.License license = new Aspose.Pdf.License();
 license.SetLicense(@"C:\licenses\license.lic");

//load a PDF document into Aspose.PDF Document object
Aspose.Pdf.Document document = new Aspose.Pdf.Document(@"C:\files\sample.pdf");

//create an image stamp object to setup image watermark
Aspose.Pdf.ImageStamp imgStamp = new Aspose.Pdf.ImageStamp(@"C:\files   
 \image_stamp.png");

//specify location for the image stamp as image watermark
imgStamp.XIndent = 100;
imgStamp.YIndent = 100;

//set attributes of the image watermark 
imgStamp.Height = 50;
imgStamp.Width = 50;
imgStamp.Background = false; 

//add image stamp as watermark on a particular page of your choice
document.Pages[1].AddStamp(imgStamp);

//save the output file having the image watermark on it
document.Save(@"C:\files\output.pdf");

The above steps and code samples show you how you can easily add text or image watermarks to PDF file in your .NET applications. If you want to quickly see how this works practically then you can check Aspose.PDF Online App to add Watermark to PDF. The source code of the Watermark PDF file App is also available.

If you’re interested to know more about PDF Watermark feature of Aspose.PDF, or have any questions about processing PDF files programmatically then feel free to contact our support team in our free Aspose.PDF support forums.