التلاعب بالأشكال

البحث عن شكل في الشريحة

سيتناول هذا الموضوع تقنية بسيطة لتسهيل عمل المطورين في العثور على شكل معين في شريحة دون استخدام معرّفه الداخلي. من المهم معرفة أن ملفات PowerPoint Presentation ليس لها أي وسيلة لتعريف الأشكال على الشريحة باستثناء معرف فريد داخلي. يبدو أنه من الصعب على المطورين العثور على شكل باستخدام معرفه الفريد الداخلي. جميع الأشكال المضافة إلى الشرائح تحتوي على نص بديل. نقترح على المطورين استخدام النص البديل للعثور على شكل محدد. يمكنك استخدام MS PowerPoint لتحديد النص البديل للأشياء التي تخطط لتغييرها في المستقبل.

بعد تعيين النص البديل لأي شكل مرغوب، يمكنك فتح تلك العروض التقديمية باستخدام Aspose.Slides لـ 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 لـ 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 لـ C++ يتيح للمطورين إزالة أي شكل. لإزالة الشكل من أي شريحة، يرجى اتباع الخطوات التالية:

  1. إنشاء مثيل من Presentation class.
  2. الوصول إلى الشريحة الأولى.
  3. العثور على الشكل ذو النص البديل المحدد.
  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 لـ C++ يتيح للمطورين إخفاء أي شكل. لإخفاء الشكل من أي شريحة، يرجى اتباع الخطوات التالية:

  1. إنشاء مثيل من Presentation class.
  2. الوصول إلى الشريحة الأولى.
  3. العثور على الشكل ذو النص البديل المحدد.
  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 لـ C++ يتيح للمطورين إعادة ترتيب الأشكال. إعادة ترتيب الشكل تحدد أي شكل في المقدمة أو أي شكل في الخلف. لإعادة ترتيب الشكل من أي شريحة، يرجى اتباع الخطوات التالية:

  1. إنشاء مثيل من Presentation class.
  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);

الحصول على معرف الشكل Interop

Aspose.Slides لـ C++ يتيح للمطورين الحصول على معرّف شكل فريد في نطاق الشريحة في مقابل خاصية UniqueId، التي تسمح بالحصول على معرّف فريد في نطاق العرض التقديمي. تمت إضافة خاصية OfficeInteropShapeId إلى واجهات IShape وفئة Shape على التوالي. القيمة التي يتم إرجاعها بواسطة خاصية OfficeInteropShapeId تتوافق مع قيمة المعرف لجسم Microsoft.Office.Interop.PowerPoint.Shape. أدناه يتم إعطاء مثال على الكود.

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);

تعيين خاصية النص البديل

Aspose.Slides لـ C++ يتيح للمطورين تعيين النص البديل لأي شكل. لتعيين النص البديل لشكل، يرجى اتباع الخطوات التالية:

  1. إنشاء مثيل من Presentation class.
  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/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 لـ 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 لـ 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());