このステップバイステップのチュートリアルでは、PowerPointがインストールされていないC#のPowerPoint Presentationにドラフト透かしを挿入する方法を詳しく説明します。 PPTX形式は、現在PowerPointプレゼンテーションで広く使用されている形式ですが、この例は、機密透かしPowerPointを追加するためのレガシーPPTにも適用できます。
C#でPowerPointに機密透かしを追加する手順
- NuGet.orgからAspose.Slides for .NETパッケージを取得します
- Aspose.Slides名前空間を使用して、プレゼンテーションを読み込み、透かしを挿入します
- SetLicenseメソッドを使用してライセンスを設定する
- PowerPointプレゼンテーションをロードして、Presentation Classオブジェクトに透かしを挿入します
- プレゼンテーション内のMaster Slide/sにアクセスして反復する
- マスタースライドごとに、オートシェイプを追加します
- 追加された図形の機密ドラフトテキストを挿入しますTextFrame
- 図形とテキストのプロパティをフォーマットします
- 透かしを保護するために、追加された形状にロックを適用します
- 透かし入りのプレゼンテーションを保存する
以前、別のハウツートピックでC#.NETでPowerPointプレゼンテーションを保護する方法を調べました。ただし、このトピックでは、C#のPowerPointに機密透かしを追加する手順について説明します。この機能を使用するためにMicrosoftPowerPointまたはInteropに依存しておらず、すべてのプラットフォームでこのコードをシームレスに実行できます。
何よりも、シェイプロック機能はAspose.Slidesが提供する独自の機能であり、PowerPointでは一般公開されていません。ロック機能を使用してそれを透かしの形に適用し、PowerPointでそれを変更または削除することを禁止することにより、プレゼンテーションの知的財産権を保護できます。
相互運用機能なしでC#のPowerPointにドラフト透かしを追加するコード
using System; | |
using Aspose.Slides; | |
using Aspose.Slides.Export; | |
namespace WatermarkPPTXinC_Sharp | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
string PathForWatermarkPptFile = @".\"; | |
//Loading the product license | |
License license = new License(); | |
license.SetLicense(PathForWatermarkPptFile+"Conholdate.Total.Product.Family.lic"); | |
//Load the presentation to insert watermark | |
Presentation WatermarkPptxPresentation = new Presentation(PathForWatermarkPptFile + "Draft.pptx"); | |
//Accessing the master slides for adding watermark | |
foreach (IMasterSlide masterSlide in WatermarkPptxPresentation.Masters) | |
{ | |
//Adding a Ppt watermark shape | |
IAutoShape PptxWatermark = masterSlide.Shapes.AddAutoShape(ShapeType.Rectangle, | |
WatermarkPptxPresentation.SlideSize.Size.Width / 2 - 50, | |
WatermarkPptxPresentation.SlideSize.Size.Height / 2 - 50, | |
200, 50); | |
//Setting rotation angle and fill type of the shape | |
PptxWatermark.Rotation = 325; | |
PptxWatermark.FillFormat.FillType = FillType.NoFill; | |
//Adding Text frame with watermark text | |
ITextFrame WatermarkText = PptxWatermark.AddTextFrame("Confidential Draft"); | |
//Setting textual properties of the watermark text | |
IPortionFormat WatermarkTextFormat = WatermarkText.Paragraphs[0].Portions[0].PortionFormat; | |
WatermarkTextFormat.FontBold = NullableBool.True; | |
WatermarkTextFormat.FontItalic = NullableBool.True; | |
WatermarkTextFormat.FontHeight = 20; | |
WatermarkTextFormat.FillFormat.FillType = FillType.Solid; | |
WatermarkTextFormat.FillFormat.SolidFillColor.Color = System.Drawing.Color.Red; | |
//Locking Pptx watermark shape to be uneditable in PowerPoint | |
PptxWatermark.AutoShapeLock.TextLocked=true; | |
PptxWatermark.AutoShapeLock.SelectLocked = true; | |
PptxWatermark.AutoShapeLock.PositionLocked = true; | |
} | |
//Saving the watermark PowerPoint presentation file | |
WatermarkPptxPresentation.Save(PathForWatermarkPptFile+"WatermarkPresentation.pptx", | |
SaveFormat.Pptx); | |
} | |
} | |
} |
この例は、ASP.NET Webアプリケーション、Windowsフォームアプリケーション、またはコンソールベースのいずれであっても、C#コードを使用する任意の.NETアプリケーション環境で使用できます。ローカルの作業マシンまたは.NETFrameworkがインストールされている任意のサーバーで使用することもできます。