SoFunction
Updated on 2025-03-07

C# Add watermark to PDF files

This article shares the specific code for adding PDF file watermark in C# for your reference. The specific content is as follows

using System;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;

// version:5.1.10

protected void Button1_Click(object sender, EventArgs e)
  {
  string source =@"D:\\C#NET\Exoport2PDF\Web2\"; //Template path  string output = @"D:\\C#NET\Exoport2PDF\Web2\"; // PDF after exporting the watermark background  string watermark = @"D:\\C#NET\Exoport2PDF\Web2\"; // Watermark picture
  bool isSurrcess = PDFWatermark(source, output, watermark, 10, 10);
  
  }


 public bool PDFWatermark(string inputfilepath, string outputfilepath, string ModelPicName, float top, float left)

 {
  //throw new NotImplementedException();
  PdfReader pdfReader = null;
  PdfStamper pdfStamper = null;
  try
  {
   pdfReader = new PdfReader(inputfilepath);

   int numberOfPages = ;

    psize = (1);

   float width = ;

   float height = ;

   pdfStamper = new PdfStamper(pdfReader, new FileStream(outputfilepath, ));

   PdfContentByte waterMarkContent;

    image = (ModelPicName);

    = 20;//Transparency, grey fill   ////Rotation   ////Rotation angle   //The location of the watermark   if (left < 0)
   {
    left = width -  + left;
   }

   (left, (height - ) - top);


   // Add watermark to each page, or you can also set a watermark to add a certain page.   for (int i = 1; i <= numberOfPages; i++)
   {
    waterMarkContent = (i);

    (image);
   }
   //strMsg = "success";
   return true;
  }
  catch (Exception ex)
  {
    ();
   return false;
  }
  finally
  {

   if (pdfStamper != null)
    ();

   if (pdfReader != null)
    ();
  }

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.