このシンプルで詳細なチュートリアルでは、PowerPointがインストールされていないC#を使用してPPTXPresentationに画像透かしを追加する方法を紹介します。現在、PPTXはPowerPointプレゼンテーションの一般的な形式ですが、この例をPPT形式にも使用して、プレゼンテーションの知的財産権を保護するために画像の透かしを追加することができます。
C#でPPTXプレゼンテーションに画像透かしを追加する手順
- NuGet.orgからAspose.Slides for .NETパッケージをダウンロードします
- Aspose.Slides名前空間を使用して、透かしをロードおよび追加します
- SetLicenseメソッドを使用してライセンスを設定する
- Presentation Classオブジェクトを使用して、プレゼンテーションをロードし、画像の透かしを追加します
- 透かし/ロゴ画像をプレゼンテーション画像コレクションにロードします
- プレゼンテーション内のMaster Slide/sにアクセスして反復する
- マスタースライドごとに、透かし画像を追加したPictureFrameを追加します
- 形状プロパティをフォーマットする
- 画像の透かしを保護するために、追加された形状にロックを適用します
- 透かし入りのプレゼンテーションを保存する
以前、別のハウツートピックでC#のPowerPointプレゼンテーションにドラフト透かしを挿入する方法を調べました。ただし、このトピックでは、C#のPowerPointプレゼンテーションに画像透かしを追加する手順について説明します。この機能を使用するためにMicrosoftPowerPointやInteropに依存する必要がなくなり、すべてのプラットフォームでシームレスにコードを実行できます。
とりわけ、Aspose.Slidesが提供する独自の形状ロック機能は、透かし画像を保護するためにPowerPointで公開されていません。ロック機能を使用し、それを画像の透かしの形状に適用して、PowerPointプレゼンテーションの変更や焼き戻しを禁止することにより、プレゼンテーションの知的財産権を保護できます。
相互運用機能なしでC#のPowerPointに画像透かしを追加するコード
using System; | |
using System.Drawing; | |
using Aspose.Slides; | |
using Aspose.Slides.Export; | |
namespace SlidesWatermark | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
string PathForWatermarkPptFile = @"Y:\Downloads\"; | |
License license = new License(); | |
license.SetLicense(PathForWatermarkImageFile + "Conholdate.Total.Product.Family.lic"); | |
//Load the presentation to insert watermark | |
Presentation WatermarkPptxPresentation = new Presentation(PathForWatermarkPptFile + "PictureWatermark.pptx"); | |
// Loading watermark image to add in PPTX | |
System.Drawing.Image WatermarkLogo = (System.Drawing.Image)new Bitmap("Picture Watermark Logo.jpg"); | |
IPPImage WatermarkImage = WatermarkPptxPresentation.Images.AddImage(WatermarkLogo); | |
//Accessing the master slides for adding watermark image | |
foreach (IMasterSlide masterSlide in WatermarkPptxPresentation.Masters) | |
{ | |
//Adding a Ppt watermark shape for logo image | |
IPictureFrame PptxWatermark = masterSlide.Shapes.AddPictureFrame(ShapeType.Rectangle,0, 0, | |
200, 50, WatermarkImage); | |
//Set the rotation angle of the shape | |
PptxWatermark.Rotation = 325; | |
//Lock Pptx watermark image shape for protection in PowerPoint | |
PptxWatermark.ShapeLock.SizeLocked = true; | |
PptxWatermark.ShapeLock.SelectLocked = true; | |
PptxWatermark.ShapeLock.PositionLocked = true; | |
} | |
//Saving the image watermark PPTX presentation file | |
WatermarkPptxPresentation.Save(PathForWatermarkPptFile + "ImageWatermarkedPresentation.pptx", | |
SaveFormat.Pptx); | |
} | |
} | |
} |
この例は、ASP.NET Webアプリケーション、Windowsフォームアプリケーション、コンソールベースのアプリケーションなど、C#を使用するすべての.NETアプリケーション環境に適しています。ローカルの作業マシンまたは.NETFrameworkがインストールされている任意のサーバーで使用できます。