Watermarking is one of the ways to protect Excel files from illegal use. Typically, watermarks define ownership of copyrighted content. In this article, you’ll learn how to programmatically add watermarks to Excel files. In particular, this article shows you how to use C# to add watermarks to Excel worksheets.
- Protect Excel VBA macros
To add watermarks to Excel worksheets, we will use Aspose.Cells for.net. It is a powerful and easy-to-use API that lets you create, modify, or transform Excel files. .NET document development and management can all use Aspose. (Download Aspose.Cells)
Add watermarks to Excel worksheets in C#
Here are the steps to add a watermark to an Excel worksheet using C#.
- Load the Excel file using the Workbook class by providing its path.
- Gets a reference to the worksheet to watermark.
- Create a watermark by adding a new shape to the worksheet and setting its text and properties
- Set shape. IsLocked to true to lock the watermark.
- Set other lock types using shape.setLockedProperty (ShapeLockType, bool).
- Use the workbook.save (String) method to Save the updated Excel file.
The following code example shows how to add a watermark to an Excel worksheet.
// Instantiate a new Workbook Workbook workbook = new Workbook("Excel.xlsx"); // Get the first default sheet Worksheet sheet = workbook.Worksheets[0]; // Add watermark Aspose.Cells.Drawing.Shape wordart = sheet.Shapes.AddTextEffect(MsoPresetTextEffect.TextEffect1, "CONFIDENTIAL", "Arial Black", 50, false, true , 18, 8, 1, 1, 130, 800); // Lock shape aspects wordart.IsLocked = true; wordart.SetLockedProperty(ShapeLockType.Selection, true); wordart.SetLockedProperty(ShapeLockType.ShapeType, true); wordart.SetLockedProperty(ShapeLockType.Move, true); wordart.SetLockedProperty(ShapeLockType.Resize, true); wordart.SetLockedProperty(ShapeLockType.Text, true); // Get the fill format of the word art FillFormat wordArtFormat = wordart.Fill; / / Set the transparency wordArtFormat. Transparency = 0.9; // Save the watermarked Excel file workbook.Save("Watermarked-locked.xlsx");Copy the code
Below is a screenshot of the Excel worksheet with the watermark added.
If you have any questions or requirements, please feel free to join the Aspose Technology Exchange Group (761297826), we are happy to provide you with inquiries and consultation.