本文指导如何使用 C# REST API 在 PowerPoint 中添加水印图片。您将学习**如何使用基于 .NET 的 SDK 通过 C# REST 接口在 PowerPoint 中将照片设为水印。它将共享详细信息以自定义图片,然后将其作为水印添加到演示文稿中。
先决条件
下载 Aspose.Slides Cloud SDK for Dotnet to add an image watermark
使用上述SDK设置C#工程,用于插入图片水印
使用 C# REST API 在 PowerPoint 中插入图像水印的步骤
- 使用客户端凭据初始化 Aspose SlidesApi
- 使用 UploadFile() 方法将演示文稿文件上传到服务器
- 将用于水印的图像数据读取到字节数组中
- 使用 PictureFrame 类设置保存水印图像的图像框架
- 使用 CreateImageWatermark() 方法将图像作为水印添加到演示文稿中
- 下载修改后的演示文稿,并使用 DownloadFile() 方法添加水印
- 将更新后的演示文稿保存在本地
这些步骤描述如何使用 C# RESTful 服务在 PowerPoint 中使图片成为水印。初始化SlidesApi对象,将演示文稿上传到服务器,并将水印图像读取到字节数组中。设置 PictureFrame 对象以设置水印参数,并使用 CreateImageWatermark() 方法将其添加到演示文稿中。
使用基于 C# .NET 的 API 在 PowerPoint 中添加图像水印的代码
using Aspose.Slides.Cloud.Sdk; | |
using Aspose.Slides.Cloud.Sdk.Model; | |
using System; | |
using System.IO; | |
namespace PresentationProcessor | |
{ | |
// This class demonstrates how to modify a slide deck by adding an image watermark. | |
class ModifySlide | |
{ | |
static void Main(string[] args) | |
{ | |
// Initialize the Aspose Slides API with client credentials (replace with actual credentials) | |
var slideService = new SlidesApi("ID", "KEY"); | |
// Define the name of the presentation file to be modified | |
string inputFileName = "OriginalSlides.pptx"; | |
// Specify the local path of the image that will be used as a watermark | |
string imagePath = "NewImage.png"; | |
// Upload the presentation file to the server | |
var uploadResult = slideService.UploadFile(inputFileName, new MemoryStream(File.ReadAllBytes(inputFileName))); | |
// Read the image data that will be used for the watermark | |
byte[] imageContent = File.ReadAllBytes(imagePath); | |
// Set up the image frame that will hold the watermark image | |
PictureFrame newImageFrame = new PictureFrame | |
{ | |
X = 50, // Horizontal position of the watermark (from the left) | |
Y = 50, // Vertical position of the watermark (from the top) | |
Width = 800, // Width of the watermark image | |
Height = 450, // Height of the watermark image | |
PictureFillFormat = new PictureFill | |
{ | |
Base64Data = Convert.ToBase64String(imageContent), // The image data encoded in base64 | |
PictureFillMode = PictureFill.PictureFillModeEnum.Stretch, // Image will stretch to fit the frame | |
} | |
}; | |
// Add the image as a watermark to the presentation | |
slideService.CreateImageWatermark(inputFileName, null, newImageFrame); | |
// Download the modified presentation with the watermark added | |
Stream modifiedFileStream = slideService.DownloadFile(inputFileName); | |
// Save the updated presentation locally | |
using (var localFileStream = new FileStream("UpdatedSlideDeck.pptx", FileMode.Create, FileAccess.Write)) | |
{ | |
// Copy the content of the downloaded file stream to the local file stream | |
modifiedFileStream.CopyTo(localFileStream); | |
} | |
} | |
} | |
} |
此代码演示如何使用基于 C# .NET 的 API 在 PowerPoint 中将图像设为水印。配置图片框以设置水印图像从左上角开始的位置、大小和填充格式。您还可以设置 DPI、裁剪图片、平铺偏移和比例以及 SVG 数据。
本文教我们如何使用 C# 低代码 API 在 PowerPoint 中为图像添加水印。要从演示文稿中删除水印,请参阅文章 使用 C# REST API 从演示文稿中删除水印。