時々、あらかじめ作成された画像ファイルのセットから PowerPoint 文書を作成する必要があります。 たとえば、会社から素敵なチラシが提供され、それらを1つの PowerPoint 文書にまとめて、各画像を整然と表示する必要があります。この記事では、Spire.Presentation for .NET を使用して画像ファイル(PNG、JPG、BMP など)を PowerPoint に変換する方法について説明します。 画像を PowerPoint の背景に変換する 画像を PowerPoint の図形に変換する 画像をカスタマスライドサイズの PowerPoint に変換する Spire.Presentation for .NET をインストールします まず、Spire.Presentation for .NET パッケージに含まれている DLL ファイルを .NET プロジェクトの参照として追加する必要があります。DLL ファイルは、このリンクからダウンロードするか、NuGet を介してインストールできます。 PM> Install-Package Spire.Presentation 画像を PowerPoint の背景に変換する PowerPoint の各スライドの背景として画像が変換される場合、それらは移動や拡大縮小ができません。以下は、Spire.Presentation for .NET を使用して、画像を PowerPoint ファイルに背景画像として変換する手順です。 Presentation オブジェクトを作成します。 スライドのサイズの種類を Sreen16x9 に設定します。 フォルダーから画像のパスを取得し、文字列配列に保存します。 画像をループします。 特定の画像を取得し、Presentation.Images.Append() メソッドを使用して画像コレクションに追加します。 Presentation.Slides.Append() メソッドを使用して、ドキュメントにスライドを追加します。 ISlide.SlideBackground オブジェクトの下にあるプロパティを使用して、画像をスライドの背景として設定します。 Presentation.SaveToFile() メソッドを使用して、結果文書を保存します。 C# using Spire.Presentation; using Spire.Presentation.Drawing; using System.Drawing; using System.IO; namespace ConvertImageToBackground { class Program { static void Main(string[] args) { //Presentationオブジェクトを作成する Presentation presentation = new Presentation(); //スライドのサイズの種類を設定する presentation.SlideSize.Type = SlideSizeType.Screen16x9; //デフォルトのスライドを削除する presentation.Slides.RemoveAt(0); //ファイルパスを文字列配列で取得する string[] picFiles = Directory.GetFiles(@"C:UsersAdministratorDesktopImages"); //画像をループする for (int i = 0; i < picFiles.Length; i++) { //スライドを追加する ISlide slide = presentation.Slides.Append(); //特定の画像を取得する string imageFile = picFiles[i]; Image image = Image.FromFile(imageFile); //画像コレクションに追加する IImageData imageData = presentation.Images.Append(image); //画像をスライドの背景画像として設定する slide.SlideBackground.Type = BackgroundType.Custom; slide.SlideBackground.Fill.FillType = FillFormatType.Picture; slide.SlideBackground.Fill.PictureFill.FillType = PictureFillType.Stretch; slide.SlideBackground.Fill.PictureFill.Picture.EmbedImage = imageData; } //結果文書を保存する presentation.SaveToFile("ImagesToBackground.pptx", FileFormat.Pptx2013); } } } 画像を PowerPoint の図形に変換する PowerPoint ファイルで画像を移動可能かつサイズ変更可能にしたい場合は、それらを図形として変換することができます。以下は、Spire.Presentation for .NET を使用して PowerPoint 文書内の画像を図形に変換する手順です。 Presentation オブジェクトを作成します。 スライドのサイズの種類を Sreen16x9 に設定します。 フォルダーから画像のパスを取得し、文字列配列に保存します。 画像をループします。 特定の画像を取得し、Presentation.Images.Append() メソッドを使用して画像コレクションに追加します。 Presentation.Slides.Append() メソッドを使用して、ドキュメントにスライドを追加します。 ISlide.Shapes.AppendShape() メソッドを使用して、スライドと同じサイズの図形を追加します。 IAutoShape.Fill オブジェクトの下にあるプロパティを使用して、図形を画像で塗りつぶします。 Presentation.SaveToFile() メソッドを使用して、結果文書を保存します。 C# using Spire.Presentation; using Spire.Presentation.Drawing; using System.Drawing; using System.IO; namespace ConvertImageToShape { class Program { static void Main(string[] args) { //Presentationオブジェクトを作成する Presentation presentation = new Presentation(); //スライドのサイズの種類を設定する presentation.SlideSize.Type = SlideSizeType.Screen16x9; //デフォルトのスライドを削除する presentation.Slides.RemoveAt(0); //ファイルパスを文字列配列で取得する string[] picFiles = Directory.GetFiles(@"C:UsersAdministratorDesktopImages"); //画像をループする for (int i = 0; i < picFiles.Length; i++) { //スライドを追加する ISlide slide = presentation.Slides.Append(); //特定の画像を取得する string imageFile = picFiles[i]; Image image = Image.FromFile(imageFile); //画像コレクションに追加する IImageData imageData = presentation.Images.Append(image); //スライドと同じサイズの図形を追加する IAutoShape shape = slide.Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(new PointF(0, 0), presentation.SlideSize.Size)); //図形を画像で塗りつぶす shape.Line.FillType = FillFormatType.None; shape.Fill.FillType = FillFormatType.Picture; shape.Fill.PictureFill.FillType = PictureFillType.Stretch; shape.Fill.PictureFill.Picture.EmbedImage = imageData; } //結果文書を保存する presentation.SaveToFile("ImageToShape.pptx", FileFormat.Pptx2013); } } } 画像をカスタマスライドサイズの PowerPoint に変換する もし画像のアスペクト比が 16:9 ではない場合や、標準のスライドサイズでない場合は、画像の実際のサイズに基づいてスライドを作成することができます。これにより、画像が過度に伸びたり圧縮されたりするのを防ぐことができます。以下は、Spire.Presentation for .NET を使用してカスタマイズされたスライドサイズで PowerPoint 文書に画像を変換する手順です。 Presentation オブジェクトを作成します。 PdfUnitConvertor オブジェクトを作成し、ピクセルをポイントに変換するために使用します。 フォルダーから画像のパスを取得し、文字列配列に保存します。 画像をループします。 特定の画像を取得し、Presentation.Images.Append() メソッドを使用して画像コレクションに追加します。 画像の幅と高さを取得し、ポイントに変換します。 Presentation.SlideSize.Size プロパティを使用して、スライドサイズを画像のサイズに基づいて設定します。 Presentation.Slides.Append() メソッドを使用して、ドキュメントにスライドを追加します。 ISlide.SlideBackground オブジェクトの下にあるプロパティを使用して、画像をスライドの背景画像として設定します。 Presentation.SaveToFile() メソッドを使用して、結果文書を保存します。 C# using Spire.Pdf.Graphics; using Spire.Presentation; using Spire.Presentation.Drawing; using System.Drawing; using System.IO; namespace CustomSlideSize { class Program { static void Main(string[] args) { //Presentationオブジェクトを作成する Presentation presentation = new Presentation(); //デフォルトのスライドを削除する presentation.Slides.RemoveAt(0); //ファイルパスを文字列配列で取得する string[] picFiles = Directory.GetFiles(@"C:UsersAdministratorDesktopImage"); //PdfUnitConvertor オブジェクトを作成する PdfUnitConvertor convertor = new PdfUnitConvertor(); //画像をループする for (int i = 0; i < picFiles.Length; i++) { //特定の画像を取得する string imageFile = picFiles[i]; Image image = Image.FromFile(imageFile); //画像コレクションに追加する IImageData imageData = presentation.Images.Append(image); //画像の高さと幅をピクセル単位で取得する int height = imageData.Height; int width = imageData.Width; //ピクセルをポイントに変換する float widthPoint = convertor.ConvertUnits(width, PdfGraphicsUnit.Pixel, PdfGraphicsUnit.Point); float heightPoint= convertor.ConvertUnits(height, PdfGraphicsUnit.Pixel, PdfGraphicsUnit.Point); //スライドのサイズを設定する presentation.SlideSize.Size = new SizeF(widthPoint, heightPoint); //スライドを追加する ISlide slide = presentation.Slides.Append(); //画像をスライドの背景画像として設定する slide.SlideBackground.Type = BackgroundType.Custom; slide.SlideBackground.Fill.FillType = FillFormatType.Picture; slide.SlideBackground.Fill.PictureFill.FillType = PictureFillType.Stretch; slide.SlideBackground.Fill.PictureFill.Picture.EmbedImage = imageData; } //結果文書を保存する presentation.SaveToFile("CustomizeSlideSize.pptx", FileFormat.Pptx2013); } } } 一時ライセンスを申請する 結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。 にお問い合わせ、30 日間有効な一時ライセンスを取得してください。