Knowledgebase (2330)
Children categories
This article is designed to give you a detailed description about how to write a "HelloWorld" demo of Spire.XLS for WPF by Visual Studio. You can follow the below procedure step by step to finish this task. But first of all, please make sure that Spire.XLS for WPF and Visual Studio 2010 are correctly installed on system.
Step 1. Create a new project.
1. Create a new project by choosing WPF Application in Visual Studio and name the project "HelloWorld". If you want to create a C# project, select Visual C#, WPF Application, if you want to build a Visual Basic project, please choose Visual Basic, WPF Application. The detail process is:
Click File → New → Project → WPF Application → Name → OK
2. Set the Target framework property of the HelloWorld project in Solution Explorer to be .NET Framework 4.The process is:
Click Solution Explorer-> HelloWorld (The project that you already built) -> Properties (right click HelloWorld)->Target framework->.NET Framework 4
3. Add a button in MainWindow. The default button name is "button1". You can set button1 Content property to be "Run" in its properties by right clicking it. Thus, it shows "Run" in the MainWindow. You can perform as below:
Click View → Toolbox → Button → Properties (right click button1) → Content → set the Content to be "Run"
Step 2. Add reference and project namespaces.
1. Add Spire.XLS. Wpf.dll as reference in Project. The Default location of Spire.Doc for WPF is “C:\Program Files\e-iceblue\Spire.XLS for WPF”. Details are:
Click Project → Add Reference → Browse → Choose the folder contains Spire.XLS for WPF → Bin → WPF 4.0 → Spire.XLS.Wpf.dll
2. Double click the "Run" button, you can see the following method has been added automatically:
namespace HelloWorld
{
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
}
}
}
Namespace HelloWorld
'''
''' Interaction logic for MainWindow.xaml
'''
Public Partial Class MainWindow
Inherits Window
Public Sub New()
InitializeComponent()
End Sub
Private Sub button1_Click(sender As Object, e As RoutedEventArgs)
End Sub
End Class
End Namespace
3. Add the below namespaces at the top of the method.
using Spire.Xls;
Imports Spire.Xls
Step 3. Write "Hello, World!" in the method.
Add the following code to the method button1_click
//create a new excel workbook
Workbook workbook = new Workbook();
//edit the first worksheet of the workbook
Worksheet sheet = workbook.Worksheets[0];
sheet.Range["A1"].Text = "Hello, World!";
//save the workbook
workbook.SaveToFile(@"..\..\sample.xls", ExcelVersion.Version97to2003);
//launch the workbook
System.Diagnostics.Process.Start(@"..\..\sample.xls");
'create a new excel workbook
Dim workbook As New Workbook()
'edit the first worksheet of the workbook
Dim sheet As Worksheet = workbook.Worksheets(0)
sheet.Range("A1").Text = "Hello, World!"
'save the workbook
workbook.SaveToFile("..\..\sample.xls", ExcelVersion.Version97to2003)
'launch the workbook
System.Diagnostics.Process.Start("..\..\sample.xls")
Step 4. Debug the project
Right click the project HelloWorld in Solution Explorer → Debug → Start new instance → Click Run in MainWindow, an Excel Document will be created, edited and opened. The string "Hello, World!" is drawn in the first cell A1 of sheet 1.
Preview

This document aims at clearly introducing a simple “HelloWorld” demo about Spire.Doc for WPF by using Visual Studio. The below procedure will help you realize this task step by step. Before getting started, please make sure that Spire.Doc for WPF and Visual Studio are correctly installed on system.
Step 1. Create a new project.
1. Create a new project by choosing WPF Application in Visual Studio and name the project "HelloWorld". If you want to create a C# project, select Visual C#, WPF Application, if you want to build a Visual Basic project, please choose Visual Basic, WPF Application. The detail process is:
Click File → New → Project → WPF Application → Name → OK
2. Set the Target framework property of the HelloWorld project in Solution Explorer to be .NET Framework 4.The process is:
Click Solution Explorer → HelloWorld (The project that you already built) → Properties(right click HelloWorld) → Target framework → .NET Framework 4
3. Add a button in MainWindow. The default button name is "button1". You can set button1 Content property to be "Run" in its properties by right clicking it. Thus, it shows "Run" in the MainWindow. You can perform as below:
Click View → Toolbox → Button → Properties (right click button1) → Content → set the Content to be "Run"
Step 2. Add reference and project namespaces.
1. Add Spire.Doc. Wpf.dll as reference in Project. The Default location of Spire.Doc for WPF is “C:\Program Files\e-iceblue\Spire.Doc for WPF”. Details are:
Click Project → Add Reference → Browse → Choose the folder contains Spire.Doc for WPF → Bin → WPF 4.0 → Spire.Doc.Wpf.dll
2. Double click the "Run" button, you can see the following method has been added automatically:
namespace HelloWorld
{
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
}
}
}
Namespace HelloWorld
'''
''' Interaction logic for MainWindow.xaml
'''
Public Partial Class MainWindow
Inherits Window
Public Sub New()
InitializeComponent()
End Sub
Private Sub button1_Click(sender As Object, e As RoutedEventArgs)
End Sub
End Class
End Namespace
3. Add the below namespaces at the top of the method.
using Spire.Doc; using Spire.Doc.Documents;
Imports Spire.Doc Imports Spire.Doc.Documents
Step 3. Write "Hello, World!” in the method.
Add the following code to the method button1_click
//create a new document using spire.Doc
Document document = new Document();
//add one paragraph
Paragraph paragraph = document.AddSection().AddParagraph();
paragraph.AppendText("Hello, World!");
//save the document
document.SaveToFile(@"..\..\sample.doc", FileFormat.Doc);
//launch the document
System.Diagnostics.Process.Start(@"..\..\sample.doc");
'create a new document using spire.Doc
Dim document As New Document()
'add one paragraph
Dim paragraph As Paragraph = document.AddSection().AddParagraph()
paragraph.AppendText("Hello, World!")
'save the document
document.SaveToFile("..\..\sample.doc", FileFormat.Doc)
'launch the document
System.Diagnostics.Process.Start("..\..\sample.doc")
Step 4. Debug the project
Right click the project HelloWorld in Solution Explorer → Debug-> Start new instance → Click Run in MainWindow, a Word Document will be created, edited and opened. The string “Hello, World!” is drawn in the first line of page1.
Preview


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.