C# PDF Watermark Library
How to add, remove text, image watermark to existing pdf file using c# .net
An excellent PDF library allows C# developers to add or delete watermark to PDF File in C#.NET without itextsharp
In this C# tutorial, you will learn how to use C# code to add, insert, remove text or image watermarks on PDF pages in your .NET Windows and ASP.NET MVC web applications.
- Add, insert, remove text, image watermark to PDF pages
- Apply watermark from another PDF
- Add the same watermark to every page of the PDF file, or use different watermarks to specific PDF pages
- Adjust the placement, text style, rotate, opacity of watermarks on PDF
- Support .NET Core, .NET Framework on ASP.NET, MVC, WinForms application. No need itextsharp.
How to add, remove text, image watermarks on PDF pages using C#
- 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:
- Text message: the text watermark message
- Text font style: the text font style
- Text color: the text font color
- Text alignment: the text watermark alignment
- IsAbovePage: the text watermark will be displayed above pdf page content or below the content
- Rotate: text rotation value
- Opacity: the text transparency
- PageRangeOptions: list of pages where the watermark will be applied.
Image watermark supports the following property settings:
- Image file resource: the Bitmap object includes watermark image data
- IsAbovePage: the image watermark will be displayed above pdf page content or below the content
- Opacity: the image transparency
- 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.
- Create a PDFDocument object from a PDF file
- Create a PDFWatermarkTextRes object with text content, font family, colors specified, which will be added to PDF document as text watermark.
- Create a PDFPageWatermark object from the previously generated PDFWatermarkTextRes object
- Create a PageRangeOptions object with customized watermark added PDF pages range.
- Call PDFPageFieldHandler.ApplyWatermark() to add watermark to PDF specified page range.
- Save modified PDF document with text watermark inserted to a new file
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 text and C# example source code shows how to add an image watermark to every pages in a PDF document.
- Create a new PDFDocument object from an existing PDF file
- Create a PDFWatermarkImageRes object from an existing image file, which will be added to PDF document as watermark.
- Create a PDFPageWatermark object from the PDFWatermarkImageRes object
- Apply the following watermark settings to PDFPageWatermark object: place the watermark below PDF page content, and the opacity of the image watermark.
- Create a PageRangeOptions object with customized watermark added PDF pages range.
- Call PDFPageFieldHandler.ApplyWatermark() to add watermark to PDF specified page range.
- Save PDF document with image watermark inserted to a new file
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);
Add watermark to PDF page header and footer using C#
Sometimes you need add watermark to PDF special page area, such as page header and footers in your C# ASP.NET, MVC web and Windows application. Using XDoc.PDF C# library,
you could easily add, insert watermarks to PDF page header and footers.
Please view the step by step C# source code at
How to add image in PDF header and footer using C# code
C# remove all page watermark settings in a PDF document object
The following C# example source code and text shows how to remove, delete all existing watermarks from a PDF document.
- Create a PDFDocument object from an existing PDF file
- Utilize PDFPageFieldHandler.RemoveWatermarks() to remove the watermark data from the PDF object
- Save the watermark removed PDF object to a new PDF file
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);
}