Knowledgebase (2328)
Children categories

Adding watermarks to PDF documents is a common requirement for protecting sensitive information, indicating document status, or branding files. Watermarks can enhance the security of your documents by discouraging unauthorized copying or distribution. In this article, we'll explore how to add text watermarks to PDFs in C# using Spire.PDF for .NET, a powerful library designed for PDF manipulation.
- Getting Started with Spire.PDF for .NET
- Understanding the Coordinate System
- Step-by-Step Guide: Adding a Text Watermark to PDF in C#
- Conclusion
- FAQs
Getting Started with Spire.PDF for .NET
Spire.PDF for .NET is a robust API that allows developers to create, edit, and manipulate PDF documents programmatically. It supports a wide range of features, including:
- PDF generation and conversion
- Text and image extraction
- Digital signatures
- Watermarking
Before proceeding, ensure you have installed the Spire.PDF NuGet package. You can install it via the NuGet Package Manager in Visual Studio:
Install-Package Spire.PDF
Alternatively, download it from our official website and reference the DLL file mannually.
Understanding the Coordinate System
Before writing code, it's crucial to understand how PDFs handle coordinates. In Spire.PDF:
- The origin (0, 0) is at the top-left corner of the page.
- The X-axis increases from left to right.
- The Y-axis increases from top to bottom.
- Measurements are in points (72 points = 1inch).
This coordinate system is essential when positioning watermarks, text, or images on a PDF page.

Step-by-Step Guide: Adding a Text Watermark to PDF in C#
Let’s break down the process of adding a watermark to a PDF document using Spire.PDF.
Step 1: Import Namespaces
First, include the necessary namespaces. These namespaces provide the essential classes needed for PDF manipulation and graphics rendering, enabling you to work with PDF files in your C# application.
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;
Step 2: Load the PDF Document
This step involves initializing a new PdfDocument object and loading the PDF file you want to modify, making it ready for further operations.
PdfDocument doc = new PdfDocument();
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.pdf");
Step 3: Customize a Function to Add Watermaks
Define a helper function to apply a watermark to a single page. This function is designed to position and draw the watermark text on the page, ensuring it is centered and visually appealing, while also applying transparency for a subtle effect.
private static void AddWatermark(PdfPageBase page, string watermarkText, PdfTrueTypeFont font, PdfBrush brush, float opacity)
{
page.Canvas.SetTransparency(opacity);
SizeF textSize = font.MeasureString(watermarkText);
float pageWidth = page.ActualSize.Width;
float pageHeight = page.ActualSize.Height;
float x = (pageWidth - textSize.Width) / 2;
float y = (pageHeight - textSize.Height) / 2;
page.Canvas.DrawString(watermarkText, font, brush, x, y);
}
Step 4: Apply the Watermark to All Pages
In this step, you set up the font, color, and opacity for the watermark. The loop iterates through each page in the document, applying the watermark consistently across all pages.
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial Black", 50f), true);
PdfBrush brush = PdfBrushes.Blue;
string watermarkText = "DO NOT COPY";
float opacify = 0.6f;
foreach (PdfPageBase page in doc.Pages)
{
AddWatermark(page, watermarkText, font, brush, opacify);
}
Step 5: Save the Modifyed PDF
Finally, save the document under a new filename, ensuring that your watermark is preserved in the final output.
doc.SaveToFile("Watermark.pdf");
Result:

Conclusion
Adding watermarks to PDFs in C# is straightforward with Spire.PDF for .NET . By understanding the coordinate system and using the library’s drawing capabilities, you can customize watermarks with different fonts, colors, and transparency levels. This method is useful for document security, branding, or marking drafts.
FAQs:
Q1. How do I rotate the watermark?
To add a diagonal watermark to a PDF in C#, you can rotate the coordinate system before drawing the text. Here is the code for your reference:
page.Canvas.RotateTransform(-45);
page.Canvas.DrawString(watermarkText, font, brush, x, y);
Q2. How can I adjust the watermark position?
Change the x and y values in DrawString() to reposition the watermark.
Q3. Can I add an image watermark instead of text?
Yes, you can add an image watermark instead of text. Use PdfPage.Canvas.DrawImage() for rendering the image directly on the page. Alternatively, set a background image for each page using the PdfPageBase.BackgroundImage property.
Q4: How do I add handwritten text to a PDF?
You can create an image of your handwritten text, and then draw it on each page as an image watermark.
Get a Free License
To fully experience the capabilities of Spire.PDF for .NET without any evaluation limitations, you can request a free 30-day trial license.
Pivot table is a kind of interactive table that is capable of quickly calculating, summarizing and analyzing large amounts of data. As one of the most powerful tools in Excel, it enables users to view static data from different perspectives, and also makes the comparisons between data more intuitive. In this article, you will learn how to programmatically create a pivot table in Excel using Spire.XLS for .NET.
Install Spire.XLS for .NET
To begin with, you need to add the DLL files included in the Spire.XLS for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.XLS
Create a Pivot Table in Excel
The detailed steps are as follows.
- Create a Workbook object.
- Load a sample Excel document using Workbook.LoadFromFile() method.
- Get a specified worksheet using Workbook.Worksheets[] property.
- Select the data source range using Worksheet.Range[] property, and then create a PivotCache to save the data information using Workbook.PivotCaches.Add (CellRange) method.
- Add a pivot table to the specified worksheet and set the location and cache of it using Worksheet.PivotTables.Add(String, CellRange, PivotCache) method.
- Define row labels of the pivot table and then add fields to the data area to calculate data using PivotTable.DataFields.Add(IPivotField, String, SubtotalTypes) method.
- Set the pivot table style using PivotTable.BuiltInStyle property.
- Save the result document using Workbook.SaveToFile() method.
- C#
- VB.NET
using Spire.Xls;
namespace createPivotTable
{
class Program
{
static void Main(string[] args)
{
//Create a Workbook object
Workbook workbook = new Workbook();
//Load a sample Excel document
workbook.LoadFromFile(@"E:\Files\sample.xlsx");
//Get the first worksheet
Worksheet sheet = workbook.Worksheets[0];
//Select the data source range
CellRange dataRange = sheet.Range["B1:F11"];
PivotCache cache = workbook.PivotCaches.Add(dataRange);
//Add a PivotTable to the worksheet and set the location and cache of it
PivotTable pt = sheet.PivotTables.Add("Pivot Table", sheet.Range["H3"], cache);
//Define row labels
PivotField r1 = pt.PivotFields["Country"] as PivotField;
r1.Axis = AxisTypes.Row;
pt.Options.RowHeaderCaption = "Country";
PivotField r2 = pt.PivotFields["Product"] as PivotField;
r2.Axis = AxisTypes.Row;
//Add data fields to calculate data
pt.DataFields.Add(pt.PivotFields["Quantity"], "SUM of Quantity", SubtotalTypes.Sum);
pt.DataFields.Add(pt.PivotFields["Total Amount"], "SUM of Total Amount", SubtotalTypes.Sum);
//Set pivot table style
pt.BuiltInStyle = PivotBuiltInStyles.PivotStyleMedium10;
//Save the document
workbook.SaveToFile("CreatePivotTable.xlsx", ExcelVersion.Version2010);
}
}
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
This web site is maintained by E-iceblue Ltd. By accessing or using www.e-iceblue.com, you need to agree to terms of E-iceblue Online Privacy Policy, as outlined below. If you do not agree to these terms, please do not access or use this site.
Overview
E-iceblue has established this Onine Privacy Policy to help you understand how we intend to treat your Personal Information.
E-iceblue is committed to protecting your privacy. For accessing service from E-iceblue, we may ask for personal identification information (“Personal Information”), including your name (first and last), your e-mail address or some other contact information. The personal information can be provided by yourself or any other party. In general, you may visit our website without disclosing to us who you are and without revealing any Personal Information about yourself.
Also, you should be aware though, that if you choose to provide us with Personal Information, we may transfer such information, within E-iceblue or E-iceblue’s third party service providers, across borders and from your country or jurisdiction to other countries or jurisdictions around the world, subject to the limitations of this Privacy Policy.
E-iceblue Strives to comply with all applicable laws around the globe that are designed to protect your privacy. Although legal requirements may vary from country to country, E-iceblue intends to adhere to the principles set forth in this Online Privacy Policy.
Cookies and Other Tracking Technologies
When you visit www.e-iceblue.com, you may surf the site anonymously and access important information without revealing your identity. In order to analyze and improve our site, we use “cookies” to track your visit. A cookie is small amount of data that is transferred to your browser by a Web server and can only be read by the server that give it to you. “Cookies” cannot be executed as code or deliver viruses.
Most browsers are initially set to accept cookies. You can set your browser to notify you when you receive a cookie, therefore allowing you to decide whether or not to accept it. (For some Web pages that require an authorization, cookies are not optional. Users choosing not to accept cookies will not be able to access such pages.)
While E-iceblue uses cookies to track your visit to www.e-iceblue.com, and our Web servers automatically log the IP/Internet address of your computer. This information does not identify you personally and you remain anonymous unless you have otherwise provided e-iceblue with Personal Information.
Principles
The following principles we adopted to protect your privacy.
Choice
You may choose whether or not to provide Personal Information to E-iceblue. However, when you engage in certain activities on this site, such as ordering products, downloading software, or entering contests, E-iceblue may require that you provide certain information about yourself by filling out and submitting an online form. It is entirely optional for you to engage in these activities. If you elect to engage in these activities, E-iceblue may require that you provide certain personal information, such as your name, mailing address, e-mail address, and other personal identifying information.
Security
Wherever your Personal Information may be held by E-iceblue or a third party on E-iceblue’s behalf, reasonable and appropriate precautions, such as encryption, firewalls and like technologies, are and will be implemented to protect such Personal Information from loss, misuse or unauthorized access.
Access/Accuracy
To the extent that you do provide us with Personal Information, E-iceblue wishes to maintain it accurately and updated. Where we collect Personal Information from you, our goal is to provide a means of contacting E-iceblue should you need to access, update or correct that Information. If for any reason you desire to review such information, please contact us and we will make reasonable efforts to promptly provide you with such information. Further, if you notify us that such information is incorrect, or you wish to have such information removed, we will correct, amend, or delete your Personal Information as soon as practicable.
Third Party Services
In the interest of providing better service to its customers, E-iceblue is represented in the United States and in some foreign countries by authorized distributors and resellers who provide marketing, sales, and support services to E-iceblue customers. When you submit personal information to E-iceblue, you understand and agree that E-iceblue may allow E-iceblue’s authorized distributors and resellers access to your customer profile for the exclusive purpose of providing you with marketing, sales, and support services for E-iceblue products.
Your personal information will not be leased, sold, rented or otherwise made available to any other third party except to the extent necessary to comply with applicable laws, police investigations, or in legal proceedings where such information is relevant. Occasionally, E-iceblue allows access to database information to bonded mailing houses only for the purpose of allowing for the printing of mailing labels that are used for E-iceblue 's own direct mail and promotional purposes. In those instances, the third party is strictly bound by these terms.
Notwithstanding the foregoing, you hereby agree that E-iceblue may assign, sell, license, or otherwise transfer to a third party, your name, address, email address, member name and any other personal information in connection with an assignment, sale, joint venture, or other transfer or disposition of a material portion or essentially all of the assets or stock of E-iceblue or its affiliated entities.
Children's Privacy
www.e-iceblue.com is not designed nor structured to attract children. Accordingly, we do not intend to collect Personal Information from anyone we know to be under 13 years of age. If we are made aware that information is or has been submitted by or collected from a child under the age of thirteen, we will promptly delete this information.
Sensitive Information
We do not collect Sensitive Information such as medical, health, racial, ethnic, political, religious, philosophic, union membership or sexual orientation information. If we are made aware that we have received such information, we will promptly proceed to its deletion.
Notice
E-iceblue collects Personal Information on its Web site in order to record and support your participation in the activities you select. If you order a product, for example, the information is used to register your license and rights, if any, to ensure your eligibility to receive technical support, upgrade discounts, or other benefits that may be made available to registered users. If you enter a contest, information is collected to qualify the entry and contact you regarding the contest or prize awards. E-iceblue also uses information that you provide as part of our effort to keep you informed about the status of your subscription, product upgrades, special offers, and other E-iceblue products and services.
We will only collect Personal Information where it is relevant for the purposes for which we are collecting it. We will not process any Personal Information for any purposes that are incompatible with the purposes for which it has been collected or subsequently authorized by you.
Commitment
We, at E-iceblue, are committed to your right to privacy and we support current industry initiatives to preserve the integrity of individual privacy rights on the Internet. Protecting your privacy on-line is a rapidly evolving area, and E-iceblue's Web sites are continually evolving to meet these demands.
Your Consent
By using this Web site, you consent to the terms of our Online Privacy Policy and to E-iceblue's processing of Personal Information for the purposes outlined above. Should the Online Privacy Policy change, we will take reasonable steps to ensure that these changes are brought to your attention by posting all changes prominently on our Web site for a reasonable period of time.
Contact
If you have any comments or questions regarding our Online Privacy Policy, please contact us at comments@e-iceblue.com.