在 C++ 中格式化 PowerPoint 形狀
簡介
透過調整 PowerPoint 中的形狀輪廓、填色和效果來格式化形狀。Aspose.Slides for C++ 讓您能以程式方式控制這些設定,包括透明度、旋轉和 3D 格式設定。

Aspose.Slides for C++ 提供介面和方法,讓您使用 PowerPoint 中相同的選項來格式化形狀。
格式線條
使用IShape::get_LineFormat 來控制形狀的輪廓。此範例建立一個未填色、藍色虛線輪廓的矩形。ILineFormat::set_Style 會選取粗細交錯的合成線,而ILineFormat::set_Width 將寬度設定為 7 點,ILineFormat::set_DashStyle 則選擇虛線樣式。
#include <DOM/FillType.h>
#include <DOM/IAutoShape.h>
#include <DOM/IColorFormat.h>
#include <DOM/IFillFormat.h>
#include <DOM/ILineFillFormat.h>
#include <DOM/ILineFormat.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/LineDashStyle.h>
#include <DOM/LineStyle.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <Export/SaveFormat.h>
#include <drawing/color.h>
#include <system/smart_ptr.h>
using namespace Aspose::Slides;
using namespace System;
using namespace Aspose::Slides::Export;
using namespace System::Drawing;
auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto shape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 50, 50, 150, 75);
shape->get_FillFormat()->set_FillType(FillType::NoFill);
shape->get_LineFormat()->set_Style(LineStyle::ThickThin);
shape->get_LineFormat()->set_Width(7);
shape->get_LineFormat()->set_DashStyle(LineDashStyle::Dash);
shape->get_LineFormat()->get_FillFormat()->set_FillType(FillType::Solid);
shape->get_LineFormat()->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_Blue());
presentation->Save(u"formatted_lines.pptx", SaveFormat::Pptx);
presentation->Dispose();
結果:

設定自訂線條虛線模式
當預定義的線條樣式無法提供所需的虛線長度與間距(例如品牌輪廓或技術圖表)時,請使用ILineFormat::set_CustomDashPattern。指派模式會自動將虛線樣式變更為LineDashStyle::Custom。
陣列項目在虛線與間距長度之間交替,從虛線開始。每個值都是線寬的倍數:寬度為 3 點時,{ 4, 2 } 會產生 12 點的虛線,接著是 6 點的間距。使用成對的有限、非負值來定義完整的虛線‑間距段落。範例使用正值,使每段虛線與間距皆可見。
此範例建立五條相同長度且端點為平直帽的藍色線條。前三條在寬度為 3 點時比較不同模式;第四條以兩倍寬度重複第一種模式,虛線與間距長度皆加倍;第五條則從自訂模式切換為實線。
#include <DOM/FillType.h>
#include <DOM/IAutoShape.h>
#include <DOM/IColorFormat.h>
#include <DOM/IFillFormat.h>
#include <DOM/ILineFillFormat.h>
#include <DOM/ILineFormat.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/ISlideSize.h>
#include <DOM/LineArrowheadStyle.h>
#include <DOM/LineCapStyle.h>
#include <DOM/LineDashStyle.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <DOM/SlideSizeScaleType.h>
#include <Export/SaveFormat.h>
#include <drawing/color.h>
#include <system/smart_ptr.h>
#include <system/array.h>
#include <array>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
using namespace System;
using namespace System::Drawing;
auto presentation = MakeObject<Presentation>();
presentation->get_SlideSize()->SetSize(600, 300, SlideSizeScaleType::DoNotScale);
auto slide = presentation->get_Slide(0);
const auto patterns = std::array<ArrayPtr<float>, 5>{
MakeArray<float>({ 4, 2 }),
MakeArray<float>({ 1, 2 }),
MakeArray<float>({ 6, 2, 1, 2 }),
MakeArray<float>({ 4, 2 }),
MakeArray<float>({ 4, 2 })
};
for (auto i = 0; i < patterns.size(); i++)
{
auto line = slide->get_Shapes()->AddAutoShape(ShapeType::Line, 50, 50 + i * 50, 500, 0);
line->get_LineFormat()->set_Width(i == 3 ? 6 : 3);
line->get_LineFormat()->get_FillFormat()->set_FillType(FillType::Solid);
line->get_LineFormat()->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_DarkBlue());
line->get_LineFormat()->set_CapStyle(LineCapStyle::Flat);
line->get_LineFormat()->set_BeginArrowheadStyle(LineArrowheadStyle::None);
line->get_LineFormat()->set_EndArrowheadStyle(LineArrowheadStyle::None);
line->get_LineFormat()->set_CustomDashPattern(patterns[i]);
}
// Return the last line to a solid style.
slide->get_Shape(4)->get_LineFormat()->set_DashStyle(LineDashStyle::Solid);
presentation->Save(u"custom_dash_patterns.pptx", SaveFormat::Pptx);
presentation->Dispose();
若要改回預定義的虛線樣式,請將LineDashStyle::Dash 傳遞給ILineFormat::set_DashStyle。所選樣式會控制線條的外觀。
PowerPoint 中從上到下的結果:{4, 2}, 3 pt;{1, 2}, 3 pt;{6, 2, 1, 2}, 3 pt;{4, 2}, 6 pt;以及 Solid, 3 pt。

設定線條端點樣式
使用ILineFormat::set_CapStyle 來設定開放線條的兩端。LineCapStyle 列舉提供三種明確的端點樣式:
Flat:筆畫在端點處停止。Round:半圓形端點延伸至端點之外,長度為線寬的一半。Square:方形端點延伸至端點之外,長度為線寬的一半。
列舉亦包含 NotDefined,表示未明確指派端點樣式。
端點樣式不同於箭頭,箭頭會在端點加入符號;亦不同於接合,接合控制線段之間的拐角。此範例透過ILineFormat::set_BeginArrowheadStyle 與ILineFormat::set_EndArrowheadStyle 停用箭頭,僅比較端點形狀。
以下範例建立三條在其他屬性相同的水平線,寬度為 20 點。由上而下分別使用平直、圓形與方形端點。它們的端點具有相同的水平座標;圓形與方形端點會超出每個端點 10 點。
#include <DOM/FillType.h>
#include <DOM/IAutoShape.h>
#include <DOM/IColorFormat.h>
#include <DOM/IFillFormat.h>
#include <DOM/ILineFillFormat.h>
#include <DOM/ILineFormat.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/ISlideSize.h>
#include <DOM/LineArrowheadStyle.h>
#include <DOM/LineCapStyle.h>
#include <DOM/LineDashStyle.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <DOM/SlideSizeScaleType.h>
#include <Export/SaveFormat.h>
#include <drawing/color.h>
#include <system/smart_ptr.h>
#include <array>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
using namespace System;
using namespace System::Drawing;
auto presentation = MakeObject<Presentation>();
presentation->get_SlideSize()->SetSize(500, 240, SlideSizeScaleType::DoNotScale);
auto slide = presentation->get_Slide(0);
const auto capStyles = std::array<LineCapStyle, 3>{ LineCapStyle::Flat, LineCapStyle::Round, LineCapStyle::Square };
for (auto i = 0; i < capStyles.size(); i++)
{
auto line = slide->get_Shapes()->AddAutoShape(ShapeType::Line, 100, 50 + i * 70, 300, 0);
line->get_LineFormat()->set_Width(20);
line->get_LineFormat()->get_FillFormat()->set_FillType(FillType::Solid);
line->get_LineFormat()->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_DarkBlue());
line->get_LineFormat()->set_DashStyle(LineDashStyle::Solid);
line->get_LineFormat()->set_CapStyle(capStyles[i]);
line->get_LineFormat()->set_BeginArrowheadStyle(LineArrowheadStyle::None);
line->get_LineFormat()->set_EndArrowheadStyle(LineArrowheadStyle::None);
}
presentation->Save(u"line_caps.pptx", SaveFormat::Pptx);
presentation->Dispose();
PowerPoint 中從上到下的結果:Flat、Round 與 Square。

將草圖效果套用於形狀線條
草圖效果會讓形狀線條看起來像手繪。使用IShape::get_LineFormat 取得線條設定,透過ILineFormat::get_SketchFormat 取得草圖設定,並使用ISketchFormat::set_SketchType 從LineSketchType 列舉中選取值。
以下 C++ 程式碼示範如何套用LineSketchType::Curved 效果、讀取明確指派的值,並使用LineSketchType::None 移除效果:
#include <DOM/IAutoShape.h>
#include <DOM/ILineFormat.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISketchFormat.h>
#include <DOM/ISlide.h>
#include <DOM/LineSketchType.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <system/console.h>
#include <system/smart_ptr.h>
using namespace Aspose::Slides;
using namespace System;
auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto shape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 50, 50, 200, 100);
auto sketchFormat = shape->get_LineFormat()->get_SketchFormat();
sketchFormat->set_SketchType(LineSketchType::Curved);
auto explicitSketchType = sketchFormat->get_SketchType();
Console::WriteLine(u"Explicit sketch type: {0}", explicitSketchType);
sketchFormat->set_SketchType(LineSketchType::None);
presentation->Dispose();
由ISketchFormat::get_SketchType 回傳的值代表直接指派給形狀的設定。若線條格式可從佈景主題、母片或佈局投影片繼承,請使用ILineFormat::GetEffective,取得ILineFormatEffectiveData::get_SketchFormat,再讀取ISketchFormatEffectiveData::get_SketchType。有效值反映經過繼承解析後實際套用的格式。本範例需要一個已有的簡報,且第一張投影片上至少有一個形狀,會同時列印兩個值:
#include <DOM/ILineFormat.h>
#include <DOM/ILineFormatEffectiveData.h>
#include <DOM/IShape.h>
#include <DOM/ISketchFormat.h>
#include <DOM/ISketchFormatEffectiveData.h>
#include <DOM/ISlide.h>
#include <DOM/LineSketchType.h>
#include <DOM/Presentation.h>
#include <system/console.h>
#include <system/smart_ptr.h>
using namespace Aspose::Slides;
using namespace System;
auto presentation = MakeObject<Presentation>(u"presentation.pptx");
auto shape = presentation->get_Slide(0)->get_Shape(0);
auto lineFormat = shape->get_LineFormat();
auto explicitSketchType = lineFormat->get_SketchFormat()->get_SketchType();
auto effectiveLineFormat = lineFormat->GetEffective();
auto effectiveSketchType = effectiveLineFormat->get_SketchFormat()->get_SketchType();
Console::WriteLine(u"Explicit sketch type: {0}", explicitSketchType);
Console::WriteLine(u"Effective sketch type: {0}", effectiveSketchType);
presentation->Dispose();
格式接合樣式
以下是三種接合類型選項:
- Round
- Miter
- Bevel
接合樣式控制兩個輪廓段在拐角處的相遇方式。使用 Round 取得圓角,Miter 取得尖角,Bevel 取得平緣角。

此範例建立三個黑色矩形,藍色輪廓寬度為 15 點。它分別將ILineFormat::set_JoinStyle 設為LineJoinStyle 的 miter、bevel 與 round 值,以比較拐角的繪製方式。
#include <DOM/FillType.h>
#include <DOM/IAutoShape.h>
#include <DOM/IColorFormat.h>
#include <DOM/IFillFormat.h>
#include <DOM/ILineFillFormat.h>
#include <DOM/ILineFormat.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/ITextFrame.h>
#include <DOM/LineJoinStyle.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <Export/SaveFormat.h>
#include <drawing/color.h>
#include <system/smart_ptr.h>
using namespace Aspose::Slides;
using namespace System;
using namespace Aspose::Slides::Export;
using namespace System::Drawing;
auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto miterShape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 20, 20, 150, 75);
auto bevelShape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 210, 20, 150, 75);
auto roundShape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 20, 135, 150, 75);
miterShape->get_FillFormat()->set_FillType(FillType::Solid);
miterShape->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_Black());
bevelShape->get_FillFormat()->set_FillType(FillType::Solid);
bevelShape->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_Black());
roundShape->get_FillFormat()->set_FillType(FillType::Solid);
roundShape->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_Black());
miterShape->get_LineFormat()->set_Width(15);
bevelShape->get_LineFormat()->set_Width(15);
roundShape->get_LineFormat()->set_Width(15);
miterShape->get_LineFormat()->get_FillFormat()->set_FillType(FillType::Solid);
miterShape->get_LineFormat()->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_Blue());
bevelShape->get_LineFormat()->get_FillFormat()->set_FillType(FillType::Solid);
bevelShape->get_LineFormat()->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_Blue());
roundShape->get_LineFormat()->get_FillFormat()->set_FillType(FillType::Solid);
roundShape->get_LineFormat()->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_Blue());
miterShape->get_LineFormat()->set_JoinStyle(LineJoinStyle::Miter);
bevelShape->get_LineFormat()->set_JoinStyle(LineJoinStyle::Bevel);
roundShape->get_LineFormat()->set_JoinStyle(LineJoinStyle::Round);
miterShape->get_TextFrame()->set_Text(u"Miter Join Style");
bevelShape->get_TextFrame()->set_Text(u"Bevel Join Style");
roundShape->get_TextFrame()->set_Text(u"Round Join Style");
presentation->Save(u"join_styles.pptx", SaveFormat::Pptx);
presentation->Dispose();
在尖角限制 Miter 接合
將ILineFormat::set_MiterLimit 與將ILineFormat::set_JoinStyle 設為LineJoinStyle::Miter 結合使用,可限制尖角可延伸的距離。
此限制是接合內外角之間距離與線寬的比率。例如,值 8 允許 miter 長度最高為線寬的八倍;它不是以點為單位的測量。如果拐角會超過此限制,PowerPoint 會在該拐角使用斜角接合。較大的限制允許更銳利的點。此設定僅影響 miter 接合,當拐角仍在兩個限制內時,變更不會產生可見效果。
本範例建立兩個相同、未填色的三角形,輪廓寬度為 20 點。左側三角形的限制為 2,右側為 8。每個三角形寬 80 點、高 160 點,使其上方拐角的 miter‑長度與線寬比約為 4.12。因此 PowerPoint 會將左側頂點切成斜角,而保留右側尖銳頂點。
#include <DOM/FillType.h>
#include <DOM/IAutoShape.h>
#include <DOM/IColorFormat.h>
#include <DOM/IFillFormat.h>
#include <DOM/ILineFillFormat.h>
#include <DOM/ILineFormat.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/ISlideSize.h>
#include <DOM/LineJoinStyle.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <DOM/SlideSizeScaleType.h>
#include <Export/SaveFormat.h>
#include <drawing/color.h>
#include <system/smart_ptr.h>
#include <array>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
using namespace System;
using namespace System::Drawing;
auto presentation = MakeObject<Presentation>();
presentation->get_SlideSize()->SetSize(500, 320, SlideSizeScaleType::DoNotScale);
auto slide = presentation->get_Slide(0);
const auto miterLimits = std::array<float, 2>{ 2.0f, 8.0f };
for (auto i = 0; i < miterLimits.size(); i++)
{
auto triangle = slide->get_Shapes()->AddAutoShape(ShapeType::Triangle, 90 + i * 240, 110, 80, 160);
triangle->get_FillFormat()->set_FillType(FillType::NoFill);
triangle->get_LineFormat()->set_Width(20);
triangle->get_LineFormat()->get_FillFormat()->set_FillType(FillType::Solid);
triangle->get_LineFormat()->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_DarkBlue());
triangle->get_LineFormat()->set_JoinStyle(LineJoinStyle::Miter);
triangle->get_LineFormat()->set_MiterLimit(miterLimits[i]);
}
presentation->Save(u"miter_limits.pptx", SaveFormat::Pptx);
presentation->Dispose();
PowerPoint 中的結果:左側 Miter limit 2,右側 miter limit 8。

漸層填色
漸層填色會在形狀內部混合顏色。此範例使用IFillFormat::get_GradientFormat 以 45 度角從紅色到紫色的線性漸層填滿橢圓形。透過IGradientStopCollection::Add 方法將顏色停止點分別放在 0 與 1(漸層的開始與結束)。先清除現有的停止點可確保僅使用這兩種顏色。
#include <DOM/FillType.h>
#include <DOM/GradientShape.h>
#include <DOM/IAutoShape.h>
#include <DOM/IColorFormat.h>
#include <DOM/IFillFormat.h>
#include <DOM/IGradientFormat.h>
#include <DOM/IGradientStopCollection.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <DOM/PresetColor.h>
#include <DOM/ShapeType.h>
#include <Export/SaveFormat.h>
#include <drawing/color.h>
#include <system/smart_ptr.h>
using namespace Aspose::Slides;
using namespace System;
using namespace Aspose::Slides::Export;
using namespace System::Drawing;
auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto shape = slide->get_Shapes()->AddAutoShape(ShapeType::Ellipse, 50, 50, 150, 75);
shape->get_FillFormat()->set_FillType(FillType::Gradient);
shape->get_FillFormat()->get_GradientFormat()->set_GradientShape(GradientShape::Linear);
shape->get_FillFormat()->get_GradientFormat()->set_LinearGradientAngle(45);
shape->get_FillFormat()->get_GradientFormat()->get_GradientStops()->Clear();
shape->get_FillFormat()->get_GradientFormat()->get_GradientStops()->Add(1.0f, PresetColor::Purple);
shape->get_FillFormat()->get_GradientFormat()->get_GradientStops()->Add(0.0f, PresetColor::Red);
presentation->Save(u"gradient_fill.pptx", SaveFormat::Pptx);
presentation->Dispose();
結果:

如果匯出圖像的漸層與 PowerPoint 中顯示不同,請參閱選擇漸層呈現樣式。您可以在不更改簡報內儲存的漸層填色的前提下,選擇呈現樣式。
設定線性漸層角度
使用IGradientFormat::set_LinearGradientAngle 以控制GradientShape::Linear 填色的方向。角度以度為單位,順時針遞增:0 為左至右,90 為上至下,180 為右至左,270 為下至上。支援小數角度。超出 [0, 360) 的有限角度會正規化為一圈,例如 -45 變為 315,360 變為 0。
此範例建立六個矩形,紅、綠、藍三種停止點分別位於 0、0.5 與 1 位置。關閉縮放,使角度比較不受矩形比例影響。從左至右的角度分別為第一列的 0、45、90,第二列的 135、180、270。
#include <DOM/FillType.h>
#include <DOM/GradientShape.h>
#include <DOM/IAutoShape.h>
#include <DOM/IFillFormat.h>
#include <DOM/IGradientFormat.h>
#include <DOM/IGradientStopCollection.h>
#include <DOM/ILineFillFormat.h>
#include <DOM/ILineFormat.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/ISlideSize.h>
#include <DOM/NullableBool.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <DOM/SlideSizeScaleType.h>
#include <Export/SaveFormat.h>
#include <drawing/color.h>
#include <system/smart_ptr.h>
#include <array>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
using namespace System;
using namespace System::Drawing;
auto presentation = MakeObject<Presentation>();
presentation->get_SlideSize()->SetSize(720, 400, SlideSizeScaleType::DoNotScale);
auto slide = presentation->get_Slide(0);
const auto angles = std::array<float, 6>{ 0, 45, 90, 135, 180, 270 };
for (auto i = 0; i < angles.size(); i++)
{
auto x = 30 + (i % 3) * 230;
auto y = 30 + (i / 3) * 190;
auto shape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, x, y, 200, 150);
shape->get_LineFormat()->get_FillFormat()->set_FillType(FillType::NoFill);
shape->get_FillFormat()->set_FillType(FillType::Gradient);
auto gradient = shape->get_FillFormat()->get_GradientFormat();
gradient->set_GradientShape(GradientShape::Linear);
gradient->get_GradientStops()->Clear();
gradient->get_GradientStops()->Add(0.0f, Color::get_Red());
gradient->get_GradientStops()->Add(0.5f, Color::get_Lime());
gradient->get_GradientStops()->Add(1.0f, Color::get_Blue());
gradient->set_LinearGradientScaled(NullableBool::False);
gradient->set_LinearGradientAngle(angles[i]);
}
presentation->Save(u"linear_gradient_angles.pptx", SaveFormat::Pptx);
presentation->Dispose();
PowerPoint 中的結果:

以形狀縮放線性漸層
IGradientFormat::set_LinearGradientScaled 接受NullableBool 值。明確設為 False 可在形狀比例變更時保留幾何漸層角度,設為 True 則會隨形狀寬高縮放漸層。啟用縮放時,非正方形形狀的可見角度可能與指派角度不同。
下面的範例使用 45 度角及與角度比較相同的顏色停止點。左欄使用 False,右欄使用 True。每欄皆包含一個寬矩形上方與高矩形下方。兩者起始尺寸相同;改變第二個矩形的寬高,即可示範在不更改漸層設定的情況下,填色如何因調整大小而回應。
#include <DOM/FillType.h>
#include <DOM/GradientShape.h>
#include <DOM/IAutoShape.h>
#include <DOM/IFillFormat.h>
#include <DOM/IGradientFormat.h>
#include <DOM/IGradientStopCollection.h>
#include <DOM/ILineFillFormat.h>
#include <DOM/ILineFormat.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/ISlideSize.h>
#include <DOM/NullableBool.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <DOM/SlideSizeScaleType.h>
#include <Export/SaveFormat.h>
#include <drawing/color.h>
#include <system/smart_ptr.h>
#include <array>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
using namespace System;
using namespace System::Drawing;
auto presentation = MakeObject<Presentation>();
presentation->get_SlideSize()->SetSize(720, 460, SlideSizeScaleType::DoNotScale);
auto slide = presentation->get_Slide(0);
const auto scalingOptions = std::array<NullableBool, 2>{ NullableBool::False, NullableBool::True };
for (auto column = 0; column < scalingOptions.size(); column++)
{
for (auto row = 0; row < 2; row++)
{
auto x = 30 + column * 360;
auto y = row == 0 ? 30 : 150;
auto shape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, x, y, 300, 90);
shape->get_LineFormat()->get_FillFormat()->set_FillType(FillType::NoFill);
shape->get_FillFormat()->set_FillType(FillType::Gradient);
auto gradient = shape->get_FillFormat()->get_GradientFormat();
gradient->set_GradientShape(GradientShape::Linear);
gradient->get_GradientStops()->Clear();
gradient->get_GradientStops()->Add(0.0f, Color::get_Red());
gradient->get_GradientStops()->Add(0.5f, Color::get_Lime());
gradient->get_GradientStops()->Add(1.0f, Color::get_Blue());
gradient->set_LinearGradientAngle(45);
gradient->set_LinearGradientScaled(scalingOptions[column]);
if (row == 1)
{
shape->set_Width(90);
shape->set_Height(280);
}
}
}
presentation->Save(u"linear_gradient_scaling.pptx", SaveFormat::Pptx);
presentation->Dispose();
PowerPoint 中的結果,左側縮放停用、右側縮放啟用:

縮放停用時,對角線色帶在兩個矩形中保持相同方向;縮放啟用時,色帶會隨形狀比例伸展。此功能有助於在將形狀調整為不同大小時,復原範本的漸層。在正方形上啟用縮放不會改變此 45 度漸層的外觀。
角度與縮放設定皆適用於線性漸層。它們不會控制[GradientShape::Radial]或GradientShape::Rectangle 填色。
結合角度與漸層方向
IGradientFormat::set_GradientDirection 從GradientDirection 列舉中選取漸層樣式;它並非數值角度的替代品。對於線性漸層而言,方向樣式可以在不變更透過IGradientFormat::set_LinearGradientAngle 指定的角度的前提下,改變顏色分佈的區域。比較角度或縮放時,請保持方向不變。
以下範例以相反的順序套用相同設定。兩個矩形的外觀相同:先設定方向再設定角度,或先設定角度再設定方向,結果皆不變。
#include <DOM/FillType.h>
#include <DOM/GradientDirection.h>
#include <DOM/GradientShape.h>
#include <DOM/IAutoShape.h>
#include <DOM/IFillFormat.h>
#include <DOM/IGradientFormat.h>
#include <DOM/IGradientStopCollection.h>
#include <DOM/ILineFillFormat.h>
#include <DOM/ILineFormat.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/NullableBool.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <Export/SaveFormat.h>
#include <drawing/color.h>
#include <system/smart_ptr.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
using namespace System;
using namespace System::Drawing;
auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
for (auto i = 0; i < 2; i++)
{
auto shape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 30, 30 + i * 140, 320, 100);
shape->get_LineFormat()->get_FillFormat()->set_FillType(FillType::NoFill);
shape->get_FillFormat()->set_FillType(FillType::Gradient);
auto gradient = shape->get_FillFormat()->get_GradientFormat();
gradient->set_GradientShape(GradientShape::Linear);
gradient->get_GradientStops()->Clear();
gradient->get_GradientStops()->Add(0.0f, Color::get_Red());
gradient->get_GradientStops()->Add(0.5f, Color::get_Lime());
gradient->get_GradientStops()->Add(1.0f, Color::get_Blue());
gradient->set_LinearGradientScaled(NullableBool::True);
if (i == 0)
{
gradient->set_GradientDirection(GradientDirection::FromCorner1);
gradient->set_LinearGradientAngle(30);
}
else
{
gradient->set_LinearGradientAngle(30);
gradient->set_GradientDirection(GradientDirection::FromCorner1);
}
}
presentation->Save(u"linear_gradient_direction.pptx", SaveFormat::Pptx);
presentation->Dispose();
圖案填色
圖案填色會套用兩色設計(例如點、條紋或格子)。此範例使用IPatternFormat::set_PatternStyle 以選取格子圖案。IPatternFormat::get_BackColor 提供淺灰色背景,IPatternFormat::get_ForeColor 提供黃色圖案顏色。
#include <DOM/FillType.h>
#include <DOM/IAutoShape.h>
#include <DOM/IColorFormat.h>
#include <DOM/IFillFormat.h>
#include <DOM/IPatternFormat.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/PatternStyle.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <Export/SaveFormat.h>
#include <drawing/color.h>
#include <system/smart_ptr.h>
using namespace Aspose::Slides;
using namespace System;
using namespace Aspose::Slides::Export;
using namespace System::Drawing;
auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto shape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 50, 50, 150, 75);
shape->get_FillFormat()->set_FillType(FillType::Pattern);
shape->get_FillFormat()->get_PatternFormat()->set_PatternStyle(PatternStyle::Trellis);
shape->get_FillFormat()->get_PatternFormat()->get_BackColor()->set_Color(Color::get_LightGray());
shape->get_FillFormat()->get_PatternFormat()->get_ForeColor()->set_Color(Color::get_Yellow());
presentation->Save(u"pattern_fill.pptx", SaveFormat::Pptx);
presentation->Dispose();
結果:

圖片填色
圖片填色會在形狀內使用影像。此範例需要本機圖片,並使用Images::FromFile 載入至簡報的影像集合。將IPictureFillFormat::set_PictureFillMode 設為「平鋪」模式,可在矩形內重複圖片。以下顯示使用的來源圖片:

#include <DOM/FillType.h>
#include <DOM/IAutoShape.h>
#include <DOM/IFillFormat.h>
#include <DOM/IImageCollection.h>
#include <DOM/IPictureFillFormat.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/ISlidesPicture.h>
#include <DOM/PictureFillMode.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <Export/SaveFormat.h>
#include <IImage.h>
#include <Util/Images.h>
#include <system/smart_ptr.h>
using namespace Aspose::Slides;
using namespace System;
using namespace Aspose::Slides::Export;
auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto shape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 50, 50, 255, 130);
shape->get_FillFormat()->set_FillType(FillType::Picture);
shape->get_FillFormat()->get_PictureFillFormat()->set_PictureFillMode(PictureFillMode::Tile);
auto image = Images::FromFile(u"lotus.png");
auto presentationImage = presentation->get_Images()->AddImage(image);
shape->get_FillFormat()->get_PictureFillFormat()->get_Picture()->set_Image(presentationImage);
presentation->Save(u"picture_fill.pptx", SaveFormat::Pptx);
image->Dispose();
presentation->Dispose();
結果:

將圖片平鋪作為紋理
若想將平鋪圖片設定為紋理並自訂平鋪行為,可使用IPictureFillFormat 介面與PictureFillFormat 類別的以下方法:
- set_PictureFillMode:設定圖片填色模式——
Tile或Stretch。 - set_TileAlignment:指定平鋪在形狀內的對齊方式。
- set_TileFlip:控制平鋪是否水平、垂直或同時翻轉。
- set_TileOffsetX:以點為單位設定平鋪相對於形狀原點的水平偏移。
- set_TileOffsetY:以點為單位設定平鋪的垂直偏移。
- set_TileScaleX:以百分比定義水平縮放比例。
- set_TileScaleY:以百分比定義垂直縮放比例。
此範例需要本機圖片,並將IPictureFillFormat 設為以原始大小 50% 重複。將 X、Y 偏移設為 -32 點,使平鋪在水平與垂直方向上移動;使用右下對齊且同時翻轉兩個方向,以決定重複紋理的排列方式。
#include <DOM/FillType.h>
#include <DOM/IAutoShape.h>
#include <DOM/IFillFormat.h>
#include <DOM/IImageCollection.h>
#include <DOM/IPictureFillFormat.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/ISlidesPicture.h>
#include <DOM/PictureFillMode.h>
#include <DOM/Presentation.h>
#include <DOM/RectangleAlignment.h>
#include <DOM/ShapeType.h>
#include <DOM/TileFlip.h>
#include <Export/SaveFormat.h>
#include <IImage.h>
#include <Util/Images.h>
#include <system/smart_ptr.h>
using namespace Aspose::Slides;
using namespace System;
using namespace Aspose::Slides::Export;
auto presentation = MakeObject<Presentation>();
auto firstSlide = presentation->get_Slide(0);
auto shape = firstSlide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 50, 50, 190, 95);
shape->get_FillFormat()->set_FillType(FillType::Picture);
auto sourceImage = Images::FromFile(u"lotus.png");
auto presentationImage = presentation->get_Images()->AddImage(sourceImage);
auto pictureFillFormat = shape->get_FillFormat()->get_PictureFillFormat();
pictureFillFormat->get_Picture()->set_Image(presentationImage);
pictureFillFormat->set_PictureFillMode(PictureFillMode::Tile);
pictureFillFormat->set_TileOffsetX(-32);
pictureFillFormat->set_TileOffsetY(-32);
pictureFillFormat->set_TileScaleX(50);
pictureFillFormat->set_TileScaleY(50);
pictureFillFormat->set_TileAlignment(RectangleAlignment::BottomRight);
pictureFillFormat->set_TileFlip(TileFlip::FlipBoth);
presentation->Save(u"tile.pptx", SaveFormat::Pptx);
sourceImage->Dispose();
presentation->Dispose();
結果:

實色填色
實色填色會為形狀賦予單一、均勻的顏色。此範例將IFillFormat::set_FillType 設為實色填充模式,並透過IFillFormat::get_SolidFillColor 指派黃色,最後將矩形儲存於簡報中。
#include <DOM/FillType.h>
#include <DOM/IAutoShape.h>
#include <DOM/IColorFormat.h>
#include <DOM/IFillFormat.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <Export/SaveFormat.h>
#include <drawing/color.h>
#include <system/smart_ptr.h>
using namespace Aspose::Slides;
using namespace System;
using namespace Aspose::Slides::Export;
using namespace System::Drawing;
auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto shape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 50, 50, 150, 75);
shape->get_FillFormat()->set_FillType(FillType::Solid);
shape->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_Yellow());
presentation->Save(u"solid_color_fill.pptx", SaveFormat::Pptx);
presentation->Dispose();
結果:

將投影片背景作為形狀填色
當形狀需與投影片背景保持一致(例如在簡報範本中)時,使用IAutoShape::set_UseBackgroundFill。先將IFillFormat::set_FillType 設為FillType::NotDefined,以移除任何明確的填色。此組合下,形狀會使用投影片背景而非自身樣式填色。僅啟用此旗標不會移除已有的顯式填色。
此做法不同於FillType::NoFill:背景填色會用投影片相對應區域的背景覆蓋形狀背後的物件,而無填色則會讓那些物件保持可見。漸層仍會與投影片對齊,而不會在每個形狀內重新開始。
以下範例建立一個由母片繼承的背景,並在三個矩形後方放置一條洋紅色帶。左側矩形使用背景填色,中間矩形無填色,右側矩形則從背景填色切換為明確的橙色填色。
#include <DOM/BackgroundType.h>
#include <DOM/FillType.h>
#include <DOM/GradientShape.h>
#include <DOM/IAutoShape.h>
#include <DOM/IBackground.h>
#include <DOM/IColorFormat.h>
#include <DOM/IFillFormat.h>
#include <DOM/IGradientFormat.h>
#include <DOM/IGradientStopCollection.h>
#include <DOM/ILayoutSlide.h>
#include <DOM/ILineFillFormat.h>
#include <DOM/ILineFormat.h>
#include <DOM/IMasterSlide.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/ISlideSize.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <DOM/SlideSizeScaleType.h>
#include <Export/SaveFormat.h>
#include <drawing/color.h>
#include <system/smart_ptr.h>
using namespace Aspose::Slides;
using namespace System;
using namespace Aspose::Slides::Export;
using namespace System::Drawing;
auto presentation = MakeObject<Presentation>();
presentation->get_SlideSize()->SetSize(720, 400, SlideSizeScaleType::DoNotScale);
auto slide = presentation->get_Slide(0);
// 從母版繼承漸層。
slide->get_Background()->set_Type(BackgroundType::NotDefined);
slide->get_LayoutSlide()->get_Background()->set_Type(BackgroundType::NotDefined);
auto background = slide->get_LayoutSlide()->get_MasterSlide()->get_Background();
background->set_Type(BackgroundType::OwnBackground);
background->get_FillFormat()->set_FillType(FillType::Gradient);
auto gradient = background->get_FillFormat()->get_GradientFormat();
gradient->set_GradientShape(GradientShape::Linear);
gradient->set_LinearGradientAngle(0);
gradient->get_GradientStops()->Clear();
gradient->get_GradientStops()->Add(0, Color::get_DarkBlue());
gradient->get_GradientStops()->Add(1, Color::get_LightSkyBlue());
// 先加入此物件,使其位於矩形之後。
auto band = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 25, 150, 670, 100);
band->get_FillFormat()->set_FillType(FillType::Solid);
band->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_Magenta());
band->get_LineFormat()->get_FillFormat()->set_FillType(FillType::NoFill);
auto backgroundShape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 50, 100, 160, 200);
backgroundShape->get_FillFormat()->set_FillType(FillType::NotDefined);
backgroundShape->set_UseBackgroundFill(true);
auto noFillShape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 280, 100, 160, 200);
noFillShape->get_FillFormat()->set_FillType(FillType::NoFill);
auto explicitShape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 510, 100, 160, 200);
explicitShape->get_FillFormat()->set_FillType(FillType::NotDefined);
explicitShape->set_UseBackgroundFill(true);
// 在套用明確填色前停用背景填色。
explicitShape->set_UseBackgroundFill(false);
explicitShape->get_FillFormat()->set_FillType(FillType::Solid);
explicitShape->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_Orange());
presentation->Save(u"background_fill.pptx", SaveFormat::Pptx);
presentation->Dispose();
結果(由左至右):背景填色、無填色與明確填色。洋紅色帶在左、右矩形下方被遮蔽,但在中間矩形上仍可見。

相同的行為亦適用於直接在投影片上定義或從版面繼承的背景。形狀會使用已解析的投影片背景;此範例讓投影片與版面背景皆未定義,讓母片提供背景。
設定透明度
對於實色填色,顏色的 alpha 成分控制不透明度:0 為完全透明,255 為完全不透明。此範例將 alpha 值設為 128,為黃色矩形提供約 50% 的透明度,使下方矩形仍可見。顏色透過IFillFormat::get_SolidFillColor 指派。
#include <DOM/FillType.h>
#include <DOM/IAutoShape.h>
#include <DOM/IColorFormat.h>
#include <DOM/IFillFormat.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <Export/SaveFormat.h>
#include <drawing/color.h>
#include <system/smart_ptr.h>
using namespace Aspose::Slides;
using namespace System;
using namespace Aspose::Slides::Export;
using namespace System::Drawing;
const int alpha = 128;
auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto solidShape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 50, 50, 150, 75);
auto transparentShape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 80, 80, 150, 75);
transparentShape->get_FillFormat()->set_FillType(FillType::Solid);
transparentShape->get_FillFormat()->get_SolidFillColor()->set_Color(Color::FromArgb(alpha, Color::get_Yellow()));
presentation->Save(u"shape_transparency.pptx", SaveFormat::Pptx);
presentation->Dispose();
結果:

旋轉形狀
使用IShape::set_Rotation 可在投影片平面上旋轉形狀。角度以度為單位;此範例將矩形順時針旋轉 5 度,並儲存簡報。
#include <DOM/IAutoShape>
#include <DOM/IShapeCollection>
#include <DOM/ISlide>
#include <DOM/Presentation>
#include <DOM/ShapeType>
#include <Export/SaveFormat>
#include <system/smart_ptr>
using namespace Aspose::Slides;
using namespace System;
using namespace Aspose::Slides::Export;
auto presentation = MakeObject<Presentation>();
auto slide = presentation‑>get_Slide(0);
auto shape = slide‑>get_Shapes()->AddAutoShape(ShapeType::Rectangle, 50, 50, 150, 75);
shape‑>set_Rotation(5);
presentation‑>Save(u"shape_rotation.pptx", SaveFormat::Ppt3 );
結果:

新增 3D 斜角效果
使用IShape::get_ThreeDFormat 為形狀賦予深度與斜角邊緣。此範例建立綠色橢圓形,橙色輪廓,將IThreeDFormat::set_Depth 設為 4 點,並透過IThreeDFormat::get_BevelTop 套用寬度與高度皆為 6 點的圓形上斜角。前視正交相機與三點光源使斜角可見。
#include <DOM/BevelPresetType.h>
#include <DOM/CameraPresetType.h>
#include <DOM/FillType.h>
#include <DOM/IAutoShape.h>
#include <DOM/ICamera.h>
#include <DOM/IColorFormat.h>
#include <DOM/IFillFormat.h>
#include <DOM/ILightRig.h>
#include <DOM/ILineFillFormat.h>
#include <DOM/ILineFormat.h>
#include <DOM/IShapeBevel.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/IThreeDFormat.h>
#include <DOM/LightRigPresetType.h>
#include <DOM/LightingDirection.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <Export/SaveFormat.h>
#include <drawing/color.h>
#include <system/smart_ptr.h>
using namespace Aspose::Slides;
using namespace System;
using namespace Aspose::Slides::Export;
using namespace System::Drawing;
auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto shape = slide->get_Shapes()->AddAutoShape(ShapeType::Ellipse, 50, 50, 100, 100);
shape->get_FillFormat()->set_FillType(FillType::Solid);
shape->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_Green());
shape->get_LineFormat()->get_FillFormat()->set_FillType(FillType::Solid);
shape->get_LineFormat()->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_Orange());
shape->get_LineFormat()->set_Width(2.0);
shape->get_ThreeDFormat()->set_Depth(4);
shape->get_ThreeDFormat()->get_BevelTop()->set_BevelType(BevelPresetType::Circle);
shape->get_ThreeDFormat()->get_BevelTop()->set_Height(6);
shape->get_ThreeDFormat()->get_BevelTop()->set_Width(6);
shape->get_ThreeDFormat()->get_Camera()->set_CameraType(CameraPresetType::OrthographicFront);
shape->get_ThreeDFormat()->get_LightRig()->set_LightType(LightRigPresetType::ThreePt);
shape->get_ThreeDFormat()->get_LightRig()->set_Direction(LightingDirection::Top);
presentation->Save(u"3D_bevel_effect.pptx", SaveFormat::Pptx);
presentation->Dispose();
結果:

新增 3D 旋轉效果
形狀的 3D 視圖受相機設定控制。此範例使用ICamera::SetRotation 設定緯度、經度與旋轉角度分別為 40、35、20 度。再透過ICamera::set_CameraType 選取等角相機,並使用ILightRig::set_LightType 設定平衡光源,最後儲存已格式化的矩形。
#include <DOM/CameraPresetType.h>
#include <DOM/IAutoShape.h>
#include <DOM/ICamera.h>
#include <DOM/ILightRig.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/ITextFrame.h>
#include <DOM/IThreeDFormat.h>
#include <DOM/LightRigPresetType.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <Export/SaveFormat.h>
#include <system/smart_ptr.h>
using namespace Aspose::Slides;
using namespace System;
using namespace Aspose::Slides::Export;
auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto autoShape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 50, 50, 150, 75);
autoShape->get_TextFrame()->set_Text(u"Hello, Aspose!");
autoShape->get_ThreeDFormat()->get_Camera()->set_CameraType(CameraPresetType::IsometricLeftUp);
autoShape->get_ThreeDFormat()->get_Camera()->SetRotation(40, 35, 20);
autoShape->get_ThreeDFormat()->get_LightRig()->set_LightType(LightRigPresetType::Balanced);
presentation->Save(u"3D_rotation_effect.pptx", SaveFormat::Pptx);
presentation->Dispose();
結果:

控制形狀的黑白顯示模式
IShape::set_BlackWhiteMode 方法指定在以黑白模式檢視或處理簡報時,單一形狀的渲染方式。它本身不會啟用黑白顯示,也不會改變形狀在正常彩色模式下的填色、線條或其他格式。
使用BlackWhiteMode 列舉中的值,可選擇所需的行為。例如,Automatic 讓渲染應用程式自行決定轉換方式,Gray 與 LightGray 使用灰階著色,BlackWhite 只使用黑白,Black 與 White 強制單色,Color 保持正常彩色,Hidden 在黑白模式下省略形狀,NotDefined 表示未指派形狀層級的模式。
此範例建立橙色矩形,並將IShape::set_BlackWhiteMode 設為 Gray。儲存後的形狀在正常彩色顯示下仍保有橙色填色,並在支援黑白顯示設定的應用程式中請求灰階著色:
#include <DOM/BlackWhiteMode.h>
#include <DOM/FillType.h>
#include <DOM/IAutoShape.h>
#include <DOM/IColorFormat.h>
#include <DOM/IFillFormat.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <Export/SaveFormat.h>
#include <drawing/color.h>
#include <system/smart_ptr.h>
using namespace Aspose::Slides;
using namespace System;
using namespace Aspose::Slides::Export;
using namespace System::Drawing;
auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto shape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 50, 50, 200, 100);
shape->get_FillFormat()->set_FillType(FillType::Solid);
shape->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_Orange());
shape->set_BlackWhiteMode(BlackWhiteMode::Gray);
presentation->Save(u"shape_black_white_mode.pptx", SaveFormat::Pptx);
presentation->Dispose();
在正常彩色模式下,矩形保留橙色填色;在黑白顯示工作流程中,因模式設定為 Gray,它會以灰階顯示。此功能讓您在保留完整彩色投影片的同時,為列印、預覽或其他遵循簡報黑白顯示設定的工作流程定義不同的外觀。
重設格式
使用ISlide::Reset 可從對應的版面佈局佔位元復原投影片佔位元的位置、大小與格式。本範例需要一個已有的簡報,且其版面佔位元已連結,會在儲存前重設每張投影片。沒有對應版面佔位元的形狀不會被重設。
#include <DOM/ISlide.h>
#include <DOM/ISlideCollection.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
#include <system/enumerator_adapter.h>
#include <system/smart_ptr.h>
using namespace Aspose::Slides;
using namespace System;
using namespace Aspose::Slides::Export;
auto presentation = MakeObject<Presentation>(u"sample.pptx");
for (auto&& slide : IterateOver(presentation->get_Slides()))
{
slide->Reset();
}
presentation->Save(u"reset_formatting.pptx", SaveFormat::Pptx);
presentation->Dispose();
常見問題集
形狀格式化會影響最終簡報檔案大小嗎?
顏色、線條樣式與效果設定相較於嵌入的影像與媒體所佔的資料量通常很少。圖片填色可能會因為其影像儲存在簡報中而增加檔案大小。
如何偵測投影片上具有相同格式的形狀,以便將它們分組?
比較每個形狀的關鍵格式屬性──填色、線條與效果設定。若所有對應的值相符,便可視為樣式相同,並在邏輯上將這些形狀分組,這有助於後續的樣式管理。
我可以將一組自訂的形狀樣式儲存於單獨的檔案,以便在其他簡報中重複使用嗎?
可以。將具備所需樣式的範例形狀儲存於範本投影片或 .POTX 範本檔案中。建立新簡報時,開啟範本,複製所需的樣式形狀,並在需要的地方重新套用其格式。