形状の操作

スライド内の形状を見つける

このトピックでは、開発者が内部IDを使用せずにスライド上の特定の形状を見つけやすくするためのシンプルな手法を説明します。PowerPointプレゼンテーションファイルは、内部のユニークIDを除いてスライド上の形状を特定する方法がないことを知っておくことが重要です。開発者が内部ユニークIDを使用して形状を見つけるのは難しいようです。スライドに追加されたすべての形状には、いくつかの代替テキストがあります。特定の形状を見つけるために代替テキストを使用することを開発者に推奨します。今後変更する予定のオブジェクトの代替テキストを定義するためにMS PowerPointを使用できます。

希望する形状の代替テキストを設定した後、Aspose.Slides for C++を使用してそのプレゼンテーションを開き、スライドに追加されたすべての形状を反復処理できます。各反復の間に、形状の代替テキストを確認し、一致する代替テキストを持つ形状が必要な形状になります。この技術をより良く示すために、特定の形状をスライド内で見つけてその形状を単に返すメソッドFindShapeを作成しました。

For complete examples and data files, please go to https://github.com/aspose-slides/Aspose.Slides-for-C
// The path to the documents directory.
const String outPath = u"../out/FindShapeInSlide_out.pptx";
const String templatePath = u"../templates/FindingShapeInSlide.pptx";
// Load the desired the presentation
SharedPtr<Presentation> pres = MakeObject<Presentation>(templatePath);
// Access first slide
SharedPtr<ISlide> slide = pres->get_Slides()->idx_get(0);
SharedPtr<IShape> shape = Aspose::Slides::Util::SlideUtil::FindShape(slide, u"Shape1");
if (shape != nullptr)
{
Console::WriteLine(u"Shape Name: " + shape->get_Name());
Console::WriteLine(u"Shape Alternative Tex: " + shape->get_AlternativeText());
}

形状を複製する

Aspose.Slides for C++を使用してスライドに形状を複製するには:

  1. Presentation クラスのインスタンスを作成します。
  2. インデックスを使用してスライドの参照を取得します。
  3. ソーススライドの形状コレクションにアクセスします。
  4. プレゼンテーションに新しいスライドを追加します。
  5. ソーススライドの形状コレクションから新しいスライドに形状を複製します。
  6. 修正されたプレゼンテーションをPPTXファイルとして保存します。

以下の例では、グループ形状をスライドに追加します。

For complete examples and data files, please go to https://github.com/aspose-slides/Aspose.Slides-for-C
// The path to the documents directory.
const String outPath = u"../out/CloneShapes_out.pptx";
const String templatePath = u"../templates/Source Frame.pptx";
// Load the desired the presentation
SharedPtr<Presentation> pres = MakeObject<Presentation>(templatePath);
// Access first slide
SharedPtr<ISlide> slide = pres->get_Slides()->idx_get(0);
// Accessing shapes collection for selected slide
SharedPtr<IShapeCollection> sourceShapes = slide->get_Shapes();
SharedPtr<ILayoutSlide> blankLayout = pres->get_Masters()->idx_get(0)->get_LayoutSlides()->GetByType(SlideLayoutType::Blank);
SharedPtr<ISlide> destSlide = pres->get_Slides()->AddEmptySlide(blankLayout);
SharedPtr<IShapeCollection> destShapes = destSlide->get_Shapes();
destShapes->AddClone(sourceShapes->idx_get(1), 50, 150 + sourceShapes->idx_get(0)->get_Height());
destShapes->AddClone(sourceShapes->idx_get(2));
destShapes->InsertClone(0, sourceShapes->idx_get(0), 50, 150);
//Write the PPTX to Disk
pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx);

形状を削除する

Aspose.Slides for C++では、開発者が任意の形状を削除できます。任意のスライドから形状を削除するには、以下の手順に従ってください:

  1. Presentation クラスのインスタンスを作成します。
  2. 最初のスライドにアクセスします。
  3. 特定のAlternativeTextを持つ形状を見つけます。
  4. 形状を削除します。
  5. ファイルをディスクに保存します。
For complete examples and data files, please go to https://github.com/aspose-slides/Aspose.Slides-for-C
// The path to the documents directory.
const String outPath = u"../out/RemoveShape_out.pptx";
const String templatePath = u"../templates/ConnectorLineAngle.pptx";
// Load the desired the presentation
SharedPtr<Presentation> pres = MakeObject<Presentation>();
// Access first slide
SharedPtr<ISlide> slide = pres->get_Slides()->idx_get(0);
// Accessing shapes collection for selected slide
SharedPtr<IShapeCollection> shapes = slide->get_Shapes();
// Now create effect "PathFootball" for existing shape from scratch.
SharedPtr<IAutoShape> autoShape1 = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 50, 40, 150, 50);
SharedPtr<IAutoShape> autoShape2 = slide->get_Shapes()->AddAutoShape(ShapeType::Moon, 160, 40, 150, 50);
String alttext = u"User Defined";
int iCount = slide->get_Shapes()->get_Count();
for (int i = 0; i < iCount; i++)
{
// Accessing the added shape
SharedPtr<Shape> ashape = DynamicCast<Aspose::Slides::Shape>(slide->get_Shapes()->idx_get(i));
if (String::Compare(ashape->get_AlternativeText(), alttext, StringComparison::Ordinal) == 0)
{
slide->get_Shapes()->Remove(ashape);
}
}
//Write the PPTX to Disk
pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx);

形状を隠す

Aspose.Slides for C++では、開発者が任意の形状を隠すことができます。任意のスライドから形状を隠すには、以下の手順に従ってください:

  1. Presentation クラスのインスタンスを作成します。
  2. 最初のスライドにアクセスします。
  3. 特定のAlternativeTextを持つ形状を見つけます。
  4. 形状を隠します。
  5. ファイルをディスクに保存します。
For complete examples and data files, please go to https://github.com/aspose-slides/Aspose.Slides-for-C
// The path to the documents directory.
const String outPath = u"../out/Hidingshapes_out.pptx";
const String templatePath = u"../templates/ConnectorLineAngle.pptx";
// Load the desired the presentation
SharedPtr<Presentation> pres = MakeObject<Presentation>();
// Access first slide
SharedPtr<ISlide> slide = pres->get_Slides()->idx_get(0);
// Accessing shapes collection for selected slide
SharedPtr<IShapeCollection> shapes = slide->get_Shapes();
// Now create effect "PathFootball" for existing shape from scratch.
SharedPtr<IAutoShape> autoShape1 = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 50, 40, 150, 50);
SharedPtr<IAutoShape> autoShape2 = slide->get_Shapes()->AddAutoShape(ShapeType::Moon, 160, 40, 150, 50);
String alttext = u"User Defined";
int iCount = slide->get_Shapes()->get_Count();
for (int i = 0; i < iCount; i++)
{
// Accessing the added shape
SharedPtr<AutoShape> ashape = DynamicCast<Aspose::Slides::AutoShape>(slide->get_Shapes()->idx_get(i));
if (String::Compare(ashape->get_AlternativeText(), alttext, StringComparison::Ordinal) == 0)
{
ashape->set_Hidden(true);
}
}
//Write the PPTX to Disk
pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx);

形状の順序を変更する

Aspose.Slides for C++では、開発者が形状の順序を変更できます。形状の順序を変更することで、どの形状が前面にあるか、どの形状が背面にあるかを指定します。任意のスライドから形状の順序を変更するには、以下の手順に従ってください:

  1. Presentation クラスのインスタンスを作成します。
  2. 最初のスライドにアクセスします。
  3. 形状を追加します。
  4. 形状のテキストフレームにテキストを追加します。
  5. 同じ座標を持つ別の形状を追加します。
  6. 形状の順序を変更します。
  7. ファイルをディスクに保存します。
For complete examples and data files, please go to https://github.com/aspose-slides/Aspose.Slides-for-C
// The path to the documents directory.
const String outPath = u"../out/ChangeShapeOrder_out.pptx";
const String templatePath = u"../templates/HelloWorld.pptx";
// Load the desired the presentation
SharedPtr<Presentation> pres = MakeObject<Presentation>(templatePath);
// Access first slide
SharedPtr<ISlide> slide = pres->get_Slides()->idx_get(0);
// Add an AutoShape of Rectangle type
SharedPtr<IAutoShape> ashp = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 150, 75, 150, 50);
ashp->get_FillFormat()->set_FillType(FillType::NoFill);
// Add TextFrame to the Rectangle
ashp->AddTextFrame(u" ");
// Accessing the text frame
SharedPtr<ITextFrame> txtFrame = ashp->get_TextFrame();
// Create the Paragraph object for text frame
SharedPtr<IParagraph> paragraph = txtFrame->get_Paragraphs()->idx_get(0);
// Create Portion object for paragraph
SharedPtr<IPortion> portion = paragraph->get_Portions()->idx_get(0);
portion->set_Text(u"Watermark Text Watermark Text Watermark Text");
//Adding another shape
SharedPtr<IAutoShape> ashape2 = slide->get_Shapes()->AddAutoShape(ShapeType::Triangle, 200, 365, 400, 150);
//Reorder shape
slide->get_Shapes()->Reorder(2, ashape2);
// Save PPTX to Disk
pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx);

インタープロップ形状IDを取得する

Aspose.Slides for C++は、UniqueIdプロパティとは対照的に、スライドスコープ内でユニークな形状識別子を取得することを可能にします。これはプレゼンテーションスコープ内でユニークな識別子を取得できます。OfficeInteropShapeIdプロパティは、IShapeインターフェースおよびShapeクラスに追加されました。OfficeInteropShapeIdプロパティによって返される値は、Microsoft.Office.Interop.PowerPoint.ShapeオブジェクトのIdの値に対応します。以下はサンプルコードです。

For complete examples and data files, please go to https://github.com/aspose-slides/Aspose.Slides-for-C
// The path to the documents directory.
const String outPath = u"../out/FindShapeInSlide_out.pptx";
const String templatePath = u"../templates/FindingShapeInSlide.pptx";
// Load the desired the presentation
SharedPtr<Presentation> pres = MakeObject<Presentation>(templatePath);
long officeInteropShapeId = 0;
// Getting unique shape identifier in slide scope
// officeInteropShapeId = pres->get_Slides()->idx_get(0)->get_Shapes()->idx_get(0)->get_OfficeInteropShapeId();
Console::WriteLine(u"Office Interop Shape ID: " + officeInteropShapeId);

AlternativeTextプロパティを設定する

Aspose.Slides for C++は、開発者が任意の形状のAlternateTextを設定できるようにします。形状のAlternateTextを設定するには、以下の手順に従ってください:

  1. Presentation クラスのインスタンスを作成します。
  2. 最初のスライドにアクセスします。
  3. スライドに任意の形状を追加します。
  4. 新しく追加した形状で何らかの作業を行います。
  5. 形状を辿って形状を見つけます。
  6. AlternativeTextを設定します。
  7. ファイルをディスクに保存します。
For complete examples and data files, please go to https://github.com/aspose-slides/Aspose.Slides-for-C
// The path to the documents directory.
const String outPath = u"../out/SetAlternativeText_out.pptx";
const String templatePath = u"../templates/ConnectorLineAngle.pptx";
// Load the desired the presentation
SharedPtr<Presentation> pres = MakeObject<Presentation>();
// Access first slide
SharedPtr<ISlide> slide = pres->get_Slides()->idx_get(0);
// Accessing shapes collection for selected slide
SharedPtr<IShapeCollection> shapes = slide->get_Shapes();
// Now create effect "PathFootball" for existing shape from scratch.
SharedPtr<IAutoShape> autoShape1 = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 50, 40, 150, 50);
SharedPtr<IAutoShape> autoShape2 = slide->get_Shapes()->AddAutoShape(ShapeType::Moon, 160, 40, 150, 50);
String alttext = u"User Defined";
int iCount = slide->get_Shapes()->get_Count();
for (int i = 0; i < iCount; i++)
{
// Accessing the added shape
SharedPtr<IShape> shape = slide->get_Shapes()->idx_get(i);
SharedPtr<AutoShape> ashape = DynamicCast<Aspose::Slides::AutoShape>(shape);
if (ashape != nullptr)
{
ashape->set_AlternativeText (u"User Defined " +(i+1));
}
}
//Write the PPTX to Disk
pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx);

形状のレイアウト形式にアクセスする

Aspose.Slides for C++は、開発者が形状のレイアウト形式にアクセスできるようにします。この資料では、形状のFillFormatおよびLineFormatプロパティにアクセスする方法を示します。

以下はサンプルコードです。

// The path to the documents directory.
const String outPath = u"../templates/pres.pptx";
//Instantiate Presentation class that represents PPTX file
SharedPtr<Presentation> pres = MakeObject<Presentation>();
for (int x = 0; x < pres->get_LayoutSlides()->get_Count(); x++)
{
SharedPtr<IShapeCollection> shapeCollection = pres->get_LayoutSlides()->idx_get(x)->get_Shapes();
for (int i = 0; i < shapeCollection->get_Count(); i++) {
SharedPtr<IShape> shape = shapeCollection->idx_get(i);
System::Console::WriteLine(shape->get_FillFormat());
}
for (int j = 0; j < shapeCollection->get_Count(); j++) {
SharedPtr<IShape> shape = shapeCollection->idx_get(j);
System::Console::WriteLine(shape->get_LineFormat());
}
}

形状をSVGとしてレンダリングする

現在、Aspose.Slides for C++は、形状をSVGとしてレンダリングすることをサポートしています。WriteAsSvgメソッド(およびそのオーバーロード)がShapeクラスとIShapeインターフェースに追加されました。このメソッドは、形状のコンテンツをSVGファイルとして保存できるようにします。以下のコードスニペットは、スライドの形状をSVGファイルにエクスポートする方法を示しています。

String outSvgFileName = u"SingleShape.svg";

auto pres = System::MakeObject<Presentation>(u"TestExportShapeToSvg.pptx");

auto stream = System::MakeObject<FileStream>(outSvgFileName, FileMode::Create, FileAccess::Write);
pres->get_Slides()->idx_get(0)->get_Shapes()->idx_get(0)->WriteAsSvg(stream);

形状の配置

Aspose.Slidesでは、形状をスライドのマージンに対して、または相互に配置できます。この目的のために、オーバーロードされたSlidesUtil.AlignShapes()メソッドが追加されました。ShapesAlignmentType列挙型は、可能な整列オプションを定義します。

例1

以下のソースコードは、インデックス1、2、4の形状をスライドの上部境界に沿って整列します。

SharedPtr<Presentation> pres = System::MakeObject<Presentation>(u"example.pptx");

SharedPtr<ISlide> slide = pres->get_Slides()->idx_get(0);
SharedPtr<IShape> shape1 = slide->get_Shapes()->idx_get(1);
SharedPtr<IShape> shape2 = slide->get_Shapes()->idx_get(2);
SharedPtr<IShape> shape3 = slide->get_Shapes()->idx_get(4);
SlideUtil::AlignShapes(ShapesAlignmentType::AlignTop, true, pres->get_Slides()->idx_get(0), 
System::MakeArray<int32_t>(
    {
        slide->get_Shapes()->IndexOf(shape1),
        slide->get_Shapes()->IndexOf(shape2),
        slide->get_Shapes()->IndexOf(shape3)
    }));

例2

以下の例は、形状の全コレクションをコレクション内の最も下にある形状に対して整列する方法を示しています。

SharedPtr<Presentation> pres = MakeObject<Presentation>(u"example.pptx");
SlideUtil::AlignShapes(ShapesAlignmentType::AlignBottom, false, pres->get_Slides()->idx_get(0)->get_Shapes());