本文将详细介绍如何使用C# 给PPT文档添加图片和文本水印的方法。
添加图片水印
Spire.Presentation类提供了一个属性SlideBackground,我们通过该属性来设置当前页面的背景图以用作图片水印。
C#
//加载PPT文档
Presentation ppt = new Presentation();
ppt.LoadFromFile("sample.pptx", FileFormat.Pptx2010);
//为第一张幻灯片设置背景图片类型和样式
ppt.Slides[0].SlideBackground.Type = Spire.Presentation.Drawing.BackgroundType.Custom;
ppt.Slides[0].SlideBackground.Fill.FillType = FillFormatType.Picture;
ppt.Slides[0].SlideBackground.Fill.PictureFill.FillType = PictureFillType.Stretch;
//获取图片并将其设置为页面的背景图
Image img = Image.FromFile("Logo.png");
IImageData image = ppt.Images.Append(img);
ppt.Slides[0].SlideBackground.Fill.PictureFill.Picture.EmbedImage = image;
//保存文档
ppt.SaveToFile("ImageWatermark.pptx", FileFormat.Pptx2010);
VB.NET
'加载PPT文档
Dim ppt As New Presentation()
ppt.LoadFromFile("sample.pptx", FileFormat.Pptx2010)
'为第一张幻灯片设置背景图片类型和样式
ppt.Slides(0).SlideBackground.Type = Spire.Presentation.Drawing.BackgroundType.[Custom]
ppt.Slides(0).SlideBackground.Fill.FillType = FillFormatType.Picture
ppt.Slides(0).SlideBackground.Fill.PictureFill.FillType = PictureFillType.Stretch
'获取图片并将其设置为页面的背景图
Dim img As Image = Image.FromFile("Logo.png")
Dim image__1 As IImageData = ppt.Images.Append(img)
ppt.Slides(0).SlideBackground.Fill.PictureFill.Picture.EmbedImage = image__1
'保存文档
ppt.SaveToFile("ImageWatermark.pptx", FileFormat.Pptx2010)
添加文本水印
添加文本水印时,需要先绘制文本并设置文本格式如字体、颜色及排列方式等,然后将其添加到页面作为水印。
C#
//加载PPT文档
Presentation ppt = new Presentation();
ppt.LoadFromFile("sample.pptx", FileFormat.Pptx2010);
//获取文本的大小
Font stringFont = new Font("Arial", 45);
Size size = TextRenderer.MeasureText("E-iceblue", stringFont);
//绘制文本,设置文本格式并将其添加到第二张幻灯片
RectangleF rect = new RectangleF((ppt.SlideSize.Size.Width - size.Width) / 2, (ppt.SlideSize.Size.Height - size.Height) / 2, size.Width, size.Height);
IAutoShape shape = ppt.Slides[1].Shapes.AppendShape(Spire.Presentation.ShapeType.Rectangle, rect);
shape.Fill.FillType = FillFormatType.None;
shape.ShapeStyle.LineColor.Color = Color.White;
shape.Rotation = -45;
shape.Locking.SelectionProtection = true;
shape.Line.FillType = FillFormatType.None;
shape.TextFrame.Text = "E-iceblue";
TextRange textRange = shape.TextFrame.TextRange;
textRange.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid;
textRange.Fill.SolidColor.Color = Color.FromArgb(120, Color.Black);
textRange.FontHeight = 45;
//保存文档
ppt.SaveToFile("TextWatermark.pptx", FileFormat.Pptx2010);
VB.NET
'加载PPT文档
Dim ppt As New Presentation()
ppt.LoadFromFile("sample.pptx", FileFormat.Pptx2010)
'获取文本的大小
Dim stringFont As New Font("Arial", 45)
Dim size As Size = TextRenderer.MeasureText("E-iceblue", stringFont)
'绘制文本,设置文本格式并将其添加到第二张幻灯片
Dim rect As New RectangleF((ppt.SlideSize.Size.Width - size.Width) / 2, (ppt.SlideSize.Size.Height - size.Height) / 2, size.Width, size.Height)
Dim shape As IAutoShape = ppt.Slides(1).Shapes.AppendShape(Spire.Presentation.ShapeType.Rectangle, rect)
shape.Fill.FillType = FillFormatType.None
shape.ShapeStyle.LineColor.Color = Color.White
shape.Rotation = -45
shape.Locking.SelectionProtection = True
shape.Line.FillType = FillFormatType.None
shape.TextFrame.Text = "E-iceblue"
Dim textRange As TextRange = shape.TextFrame.TextRange
textRange.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid
textRange.Fill.SolidColor.Color = Color.FromArgb(120, Color.Black)
textRange.FontHeight = 45
'保存文档
ppt.SaveToFile("TextWatermark.pptx", FileFormat.Pptx2010)