在设定形状属性后,可禁止对形状某些属性的编辑,以达到保护形状格式的目的。常用方法有:锁定形状禁止选择, 和禁止对形状的具体属性进行修改。前者保护形状的所有格式,无法选取,无法修改任何属性。后者,对形状的具体属性,例如形状大小,位置,类型,文本,旋转等,进行编辑限制,以达到保护形状某些属性的目的。
本篇博文,旨在介绍使用我司免费产品,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");
}
}
}