在本分步教程中,我们将详细说明如何在未安装 PowerPoint 的情况下在 C# 中的 PowerPoint Presentation 中插入草稿水印。 PPTX 格式是当今广泛用于 PowerPoint 演示文稿的格式,但此示例也适用于旧版 PPT,用于添加机密水印 PowerPoint。
在 C# 中向 PowerPoint 添加机密水印的步骤
- 从 NuGet.org 获取 Aspose.Slides for .NET 包
- 使用 Aspose.Slides 命名空间加载演示文稿并插入水印
- 使用 SetLicense 方法设置许可证
- 加载 PowerPoint 演示文稿以在 Presentation Class 对象中插入水印
- 访问和遍历演示文稿中的 Master Slide/s
- 对于每张母版幻灯片,添加一个自选图形
- 为添加的形状 TextFrame 插入机密草稿文本
- 格式化形状和文本属性
- 对添加的形状应用锁定以保护水印
- 保存带水印的演示文稿
之前,我们在另一个指南主题中研究了 如何在 C# .NET 中保护 PowerPoint 演示文稿。但本主题描述了在 C# 的 powerpoint 中添加机密水印的步骤。您无需依赖 Microsoft PowerPoint 或 Interop 即可使用此功能,并且可以在所有平台上无缝运行此代码。
最重要的是,形状锁定功能是 Aspose.Slides 提供的一项独特功能,它甚至在 PowerPoint 中都没有公开提供。您可以通过使用锁定功能并将其应用于水印形状并禁止任何人在 PowerPoint 中修改或删除它来保护演示文稿的知识产权。
在没有互操作的 C# 中在 PowerPoint 中添加草稿水印的代码
using System; | |
using Aspose.Slides; | |
using Aspose.Slides.Export; | |
namespace WatermarkPPTXinC_Sharp | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
string PathForWatermarkPptFile = @".\"; | |
//Loading the product license | |
License license = new License(); | |
license.SetLicense(PathForWatermarkPptFile+"Conholdate.Total.Product.Family.lic"); | |
//Load the presentation to insert watermark | |
Presentation WatermarkPptxPresentation = new Presentation(PathForWatermarkPptFile + "Draft.pptx"); | |
//Accessing the master slides for adding watermark | |
foreach (IMasterSlide masterSlide in WatermarkPptxPresentation.Masters) | |
{ | |
//Adding a Ppt watermark shape | |
IAutoShape PptxWatermark = masterSlide.Shapes.AddAutoShape(ShapeType.Rectangle, | |
WatermarkPptxPresentation.SlideSize.Size.Width / 2 - 50, | |
WatermarkPptxPresentation.SlideSize.Size.Height / 2 - 50, | |
200, 50); | |
//Setting rotation angle and fill type of the shape | |
PptxWatermark.Rotation = 325; | |
PptxWatermark.FillFormat.FillType = FillType.NoFill; | |
//Adding Text frame with watermark text | |
ITextFrame WatermarkText = PptxWatermark.AddTextFrame("Confidential Draft"); | |
//Setting textual properties of the watermark text | |
IPortionFormat WatermarkTextFormat = WatermarkText.Paragraphs[0].Portions[0].PortionFormat; | |
WatermarkTextFormat.FontBold = NullableBool.True; | |
WatermarkTextFormat.FontItalic = NullableBool.True; | |
WatermarkTextFormat.FontHeight = 20; | |
WatermarkTextFormat.FillFormat.FillType = FillType.Solid; | |
WatermarkTextFormat.FillFormat.SolidFillColor.Color = System.Drawing.Color.Red; | |
//Locking Pptx watermark shape to be uneditable in PowerPoint | |
PptxWatermark.AutoShapeLock.TextLocked=true; | |
PptxWatermark.AutoShapeLock.SelectLocked = true; | |
PptxWatermark.AutoShapeLock.PositionLocked = true; | |
} | |
//Saving the watermark PowerPoint presentation file | |
WatermarkPptxPresentation.Save(PathForWatermarkPptFile+"WatermarkPresentation.pptx", | |
SaveFormat.Pptx); | |
} | |
} | |
} |
该示例可以在任何使用 C# 代码的 .NET 应用程序环境中使用,无论是 ASP.NET Web 应用程序、Windows 窗体应用程序还是基于控制台的。您还可以在本地工作机器或安装了 .NET Framework 的任何服务器上使用它。