在这个简单而详尽的教程中,我们将展示如何在没有安装 PowerPoint 的情况下使用 C# 在 PPTX Presentation 中添加图像水印。现在,PPTX 是 PowerPoint 演示文稿的流行格式,但您也可以将此示例用于 PPT 格式以及添加图像水印以保护演示文稿的知识产权。
在 C# 中将图像水印添加到 PPTX 演示文稿的步骤
- 从 NuGet.org 下载 Aspose.Slides for .NET 包
- 使用 Aspose.Slides 命名空间加载和添加水印
- 使用 SetLicense 方法设置许可证
- 使用 Presentation Class 对象加载演示文稿以添加图片水印
- 在演示图像集合中加载水印/徽标图像
- 访问和遍历演示文稿中的 Master Slide/s
- 对于每张母版幻灯片,添加一个带有水印图像的 PictureFrame
- 格式化形状属性
- 对添加的形状应用锁定以保护图像水印
- 保存带水印的演示文稿
早些时候,我们在另一个指南主题中研究了 如何在 C# 的 PowerPoint 演示文稿中插入草稿水印。但本主题描述了在 C# 的 PowerPoint 演示文稿中添加图像水印的步骤。您不再依赖 Microsoft PowerPoint 或 Interop 来使用此功能,并且可以在所有平台上无缝执行代码。
最重要的是,Aspose.Slides 提供的独特形状锁定功能甚至无法在 PowerPoint 中公开使用以保护您的水印图像。您可以通过使用锁定功能并将其应用于图像水印形状来保护演示文稿的知识产权,以禁止在 PowerPoint 演示文稿中进行任何修改或修改。
在没有互操作的 C# 中在 PowerPoint 中添加图像水印的代码
using System; | |
using System.Drawing; | |
using Aspose.Slides; | |
using Aspose.Slides.Export; | |
namespace SlidesWatermark | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
string PathForWatermarkPptFile = @"Y:\Downloads\"; | |
License license = new License(); | |
license.SetLicense(PathForWatermarkImageFile + "Conholdate.Total.Product.Family.lic"); | |
//Load the presentation to insert watermark | |
Presentation WatermarkPptxPresentation = new Presentation(PathForWatermarkPptFile + "PictureWatermark.pptx"); | |
// Loading watermark image to add in PPTX | |
System.Drawing.Image WatermarkLogo = (System.Drawing.Image)new Bitmap("Picture Watermark Logo.jpg"); | |
IPPImage WatermarkImage = WatermarkPptxPresentation.Images.AddImage(WatermarkLogo); | |
//Accessing the master slides for adding watermark image | |
foreach (IMasterSlide masterSlide in WatermarkPptxPresentation.Masters) | |
{ | |
//Adding a Ppt watermark shape for logo image | |
IPictureFrame PptxWatermark = masterSlide.Shapes.AddPictureFrame(ShapeType.Rectangle,0, 0, | |
200, 50, WatermarkImage); | |
//Set the rotation angle of the shape | |
PptxWatermark.Rotation = 325; | |
//Lock Pptx watermark image shape for protection in PowerPoint | |
PptxWatermark.ShapeLock.SizeLocked = true; | |
PptxWatermark.ShapeLock.SelectLocked = true; | |
PptxWatermark.ShapeLock.PositionLocked = true; | |
} | |
//Saving the image watermark PPTX presentation file | |
WatermarkPptxPresentation.Save(PathForWatermarkPptFile + "ImageWatermarkedPresentation.pptx", | |
SaveFormat.Pptx); | |
} | |
} | |
} |
该示例适用于使用 C# 的任何 .NET 应用程序环境,包括 ASP.NET Web 应用程序、Windows 窗体应用程序和基于控制台的应用程序。它可以在您的本地工作机器或任何安装了 .NET Framework 的服务器上使用。