您好,登錄后才能下訂單哦!
水印是一種有效的文檔防偽手段,在工作中非常實用。在接下來的示例中,將介紹如何通過C#編程語言來實現Power Point幻燈片添加水印。我們知道,水印可以分為文本水印、圖片水印,在此也將分別介紹實現兩種水印效果的具體方法。另外,水印幻燈片中已經存在的水印,如果我們想要去除水印效果,也可以參考下面的關于刪除水印的方法。
* Free Spire.Presentation for .NET 3.3 (社區版)
編輯代碼時,注意添加引用Spire.Presentation.dll(dll文件可在安裝路徑下的Bin文件夾中獲取)
using System;
using System.Text;
using Spire.Presentation;
using System.Drawing;
using Spire.Presentation.Drawing;
using System.Windows.Forms;
namespace InsertWatermark_PPT
{
class Program
{
static void Main(string[] args)
{
//初始化一個Presentation類實例并加載文檔
Presentation ppt = new Presentation();
ppt.LoadFromFile("test.pptx", FileFormat.Pptx2010);
//初始化一個Font類字體實例并實例化字體格式
Font stringFont = new Font("Arial", 90);
Size size = TextRenderer.MeasureText("內部資料", stringFont);
//繪制一個Shape并指定大小、填充顏色、邊框顏色和旋轉度
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[0].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 = "內部資料";
TextRange textRange = shape.TextFrame.TextRange;
textRange.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid;
textRange.Fill.SolidColor.Color = Color.FromArgb(150, Color.LightBlue);
textRange.FontHeight = 90;
//保存并打開文檔
ppt.SaveToFile("TextWatermark.pptx", FileFormat.Pptx2010);
System.Diagnostics.Process.Start("TextWatermark.pptx");
}
}
}
文本水印添加效果:
using System;
using System.Drawing;
using Spire.Presentation;
using Spire.Presentation.Drawing;
namespace ImageWatermark_PPT
{
class Program
{
static void Main(string[] args)
{
//初始化一個Presentation類實例并加載文檔
Presentation ppt = new Presentation();
ppt.LoadFromFile("test.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("1.jpg");
IImageData image = ppt.Images.Append(img);
ppt.Slides[0].SlideBackground.Fill.PictureFill.Picture.EmbedImage = image;
//保存并打開文檔
ppt.SaveToFile("ImageWatermark.pptx", FileFormat.Pptx2010);
System.Diagnostics.Process.Start("ImageWatermark.pptx");
}
}
}
圖片水印添加效果:
using Spire.Presentation;
namespace DeleteTextWatermark_PPT
{
class Program
{
static void Main(string[] args)
{
//實例化Presentation類,加載有水印的PowerPoint文檔
Presentation ppt = new Presentation();
ppt.LoadFromFile("TextWatermark.pptx");
//遍歷每一張幻燈片, 查找水印文字內容所在的形狀并刪除
for (int i = 0; i < ppt.Slides.Count; i++)
{
for (int j = 0; j < ppt.Slides[i].Shapes.Count; j++)
{
if (ppt.Slides[i].Shapes[j] is IAutoShape)
{
IAutoShape shape = ppt.Slides[i].Shapes[j] as IAutoShape;
if (shape.TextFrame.Text.Contains("內部資料"))
{
ppt.Slides[i].Shapes.Remove(shape);
}
}
}
}
//保存并打開文檔
ppt.SaveToFile("RemoveTextWatermak.pptx", FileFormat.Pptx2010);
System.Diagnostics.Process.Start("RemoveTextWatermak.pptx");
}
}
}
刪除效果:
using Spire.Presentation;
using Spire.Presentation.Drawing;
namespace DeleteImageWatermark_PPT
{
class Program
{
static void Main(string[] args)
{
//實例化Presentation類,加載有圖片水印的PowerPoint文檔
Presentation ppt = new Presentation();
ppt.LoadFromFile("ImageWatermark.pptx");
//遍歷每一張幻燈片, 設置背景填充類型為None
for (int i = 0; i < ppt.Slides.Count; i++)
{
ppt.Slides[0].SlideBackground.Fill.FillType = FillFormatType.None;
}
//保存結果文檔到本地并打開
ppt.SaveToFile("RemovePicWatermak.pptx", FileFormat.Pptx2010);
System.Diagnostics.Process.Start("RemovePicWatermak.pptx");
}
}
}
刪除效果:
(本文完)
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。