標籤:免費ppt控制項 net c#
在設定形狀屬性後,可禁止對形狀某些屬性的編輯,以達到保護形狀格式的目的。常用方法有:鎖定形狀禁止選擇, 和禁止對形狀的具體屬性進行修改。前者保護形狀的所有格式,無法選取,無法修改任何屬性。後者,對形狀的具體屬性,例如形狀大小,位置,類型,文本,旋轉等,進行編輯限制,以達到保護形狀某些屬性的目的。
本篇博文,旨在介紹使用我司免費產品,Free Spire.Presentation,使用C#,建立PPT文檔,添加設定形狀,並對形狀進行保護設定。歡迎大家從我司官網E-iceblue下載使用Free Spire.Presentation,測試這些功能。下載完成後,請將bin檔案夾的.dll添加作為Visual Studio的引用。
需要用到的命名空間(namespace):
using Spire.Presentation;using Spire.Presentation.Drawing;using System.Drawing;
步驟一:建立PPT文檔,並添加矩形形狀。
Presentation presentation = new Presentation();IAutoShape shape = presentation.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(50, 100, 450, 150));
步驟二:設定形狀格式,為其添加文本,設定文本字型,顏色,對齊。
shape.Fill.FillType = FillFormatType.None;shape.ShapeStyle.LineColor.Color = Color.DarkGreen;shape.TextFrame.Paragraphs[0].Alignment = TextAlignmentType.Justify;shape.TextFrame.Text = "示範文檔:\n 灰色部分表示不可編輯。 綠色和黑色部分表示可編輯";shape.TextFrame.Paragraphs[0].TextRanges[0].LatinFont = new TextFont("楷體");shape.TextFrame.Paragraphs[0].TextRanges[0].Fill.FillType = FillFormatType.Solid;shape.TextFrame.Paragraphs[0].TextRanges[0].Fill.SolidColor.Color = Color.Black;
步驟三:利用bool聲明禁止改變的屬性部分。此處設定可選取,然後再對具體屬性保護項進行設定。
shape.Locking.RotationProtection = false;shape.Locking.SelectionProtection = false;shape.Locking.ResizeProtection = true;shape.Locking.PositionProtection = true;shape.Locking.ShapeTypeProtection = true;shape.Locking.AspectRatioProtection = true;shape.Locking.TextEditingProtection = true;shape.Locking.AdjustHandlesProtection = true;
步驟4:儲存presentation文檔,啟動查看效果。
presentation.SaveToFile("result.pptx", FileFormat.Pptx2010);System.Diagnostics.Process.Start("result.pptx");
運行結果圖:
完整代碼:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using Spire.Presentation;using Spire.Presentation.Drawing;using System.Drawing;namespace test{ class Program { static void Main(string[] args) { Presentation presentation = new Presentation(); IAutoShape shape = presentation.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(50, 100, 450, 150)); shape.Fill.FillType = FillFormatType.None; shape.ShapeStyle.LineColor.Color = Color.DarkGreen; shape.TextFrame.Paragraphs[0].Alignment = TextAlignmentType.Justify; shape.TextFrame.Text = "示範文檔:\n 灰色部分表示不可編輯。 綠色和黑色部分表示可編輯"; shape.TextFrame.Paragraphs[0].TextRanges[0].LatinFont = new TextFont("楷體"); shape.TextFrame.Paragraphs[0].TextRanges[0].Fill.FillType = FillFormatType.Solid; shape.TextFrame.Paragraphs[0].TextRanges[0].Fill.SolidColor.Color = Color.Black; shape.Locking.RotationProtection = false; shape.Locking.SelectionProtection = false; shape.Locking.ResizeProtection = true; shape.Locking.PositionProtection = true; shape.Locking.ShapeTypeProtection = true; shape.Locking.AspectRatioProtection = true; shape.Locking.TextEditingProtection = true; shape.Locking.AdjustHandlesProtection = true; presentation.SaveToFile("result.pptx", FileFormat.Pptx2010); System.Diagnostics.Process.Start("result.pptx"); } }}
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
C# Presentation 禁止或允許修改形狀