C#添加、刪除PPT水印

【前言】

水印是一種有效的文檔防偽手段,在工作中非常實用。在接下來的示例中,將介紹如何通過C#編程語言來實現(xiàn)Power Point幻燈片添加水印。我們知道,水印可以分為文本水印、圖片水印,在此也將分別介紹實現(xiàn)兩種水印效果的具體方法。另外,水印幻燈片中已經(jīng)存在的水印,如果我們想要去除水印效果,也可以參考下面的關(guān)于刪除水印的方法。

為汨羅等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計制作服務(wù),及汨羅網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為做網(wǎng)站、成都網(wǎng)站制作、汨羅網(wǎng)站設(shè)計,以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!

【工具】

* Free Spire.Presentation for .NET 3.3 (社區(qū)版)
編輯代碼時,注意添加引用Spire.Presentation.dll(dll文件可在安裝路徑下的Bin文件夾中獲?。?/p>

C#  添加、刪除PPT水印

【示例1】添加文本水印

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("內(nèi)部資料", stringFont);

            //繪制一個Shape并指定大小、填充顏色、邊框顏色和旋轉(zhuǎn)度
            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;

            //設(shè)定形狀保護(hù)屬性、填充模式
            shape.Locking.SelectionProtection = true;
            shape.Line.FillType = FillFormatType.None;

            //設(shè)置文本水印文字,并設(shè)置水印填充模式、水印顏色、大小等
            shape.TextFrame.Text = "內(nèi)部資料";
            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");
        }
    }
}

文本水印添加效果:
C#  添加、刪除PPT水印

【示例2】添加圖片水印

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);

            //為第一張幻燈片設(shè)置背景圖片類型和樣式
            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;

            //加載圖片并為第一張幻燈片設(shè)置水印效果
            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");
        }
    }
}

圖片水印添加效果:
C#  添加、刪除PPT水印

【示例3】刪除文本水印效果

using Spire.Presentation;

namespace DeleteTextWatermark_PPT
{
    class Program
    {
        static void Main(string[] args)
        {
            //實例化Presentation類,加載有水印的PowerPoint文檔
            Presentation ppt = new Presentation();
            ppt.LoadFromFile("TextWatermark.pptx");

            //遍歷每一張幻燈片, 查找水印文字內(nèi)容所在的形狀并刪除
            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("內(nèi)部資料"))
                        {
                            ppt.Slides[i].Shapes.Remove(shape);
                        }
                    }
                }
            }

            //保存并打開文檔
            ppt.SaveToFile("RemoveTextWatermak.pptx", FileFormat.Pptx2010);
            System.Diagnostics.Process.Start("RemoveTextWatermak.pptx");
        }
    }
}

刪除效果:
C#  添加、刪除PPT水印

【示例4】刪除圖片水印效果

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");

            //遍歷每一張幻燈片, 設(shè)置背景填充類型為None
            for (int i = 0; i < ppt.Slides.Count; i++)
            {
                ppt.Slides[0].SlideBackground.Fill.FillType = FillFormatType.None;
            }

            //保存結(jié)果文檔到本地并打開
            ppt.SaveToFile("RemovePicWatermak.pptx", FileFormat.Pptx2010);
            System.Diagnostics.Process.Start("RemovePicWatermak.pptx");
        }
    }
}

刪除效果:
C#  添加、刪除PPT水印

(本文完)

分享題目:C#添加、刪除PPT水印
鏈接地址:http://bm7419.com/article38/jdddsp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站網(wǎng)站建設(shè)、網(wǎng)站內(nèi)鏈面包屑導(dǎo)航、網(wǎng)站維護(hù)、網(wǎng)站設(shè)計公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設(shè)