- 为 PowerPoint 幻灯片添加文本水印
- 为 PowerPoint 幻灯片添加图像水印
为了给 PowerPoint 幻灯片添加水印,我们将使用Aspose.Slides for .NET它是一个功能丰富的 API,可让您从 .NET 应用程序中创建演示文档。此外,它还允许您操作现有的演示文件。
>>你可以点击这里下载Aspose.Slides 最新版测试体验。
在 C# 中为 PowerPoint 幻灯片添加文本水印
以下是使用 C# 为 PowerPoint 幻灯片添加文本水印的步骤。
- 首先,使用Presentation类加载 PowerPoint 演示文稿。
- 获取要在ISlide对象中添加水印的幻灯片的参考。
- 计算水印的位置。
- 向幻灯片的Shapes集合添加一个新的自动形状,并在IAutoShape对象中获取其引用。
- 将文本框添加到形状并使用IAutoShape.AddTextFrame(string)方法设置其文本。
- 设置水印的字体大小、颜色和旋转角度。
- 锁定水印以避免删除或修改。
- 最后,使用Presentation.Save(string, SaveFormat)方法保存更新的 PowerPoint 文件。
以下代码示例展示了如何向 PowerPoint 幻灯片添加文本水印。
// Load presentationPresentation presentation = new Presentation("presentation.pptx");// Get reference of the slideISlide slide = presentation.Slides[0];// Get the center of the slide and calculate watermark's positionPointF center = new PointF(presentation.SlideSize.Size.Width / 2, presentation.SlideSize.Size.Height / 2);float width = 300;float height = 300;float x = center.X - width / 2;float y = center.Y - height / 2;// Add watermark shapeIAutoShape watermarkShape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, x, y, width, height);// Set fill typewatermarkShape.FillFormat.FillType = FillType.NoFill;watermarkShape.LineFormat.FillFormat.FillType = FillType.NoFill;// Set rotation anglewatermarkShape.Rotation = -45; // Set textITextFrame watermarkTextFrame = watermarkShape.AddTextFrame("Watermark");IPortion watermarkPortion = watermarkTextFrame.Paragraphs[0].Portions[0];// Set font size and fill type of the watermarkwatermarkPortion.PortionFormat.FontHeight = 52;watermarkPortion.PortionFormat.FillFormat.FillType = FillType.Solid;int alpha = 150, red = 200, green = 200, blue = 200;watermarkPortion.PortionFormat.FillFormat.SolidFillColor.Color = System.Drawing.Color.FromArgb(alpha, red, green, blue);// Lock Shapes from modifyingwatermarkShape.ShapeLock.SelectLocked = true;watermarkShape.ShapeLock.SizeLocked = true;watermarkShape.ShapeLock.TextLocked = true;watermarkShape.ShapeLock.PositionLocked = true;watermarkShape.ShapeLock.GroupingLocked = true;// Save the presentationpresentation.Save("watermarked-presentation.pptx", SaveFormat.Pptx);

在 C# 中为 PowerPoint 幻灯片添加图像水印
以下是在 C# 中为 PowerPoint 幻灯片添加图像水印的步骤。
- 首先,使用Presentation类加载 PowerPoint 演示文稿。
- 获取要在ISlide对象中添加水印的幻灯片的参考。
- 计算水印的位置。
- 将图像添加到演示文稿并在IPPImage对象中获取其引用。
- 向幻灯片的Shapes集合添加一个新的自动形状,并在IAutoShape对象中获取其引用。
- 将IAutoShape.FillFormat.FillType设置为 FillType.Picture。
- 通过分配设置水印图像IPPImage对象IAutoShape.FillFormat.PictureFillFormat.Picture.Image财产。
- 锁定水印以避免删除或修改。
- 最后,使用Presentation.Save(string, SaveFormat)方法保存更新的 PowerPoint 文件。
以下代码示例展示了如何向 PowerPoint 幻灯片添加图像水印。
// Load presentationPresentation presentation = new Presentation("presentation.pptx");// Get reference of the slideISlide slide = presentation.Slides[0];// Get the center of the slide and calculate watermark's positionPointF center = new PointF(presentation.SlideSize.Size.Width / 2, presentation.SlideSize.Size.Height / 2);float width = 300;float height = 300;float x = center.X - width / 2;float y = center.Y - height / 2;// Load imageIPPImage image = presentation.Images.AddImage(File.ReadAllBytes("watermark.png"));// Add watermark shape and set imageIAutoShape watermarkShape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, x, y, width, height);watermarkShape.FillFormat.FillType = FillType.Picture;watermarkShape.FillFormat.PictureFillFormat.Picture.Image = image;watermarkShape.FillFormat.PictureFillFormat.PictureFillMode = PictureFillMode.Stretch; watermarkShape.LineFormat.FillFormat.FillType = FillType.NoFill;// Lock Shapes from modifyingwatermarkShape.ShapeLock.SelectLocked = true;watermarkShape.ShapeLock.SizeLocked = true;watermarkShape.ShapeLock.TextLocked = true;watermarkShape.ShapeLock.PositionLocked = true;watermarkShape.ShapeLock.GroupingLocked = true;// Save the presentationpresentation.Save("watermarked-presentation.pptx", SaveFormat.Pptx);
如果你想试用Aspose的全部完整功能,可 联系在线客服获取30天临时授权体验。
还想要更多吗可以点击阅读【Aspose最新资源在线文库】,查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询。
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!