How to Start Convert PDF Read PDF Build PDF Work with PDF Modules PDF Document PDF Pages Text Image Graph & Path Annotation, Markup & Drawing Redaction Security Digital Signature Forms Watermark Bookmark Link File Attachment File Metadata Printing Work with Other SDKs Barcode read Barcode create OCR Twain

C# PDF Watermark Component
How to add text, image watermark to existing pdf file using c# .net


An Excellent PDF Control Allows C# Developers to add or delete watermark to PDF File in C#.NET









  • Professional PDF SDK for Visual Studio .NET, which able to add watermark in C#.NET class
  • Advanced PDF edit control and component for modify watermark in both C#.NET WinForms
  • Free online sample code for quick evaluation in Visual C#.NET framework for PDF watermark
  • Support .NET WinForms, ASP.NET MVC in IIS, ASP.NET Ajax, Azure cloud service, DNN (DotNetNuke), SharePoint
  • Easy to add/remove watermark to/from PDF page online in browser in ASP.NET web project
  • Support to copy a PDF watermark to another PDF file page in .NET framework
  • Support to add different watermark to PDF file.




A watermark is text or an image that is displayed either in front of or behind existing PDF document content. For example, you could create and add a "Confidential" text watermark to PDF pages with sensitive information. You can add multiple watermarks to a PDF file, but you must add each watermark separately. You can specify the page or range of pages on which each watermark appears.





Watermark property settings


Text watermark supports the following property settings:


  1. Text message: the text watermark message
  2. Text font style: the text font style
  3. Text color: the text font color
  4. Text alignment: the text watermark alignment
  5. IsAbovePage: the text watermark will be displayed above pdf page content or below the content
  6. Rotate: text rotation value
  7. Opacity: the text transparency
  8. PageRangeOptions: list of pages where the watermark will be applied.




Image watermark supports the following property settings:


  1. Image file resource: the Bitmap object includes watermark image data
  2. IsAbovePage: the image watermark will be displayed above pdf page content or below the content
  3. Opacity: the image transparency
  4. PageRangeOptions: list of pages where the watermark will be applied.






C# add watermark to PDF document



The following content will explain how to add text watermark, and image watermark to pdf document with property settings.









Add text watermark to a PDF document object using C#


The following C# example source code shows how to add two text watermarks to a PDF document. One watermark is added to each odd pages, and the other watermark is added to each even pages.



String inputFilePath = Program.RootPath + "\\" + "1.pdf";
String outputFilePath = Program.RootPath + "\\" + "1_watermk.pdf";

//  open a PDF file
PDFDocument doc = new PDFDocument(inputFilePath);

PDFWatermarkTextRes resWatermark = new PDFWatermarkTextRes("Confidential", new Font("Arial", 72F, FontStyle.Regular), Color.Black, WatermarkTextAlignment.Center);

{
    //  define a watermark setting
    PDFPageWatermark watermark1 = new PDFPageWatermark(resWatermark);
    watermark1.IsAbovePage = false;
    watermark1.Rotate = -45F;
    watermark1.Opacity = 0.2F;

    //  define page range: all odd pages
    PageRangeOptions pageRange1 = new PageRangeOptions();
    pageRange1.AllPages = true;
    pageRange1.Subset = PageRangeSubset.Odd;

    //  apply watermark settings to all odd pages
    PDFPageFieldHandler.ApplyWatermark(doc, watermark1, pageRange1);
}
{
    //  define a watermark setting
    PDFPageWatermark watermark2 = new PDFPageWatermark(resWatermark);
    watermark2.IsAbovePage = false;
    watermark2.Rotate = 45F;
    watermark2.Opacity = 0.2F;

    //  define page range: all even pages
    PageRangeOptions pageRange2 = new PageRangeOptions();
    pageRange2.AllPages = true;
    pageRange2.Subset = PageRangeSubset.Even;

    //  apply watermark settings to all even pages
    PDFPageFieldHandler.ApplyWatermark(doc, watermark2, pageRange2);
}

doc.Save(outputFilePath);






Add image watermark to a PDF document object using C#


The following C# example source code shows how to add an image watermark to every pages in a PDF document.



String inputFilePath = Program.RootPath + "\\" + "1.pdf";
String outputFilePath = Program.RootPath + "\\" + "1_watermk.pdf";
Bitmap resImage = Program.RootPath + "\\" + "watermark.png";

//  Open file
PDFDocument doc = new PDFDocument(inputFilePath);

PDFWatermarkImageRes resWatermark = new PDFWatermarkImageRes(resImage);

//  Define a watermark setting
PDFPageWatermark watermark = new PDFPageWatermark(resWatermark);
//  Set watermark behind the page content.
watermark.IsAbovePage = false;
//  Set transparancy.
watermark.Opacity = 0.2F;

//  Define page range: all pages
PageRangeOptions pageRange = new PageRangeOptions();
pageRange.AllPages = true;
pageRange.Subset = PageRangeSubset.All;

//  Apply watermark settings to all odd pages
PDFPageFieldHandler.ApplyWatermark(doc, watermark, pageRange);

//  Save file
doc.Save(outputFilePath);






C# remove all page watermark settings in a PDF document object


The following C# example source code shows how to remove, delete all existing watermarks from a PDF document.



String inputFilePath = Program.RootPath + "\\" + "1_watermk.pdf";
String outputFilePath = Program.RootPath + "\\" + "output.pdf";

PDFDocument doc = new PDFDocument(inputFilePath);
PDFPageFieldHandler.RemoveWatermarks(doc);
doc.Save(outputFilePath);




C# read, extract all page watermark settings from a PDF file


The following C# example source code shows how to read, display watermark properties.



String inputFilePath = Program.RootPath + "\\" + "1_watermk.pdf";

//  Open file
PDFDocument doc = new PDFDocument(inputFilePath);

//  Get all Watermark settings in the document
PDFPageWatermarkInfo info = PDFPageFieldHandler.RetreiveWatermarks(doc);

//  Get default setting for Watermark and Page Range
PDFPageWatermark defaultSetting = info.GetDefaultSetting();
PageRangeOptions defaultPageRange = info.GetDefaultPageRangeSettings();

//  Number of watermark settings in the document.
Console.WriteLine("Watermark: " + info.Count);
//  Show default watermark setting
Console.WriteLine("Is above page:         {0}", defaultSetting.IsAbovePage);
Console.WriteLine("Opacity:               {0}", defaultSetting.Opacity);
Console.WriteLine("Enable absolute scale: {0}", defaultSetting.EnableAbsoluteScale);
//  Show all watermark settings in the document.
foreach (PDFPageWatermark setting in info.GetAllSettings())
{
    Console.WriteLine("Watermark Setting");
    Console.WriteLine("Is above page:         {0}", setting.IsAbovePage);
    Console.WriteLine("Rotate (in degree):    {0}", setting.Rotate);
    Console.WriteLine("Opacity:               {0}", setting.Opacity);
    Console.WriteLine("Enable absolute scale: {0}", setting.EnableAbsoluteScale);
    Console.WriteLine("Scale:                 {0}", setting.Scale);
    Console.WriteLine("Alignment");
    Console.WriteLine("Horizontal:            {0}; Margin: {1}", setting.HoriAlignment, setting.HoriMargin);
    Console.WriteLine("Vertical:              {0}; Margin: {1}", setting.VertAlignment, setting.VertMargin);
    PageRangeOptions pageRange = info.GetPageRangeOptions(setting);
    Console.WriteLine("Page Range Settings");
    Console.WriteLine("Is all page:           {0}", pageRange.AllPages);
    Console.WriteLine("Subset type:           {0}", pageRange.Subset.ToString());
    Console.WriteLine("Min. Page Number:      {0}", pageRange.MinPageNum);
    Console.WriteLine("Max. Page Number:      {0}", pageRange.MaxPageNum);
}