Inserting text watermarks into PowerPoint presentations is an effective way to protect intellectual property and ensure content authenticity of these documents. Spire.Presentation for .NET provides a powerful and flexible way to programmatically add text watermarks to PowerPoint slides. Unlike PowerPoint's built-in features, this approach allows for batch processing, precise control over watermark placement and appearance, and integration into larger .NET applications.
This article will demonstrate how to insert text watermarks to PowerPoint presentations with C# using the Spire.Presentation for .NET library.
Install Spire.Presentation for .NET
To begin with, you need to add the DLL files included in the Spire.Presentation 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.Presentation
Insert a Single Watermark into PowerPoint Slides
Using Spire.Presentation for .NET, developers can add a single text watermark to a PowerPoint presentation by creating a locked transparent text box with the watermark in a custom style on each page. The detailed steps are as follows:
- Create an instance of Presentation class and load a PowerPoint file using Presentation.LoadFromFile() method.
- Define the watermark text, create a Font object, and measure the size of the text.
- Get the size of the slides in the presentation through Presentation.SlideSize.Size property.
- Create a RectangleF object based on the text size and the slide size.
- Iterate through the slides in the presentation:
- Create an IAutoShape object on each slide at the position of the rectangle using ISlide.Shapes.AppendShape() method.
- Set the style of the shape using the properties under IAutoShape class.
- Add the watermark text to the shape through IAutoShape.TextFrame.Text property.
- Get the watermark text as a TextRange object through IAutoShape.TextFrame.TextRange property.
- Set the format of the watermark text through the properties under TextRange class.
- Save the presentation using Presentation.SaveToFile() method.
- C#
using Spire.Presentation; using Spire.Presentation.Drawing; using System.Drawing; namespace SingleTextWatermarkPowerPoint { class Program { static void Main(string[] args) { // Create an instance of Presentation Presentation presentation = new Presentation(); // Load a PowerPoint file presentation.LoadFromFile("Sample.pptx"); // Define the watermark text and create a font for it string text = "Example"; // Create a font object Font font = new Font("Arial", 50); // Measure the size of the watermark text Graphics graphics = Graphics.FromImage(new Bitmap(1, 1)); SizeF size = graphics.MeasureString(text, font); // Get the slide size SizeF slideSize = presentation.SlideSize.Size; // Create a retangle based on the text size and page size RectangleF rect = new RectangleF((slideSize.Width - size.Width) / 2, (slideSize.Height - size.Height) /2, size.Width, size.Height); // Iterate through the slides in the presentation foreach (ISlide slide in presentation.Slides) { // Create a shape of watermark on each slide IAutoShape watermark = slide.Shapes.AppendShape(ShapeType.Rectangle, rect); // Set the style of the watermark watermark.Fill.FillType = FillFormatType.None; watermark.ShapeStyle.LineColor.Color = Color.Empty; watermark.Rotation = -45; watermark.Locking.SelectionProtection = true; watermark.Line.FillType = FillFormatType.None; // Add the watermark text to the shape watermark.TextFrame.Text = text; // Set the style of the watermark text TextRange textRange = watermark.TextFrame.TextRange; textRange.Fill.FillType = FillFormatType.Solid; textRange.Fill.SolidColor.Color = Color.FromArgb(120, Color.HotPink); textRange.FontHeight = 50; } // Save the presentation presentation.SaveToFile("output/PowerPointSingleTextWatermark.pptx", FileFormat.Auto); presentation.Dispose(); } } }
Insert Repeated Watermarks into PowerPoint Slides
Developers can insert repeated text watermarks into slides by drawing multiple identical text watermarks at specified intervals on the slides. Here are the detailed steps:
- Create an instance of Presentation class and load a PowerPoint file using Presentation.LoadFromFile() method.
- Define the watermark text, create a Font object, and measure the size of the text.
- Get the size of the slides in the presentation through Presentation.SlideSize.Size property.
- Iterate through the slides in the presentation:
- Define the start position and the intervals.
- Add watermark shapes and text and set their format.
- Move the lateral position to the next interval on the row after finishing adding a watermark.
- Move the vertical position to the next row after finishing adding the watermarks of each row.
- Save the presentation using Presentation.SaveToFile() method.
- C#
using Spire.Presentation.Drawing; using Spire.Presentation; using System.Drawing; namespace RepeatedTextWatermarkPowerPoint { class Program { static void Main(string[] args) { // Create an instance of Presentation Presentation presentation = new Presentation(); // Load a PowerPoint file presentation.LoadFromFile("Sample.pptx"); // Define the watermark text and create a font for it string text = "Example"; // Create a font object Font font = new Font("Arial", 20); // Measure the size of the watermark text Graphics graphics = Graphics.FromImage(new Bitmap(1, 1)); SizeF size = graphics.MeasureString(text, font); // Get the slide size SizeF slideSize = presentation.SlideSize.Size; // Iterate through the slides in the presentation foreach (ISlide slide in presentation.Slides) { float x = 30; float y = 80; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { // Create a rectangle RectangleF rect = new RectangleF(x, y, size.Width, size.Height); IAutoShape watermark = slide.Shapes.AppendShape(ShapeType.Rectangle, rect); // Set the style of the watermark watermark.Fill.FillType = FillFormatType.None; watermark.ShapeStyle.LineColor.Color = Color.Empty; watermark.Rotation = -45; watermark.Locking.SelectionProtection = true; watermark.Line.FillType = FillFormatType.None; // Add the watermark text to the shape watermark.TextFrame.Text = text; // Set the style of the watermark text TextRange textRange = watermark.TextFrame.TextRange; textRange.Fill.FillType = FillFormatType.Solid; textRange.Fill.SolidColor.Color = Color.FromArgb(120, Color.HotPink); textRange.FontHeight = 20; x += ((slideSize.Width - 60) / 3 + size.Width / 2); } x = 30; y += ((slideSize.Height - 160) / 3 + size.Height / 2); } } // Save the presentation presentation.SaveToFile("output/PowerPointRepeatedTextWatermark.pptx", FileFormat.Auto); presentation.Dispose(); } } }
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.