C# REST API を使用して PowerPoint でカスタム図形を作成する

この記事に従って、C# REST API を使用して PowerPoint でカスタム シェイプを作成しますC# RESTful サービスを使用して、**PowerPoint 用のカスタム図形を自動的に作成および追加する方法を学習します。形状の作成、パラメータの設定、特定のスライドへの追加に関するすべての詳細を共有します。

前提条件

C# REST API を使用して PPT 形状を追加する手順

  1. SlidesApi オブジェクトを作成し、そこに図形を追加するためのソース プレゼンテーションをアップロードします。
  2. 変更するスライドの種類がマスター スライドであることを指定します
  3. 特定のプロパティを持つ新しい形状オブジェクトを作成する
  4. CreateSpecialSlideShape() メソッドを使用して、指定したスライドに新しい図形を追加します
  5. 新しい形状を含む更新されたプレゼンテーション ファイルをダウンロードします。

これらの手順では、C# REST API* を使用して *プレゼンテーション シェイプを生成する方法を説明します。 SlidesApi オブジェクトを作成し、プレゼンテーションをクラウド ストレージにアップロードし、変更するスライドの種類を指定して、必要なパラメーターを使用して Shape オブジェクトをインスタンス化します。マスター スライドに図形を追加し、新しい図形を含む更新されたプレゼンテーションをダウンロードします。

C# REST インターフェイスを使用して PPT の形状を追加するコード

using Aspose.Slides.Cloud.Sdk;
using Aspose.Slides.Cloud.Sdk.Model;
using System.IO;
namespace AsposeTestCodes
{
class Program
{
static void Main(string[] args) // For adding custom shape
{
SlidesApi slidesApi = new SlidesApi("API Key", "Secret");
// Upload the presentation file to Aspose cloud storage
FilesUploadResult uploadResult = slidesApi.UploadFile("SamplePresentation.pptx", new MemoryStream(File.ReadAllBytes("SamplePresentation.pptx")));//Source presentation
// Define the file name of the uploaded presentation
string fileName = "SamplePresentation.pptx";
// Specify that the slide type to modify is a Master Slide
SpecialSlideType slideType = SpecialSlideType.MasterSlide;
// Create a new shape object with specific properties
Shape dto = new Shape
{
X = 100, // X-coordinate for the position of the shape
Y = 100, // Y-coordinate for the position of the shape
Width = 500, // Width of the shape
Height = 200, // Height of the shape
ShapeType = GeometryShape.ShapeTypeEnum.Rectangle, // Define the shape type as a rectangle
Text = "New shape" // Text content to be displayed within the shape
};
// Add the new shape to the specified slide (master slide at index 1) and retrieve the created shape
Shape shape = (Shape)slidesApi.CreateSpecialSlideShape(fileName, 1, slideType, dto);
Stream stream = slidesApi.DownloadFile(fileName);
// Save the downloaded presentation file locally with a new name ("PresWithShape.pptx")
var fs = new FileStream("PresWithShape.pptx", FileMode.Create, FileAccess.Write); // Create a file stream for saving
stream.CopyTo(fs); // Copy the content of the downloaded file into the file stream
}
}
}

このコードは、C# RESTful サービス* を使用して *スライドのシェイプを処理する方法を示します。列挙子 GeometryShape.ShapeTypeEnum を使用して、Lines、Triangle、Rectangle、Diamond などの任意のタイプの図形を追加できます。このオプションは、LayoutSlide や NotesSlide などの他の特殊なタイプのスライドを選択するためにも使用できます。

この記事では、スライド上に図形を描く方法を学びました。画像を追加するには、記事 C# REST API を使用して PowerPoint に画像を追加する を参照してください。

 日本語