在 Android 上格式化 PowerPoint 圖形

簡介

透過調整 PowerPoint 中的輪廓、填色與效果來格式化圖形。Aspose.Slides for Android via Java 讓您可以以程式方式控制這些設定,包括透明度、旋轉以及 3D 格式化。

PowerPoint 中的圖形格式設定選項

Aspose.Slides for Android via Java 提供介面與方法,讓您使用與 PowerPoint 相同的選項來格式化圖形。

格式線條

使用IShape.getLineFormat 來控制圖形的輪廓。此範例建立一個未填充、藍色虛線輪廓的矩形。ILineFormat.setStyle 選擇粗細複合線,而ILineFormat.setWidth 將寬度設定為 7 點,且ILineFormat.setDashStyle 選擇虛線樣式。

import com.aspose.slides.*;
import android.graphics.Color;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 150, 75);
    shape.getFillFormat().setFillType(FillType.NoFill);
    shape.getLineFormat().setStyle(LineStyle.ThickThin);
    shape.getLineFormat().setWidth(7);
    shape.getLineFormat().setDashStyle(LineDashStyle.Dash);
    shape.getLineFormat().getFillFormat().setFillType(FillType.Solid);
    shape.getLineFormat().getFillFormat().getSolidFillColor().setColor(Color.BLUE);

    presentation.save("formatted_lines.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

結果: 簡報中格式化的線條

設定自訂線條虛線圖案

當預先定義的線條樣式無法提供所需的虛線長度與間隔時,請使用ILineFormat.setCustomDashPattern。例如品牌輪廓或技術圖表。指派圖案會自動將線條虛線樣式變更為LineDashStyle.Custom。

陣列項目交替表示虛線與間隔長度,從虛線開始。每個值都是線寬的倍數:在寬度為 3 點時,{ 4, 2 } 產生 12 點長的虛線,接著 6 點的間隔。請使用有限且非負的值成對定義完整的虛線與間隔段落。此範例使用正值以確保每段虛線與間隔皆可見。

此範例建立五條藍色線條,長度相同且使用平直端點。前三條在 3 點寬度下比較圖案。第四條以兩倍寬度重複第一個圖案,將虛線與間隔長度皆加倍。第五條則從自訂圖案切換為實線。

import com.aspose.slides.*;
import android.graphics.Color;

Presentation presentation = new Presentation();
try {
    presentation.getSlideSize().setSize(600, 300, SlideSizeScaleType.DoNotScale);
    ISlide slide = presentation.getSlides().get_Item(0);

    float[][] patterns = {
        { 4, 2 },
        { 1, 2 },
        { 6, 2, 1, 2 },
        { 4, 2 },
        { 4, 2 }
    };

    for (int i = 0; i < patterns.length; i++) {
        IAutoShape line = slide.getShapes().addAutoShape(ShapeType.Line, 50, 50 + i * 50, 500, 0);
        line.getLineFormat().setWidth(i == 3 ? 6 : 3);
        line.getLineFormat().getFillFormat().setFillType(FillType.Solid);
        line.getLineFormat().getFillFormat().getSolidFillColor().setColor(Color.rgb(0, 0, 139));
        line.getLineFormat().setCapStyle(LineCapStyle.Flat);
        line.getLineFormat().setBeginArrowheadStyle(LineArrowheadStyle.None);
        line.getLineFormat().setEndArrowheadStyle(LineArrowheadStyle.None);
        line.getLineFormat().setCustomDashPattern(patterns[i]);
    }

    // 將最後一條線回復為實線樣式。
    slide.getShapes().get_Item(4).getLineFormat().setDashStyle(LineDashStyle.Solid);

    presentation.save("custom_dash_patterns.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

若要改回預先定義的虛線樣式,請將LineDashStyle.Dash 傳遞給 ILineFormat.setDashStyle。所選樣式會控制線條的外觀。

PowerPoint 中的結果,從上到下:{4, 2}, 3 pt;{1, 2}, 3 pt;{6, 2, 1, 2}, 3 pt;{4, 2}, 6 pt;以及 Solid, 3 pt。

PowerPoint 中的三種自訂虛線圖案、一條使用第一個圖案的較寬線條,以及一條實線

設定線條端點樣式

使用ILineFormat.setCapStyle 來設定開放線條的端點。LineCapStyle 類別提供三種明確的端點樣式:

  • Flat:筆畫在端點處停止。
  • Round:半圓形端點延伸超過端點半個線寬。
  • Square:方形端點延伸超過端點半個線寬。

此類別亦包含 NotDefined,表示未明確指定端點樣式。

端點與箭頭不同,箭頭會在線條端點加入符號,接合則控制線段之間的角落。此範例透過ILineFormat.setBeginArrowheadStyle 與ILineFormat.setEndArrowheadStyle 停用箭頭,以僅比較端點形狀。

以下範例建立三條在其他方面相同的水平線,寬度為 20 點。從上到下分別使用 flat、round 與 square 端點。它們的端點具有相同的水平座標;round 與 square 端點會延伸超過每個端點 10 點。

import com.aspose.slides.*;
import android.graphics.Color;

Presentation presentation = new Presentation();
try {
    presentation.getSlideSize().setSize(500, 240, SlideSizeScaleType.DoNotScale);
    ISlide slide = presentation.getSlides().get_Item(0);

    byte[] capStyles = new byte[] { LineCapStyle.Flat, LineCapStyle.Round, LineCapStyle.Square };

    for (int i = 0; i < capStyles.length; i++) {
        IAutoShape line = slide.getShapes().addAutoShape(ShapeType.Line, 100, 50 + i * 70, 300, 0);
        line.getLineFormat().setWidth(20);
        line.getLineFormat().getFillFormat().setFillType(FillType.Solid);
        line.getLineFormat().getFillFormat().getSolidFillColor().setColor(Color.rgb(0, 0, 139));
        line.getLineFormat().setDashStyle(LineDashStyle.Solid);
        line.getLineFormat().setCapStyle(capStyles[i]);
        line.getLineFormat().setBeginArrowheadStyle(LineArrowheadStyle.None);
        line.getLineFormat().setEndArrowheadStyle(LineArrowheadStyle.None);
    }

    presentation.save("line_caps.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

PowerPoint 中的結果,從上到下:Flat、Round 與 Square。

PowerPoint 中相同粗線的平直、圓形與方形端點

將草圖效果套用至圖形線條

草圖效果會讓圖形的線條看起來像手繪。使用IShape.getLineFormat 取得線條設定,使用ILineFormat.getSketchFormat 取得草圖設定,並使用ISketchFormat.setSketchType 從LineSketchType 類別中選取值。

以下 Java 程式碼示範如何套用LineSketchType.Curved 效果、讀取明確指派的值,以及使用LineSketchType.None 移除效果:

import com.aspose.slides.*;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 200, 100);

    ISketchFormat sketchFormat = shape.getLineFormat().getSketchFormat();
    sketchFormat.setSketchType(LineSketchType.Curved);
    int explicitSketchType = sketchFormat.getSketchType();
    System.out.println("Explicit sketch type: " + explicitSketchType);
    sketchFormat.setSketchType(LineSketchType.None);
} finally {
    presentation.dispose();
}

由ISketchFormat.getSketchType 回傳的值代表直接指派給圖形的設定。若線條格式可能從佈景主題、母片或版面投影片繼承,請使用ILineFormat.getEffective,存取ILineFormatEffectiveData.getSketchFormat,並讀取ISketchFormatEffectiveData.getSketchType。有效值反映在繼承解析後實際套用的格式。此範例需要一個已存在的投影片,且其第一張投影片上至少有一個圖形,並輸出兩個值:

import com.aspose.slides.*;

Presentation presentation = new Presentation("presentation.pptx");
try {
    IShape shape = presentation.getSlides().get_Item(0).getShapes().get_Item(0);

    ILineFormat lineFormat = shape.getLineFormat();
    int explicitSketchType = lineFormat.getSketchFormat().getSketchType();
    ILineFormatEffectiveData effectiveLineFormat = lineFormat.getEffective();
    int effectiveSketchType = effectiveLineFormat.getSketchFormat().getSketchType();

    System.out.println("Explicit sketch type: " + explicitSketchType);
    System.out.println("Effective sketch type: " + effectiveSketchType);
} finally {
    presentation.dispose();
}

格式接合樣式

以下是三種接合類型選項:

  • Round
  • Miter
  • Bevel

接合樣式控制兩條輪廓線在角落的結合方式。使用 Round 以取得圓角,Miter 以取得尖角,或 Bevel 以取得平整角。

投影片中的接合樣式

此範例建立三個黑色矩形,藍色輪廓寬度為 15 點。它分別使用ILineFormat.setJoinStyle 套用 LineJoinStyle 的 miter、bevel 與 round 值,以比較角落的繪製方式。

import com.aspose.slides.*;
import android.graphics.Color;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    IAutoShape miterShape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 20, 20, 150, 75);
    IAutoShape bevelShape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 210, 20, 150, 75);
    IAutoShape roundShape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 20, 135, 150, 75);

    miterShape.getFillFormat().setFillType(FillType.Solid);
    miterShape.getFillFormat().getSolidFillColor().setColor(Color.BLACK);
    bevelShape.getFillFormat().setFillType(FillType.Solid);
    bevelShape.getFillFormat().getSolidFillColor().setColor(Color.BLACK);
    roundShape.getFillFormat().setFillType(FillType.Solid);
    roundShape.getFillFormat().getSolidFillColor().setColor(Color.BLACK);

    miterShape.getLineFormat().setWidth(15);
    bevelShape.getLineFormat().setWidth(15);
    roundShape.getLineFormat().setWidth(15);

    miterShape.getLineFormat().getFillFormat().setFillType(FillType.Solid);
    miterShape.getLineFormat().getFillFormat().getSolidFillColor().setColor(Color.BLUE);
    bevelShape.getLineFormat().getFillFormat().setFillType(FillType.Solid);
    bevelShape.getLineFormat().getFillFormat().getSolidFillColor().setColor(Color.BLUE);
    roundShape.getLineFormat().getFillFormat().setFillType(FillType.Solid);
    roundShape.getLineFormat().getFillFormat().getSolidFillColor().setColor(Color.BLUE);

    miterShape.getLineFormat().setJoinStyle(LineJoinStyle.Miter);
    bevelShape.getLineFormat().setJoinStyle(LineJoinStyle.Bevel);
    roundShape.getLineFormat().setJoinStyle(LineJoinStyle.Round);

    miterShape.getTextFrame().setText("Miter Join Style");
    bevelShape.getTextFrame().setText("Bevel Join Style");
    roundShape.getTextFrame().setText("Round Join Style");

    presentation.save("join_styles.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

限制銳角的斜接

使用ILineFormat.setMiterLimit 搭配將ILineFormat.setJoinStyle 設為 LineJoinStyle.Miter,以限制銳角可延伸的距離。

此限制是內外角之間距離與線寬的比率。例如,值 8 允許斜接長度最多為線寬的八倍;這不是點數測量。如果角落超過此限制,PowerPoint 會在該角落使用斜接。較大的限制允許更尖銳的點。此設定僅影響斜接,且在角落仍在兩個限制內時更改不會產生可見效果。

此範例建立兩個相同的未填充三角形,輪廓寬度為 20 點。左側三角形使用限制 2,右側使用 8。每個三角形寬度 80 點,高度 160 點,使其頂角的斜接長度與線寬比約為 4.12。因此,PowerPoint 會對左側頂點使用斜接,而保留右側尖銳頂點。

import com.aspose.slides.*;
import android.graphics.Color;

Presentation presentation = new Presentation();
try {
    presentation.getSlideSize().setSize(500, 320, SlideSizeScaleType.DoNotScale);
    ISlide slide = presentation.getSlides().get_Item(0);

    float[] miterLimits = new float[] { 2f, 8f };

    for (int i = 0; i < miterLimits.length; i++) {
        IAutoShape triangle = slide.getShapes().addAutoShape(ShapeType.Triangle, 90 + i * 240, 110, 80, 160);
        triangle.getFillFormat().setFillType(FillType.NoFill);
        triangle.getLineFormat().setWidth(20);
        triangle.getLineFormat().getFillFormat().setFillType(FillType.Solid);
        triangle.getLineFormat().getFillFormat().getSolidFillColor().setColor(Color.rgb(0, 0, 139));
        triangle.getLineFormat().setJoinStyle(LineJoinStyle.Miter);
        triangle.getLineFormat().setMiterLimit(miterLimits[i]);
    }

    presentation.save("miter_limits.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

PowerPoint 中的結果:左側為 Miter limit 2,右側為 miter limit 8。

PowerPoint 中,斜接限制 2 的倒角頂點與斜接限制 8 的尖銳頂點

漸層填色

漸層填色會在圖形上混合顏色。此範例使用IFillFormat.getGradientFormat 以 45 度角將橢圓以線性漸層從紅色過渡至紫色。 IGradientStopCollection.addPresetColor 方法將色標置於 0 與 1,即漸層的起點與終點。清除現有的色標可確保僅使用這兩種顏色。

import com.aspose.slides.*;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Ellipse, 50, 50, 150, 75);
    shape.getFillFormat().setFillType(FillType.Gradient);
    shape.getFillFormat().getGradientFormat().setGradientShape(GradientShape.Linear);
    shape.getFillFormat().getGradientFormat().setLinearGradientAngle(45);
    shape.getFillFormat().getGradientFormat().getGradientStops().clear();
    shape.getFillFormat().getGradientFormat().getGradientStops().addPresetColor(1.0f, PresetColor.Purple);
    shape.getFillFormat().getGradientFormat().getGradientStops().addPresetColor(0.0f, PresetColor.Red);

    presentation.save("gradient_fill.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

結果: 具有漸層填色的橢圓

如果匯出圖像的漸層外觀與 PowerPoint 中不同,請參閱選擇漸層渲染樣式。您可在不變更簡報中儲存的漸層填色的情況下選擇渲染樣式。

設定線性漸層角度

使用IGradientFormat.setLinearGradientAngle 來控制GradientShape.Linear 填色的方向。角度以度為單位,順時針遞增:0 為左至右,90 為上至下,180 為右至左,270 為下至上。支援小數角度。超出 [0, 360) 的有限角度會正規化為一圈,例如 -45 變為 315,360 變為 0。

此範例建立六個矩形,色標為相同的紅、綠、藍,位置分別為 0、0.5 與 1。停用縮放以便角度可獨立於矩形比例比較。角度自左至右在第一列為 0、45、90,第二列為 135、180、270。

import com.aspose.slides.*;
import android.graphics.Color;

Presentation presentation = new Presentation();
try {
    presentation.getSlideSize().setSize(720, 400, SlideSizeScaleType.DoNotScale);
    ISlide slide = presentation.getSlides().get_Item(0);

    float[] angles = new float[] { 0, 45, 90, 135, 180, 270 };

    for (int i = 0; i < angles.length; i++) {
        int x = 30 + (i % 3) * 230;
        int y = 30 + (i / 3) * 190;

        IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, x, y, 200, 150);
        shape.getLineFormat().getFillFormat().setFillType(FillType.NoFill);
        shape.getFillFormat().setFillType(FillType.Gradient);

        IGradientFormat gradient = shape.getFillFormat().getGradientFormat();
        gradient.setGradientShape(GradientShape.Linear);
        gradient.getGradientStops().clear();
        gradient.getGradientStops().add(0f, Color.RED);
        gradient.getGradientStops().add(0.5f, Color.GREEN);
        gradient.getGradientStops().add(1f, Color.BLUE);
        gradient.setLinearGradientScaled(NullableBool.False);
        gradient.setLinearGradientAngle(angles[i]);
    }

    presentation.save("linear_gradient_angles.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

PowerPoint 中的結果: 上排 0、45、90 度的線性漸層,下排 135、180、270 度的線性漸層

隨圖形縮放線性漸層

IGradientFormat.setLinearGradientScaled 接受NullableBool 值。明確設定為 False 可在圖形比例變化時保持幾何漸層角度不變,或設定為 True 使漸層隨圖形的寬度與高度縮放。啟用縮放時,在非正方形圖形上可見角度可能與指派的角度不同。

以下範例使用 45 度角度以及與角度比較相同的色標。左欄使用 False,右欄使用 True。每欄包含一個寬矩形在上方、一個高矩形在下方。兩者起始寬度相同;變更第二個矩形的寬度與高度可示範填色在調整大小時如何回應,而不更改漸層設定。

import com.aspose.slides.*;
import android.graphics.Color;

Presentation presentation = new Presentation();
try {
    presentation.getSlideSize().setSize(720, 460, SlideSizeScaleType.DoNotScale);
    ISlide slide = presentation.getSlides().get_Item(0);

    byte[] scalingOptions = new byte[] { NullableBool.False, NullableBool.True };

    for (int column = 0; column < scalingOptions.length; column++) {
        for (int row = 0; row < 2; row++) {
            int x = 30 + column * 360;
            int y = row == 0 ? 30 : 150;

            IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, x, y, 300, 90);
            shape.getLineFormat().getFillFormat().setFillType(FillType.NoFill);
            shape.getFillFormat().setFillType(FillType.Gradient);

            IGradientFormat gradient = shape.getFillFormat().getGradientFormat();
            gradient.setGradientShape(GradientShape.Linear);
            gradient.getGradientStops().clear();
            gradient.getGradientStops().add(0f, Color.RED);
            gradient.getGradientStops().add(0.5f, Color.GREEN);
            gradient.getGradientStops().add(1f, Color.BLUE);
            gradient.setLinearGradientAngle(45);
            gradient.setLinearGradientScaled(scalingOptions[column]);

            if (row == 1) {
                shape.setWidth(90);
                shape.setHeight(280);
            }
        }
    }

    presentation.save("linear_gradient_scaling.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

PowerPoint 中的結果,左側停用縮放,右側啟用縮放: 寬矩形與高矩形的 45 度漸層,左側 LinearGradientScaled 為 False,右側為 True 的比較

縮放停用時,對角色帶在兩個矩形中保持相同方向。啟用縮放時,色帶會隨圖形比例伸展。這有助於在將圖形調整至不同尺寸時復製模板的漸層。在正方形上,啟用縮放不會改變此 45 度漸層的外觀。

角度與縮放設定皆適用於線性漸層。它們不控制GradientShape.Radial 或 GradientShape.Rectangle 填色。

將角度與漸層方向結合

IGradientFormat.setGradientDirection 從GradientDirection 類別中選取漸層樣式;它並非數值角度的替代方案。對於線性漸層,角落樣式可變更顏色分布的範圍,而不改變透過setLinearGradientAngle 指定的角度。比較角度或縮放時,請保持方向不變。

以下範例以相反順序套用相同設定。兩個矩形的外觀相同:先設定方向再設定角度或相反皆不會改變結果。

import com.aspose.slides.*;
import android.graphics.Color;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    for (int i = 0; i < 2; i++) {
        IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 30, 30 + i * 140, 320, 100);
        shape.getLineFormat().getFillFormat().setFillType(FillType.NoFill);
        shape.getFillFormat().setFillType(FillType.Gradient);

        IGradientFormat gradient = shape.getFillFormat().getGradientFormat();
        gradient.setGradientShape(GradientShape.Linear);
        gradient.getGradientStops().clear();
        gradient.getGradientStops().add(0f, Color.RED);
        gradient.getGradientStops().add(0.5f, Color.GREEN);
        gradient.getGradientStops().add(1f, Color.BLUE);
        gradient.setLinearGradientScaled(NullableBool.True);

        if (i == 0) {
            gradient.setGradientDirection(GradientDirection.FromCorner1);
            gradient.setLinearGradientAngle(30);
        } else {
            gradient.setLinearGradientAngle(30);
            gradient.setGradientDirection(GradientDirection.FromCorner1);
        }
    }

    presentation.save("linear_gradient_direction.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

圖案填色

圖案填色會套用兩色設計,例如點、條紋或格子。此範例使用IPatternFormat.setPatternStyle 選取格柵圖案。IPatternFormat.getBackColor 提供淡灰色背景,而IPatternFormat.getForeColor 提供黃色圖案色。

import com.aspose.slides.*;
import android.graphics.Color;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 150, 75);
    shape.getFillFormat().setFillType(FillType.Pattern);
    shape.getFillFormat().getPatternFormat().setPatternStyle(PatternStyle.Trellis);
    shape.getFillFormat().getPatternFormat().getBackColor().setColor(Color.rgb(192, 192, 192));
    shape.getFillFormat().getPatternFormat().getForeColor().setColor(Color.YELLOW);

    presentation.save("pattern_fill.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

結果: 套用圖案填色的矩形

圖片填色

圖片填色在圖形內使用圖像。此範例需要本機圖像,並使用Images.fromFile 將其載入簡報的圖像集合。將IPictureFillFormat.setPictureFillMode 設為平鋪模式,可在矩形內重複圖像。下方顯示此處使用的來源圖像:

蓮花圖片

import com.aspose.slides.*;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 255, 130);
    shape.getFillFormat().setFillType(FillType.Picture);
    shape.getFillFormat().getPictureFillFormat().setPictureFillMode(PictureFillMode.Tile);

    IImage image = Images.fromFile("lotus.png");
    IPPImage picture;
    try {
        picture = presentation.getImages().addImage(image);
    } finally {
        image.dispose();
    }
    shape.getFillFormat().getPictureFillFormat().getPicture().setImage(picture);

    presentation.save("picture_fill.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

結果: 套用圖片填色的圖形

將圖片平鋪為紋理

若要將平鋪圖片設定為紋理並自訂平鋪行為,可使用以下 IPictureFillFormat 介面與 PictureFillFormat 類別的方法:

  • setPictureFillMode:設定圖片填色模式—Tile 或 Stretch。
  • setTileAlignment:指定平鋪在圖形內的對齊方式。
  • setTileFlip:控制平鋪是否水平、垂直或雙向翻轉。
  • setTileOffsetX:設定平鋪相對於圖形原點的水平偏移(單位:點)。
  • setTileOffsetY:設定平鋪相對於圖形原點的垂直偏移(單位:點)。
  • setTileScaleX:以百分比定義平鋪的水平縮放比例。
  • setTileScaleY:以百分比定義平鋪的垂直縮放比例。

此範例需要本機圖像,並設定IPictureFillFormat 以其原始大小的 50% 進行重複。−32 點的偏移會水平與垂直平移平鋪;右下對齊且雙向翻轉決定了重複紋理的排列方式。

import com.aspose.slides.*;

Presentation presentation = new Presentation();
try {
    ISlide firstSlide = presentation.getSlides().get_Item(0);

    IAutoShape shape = firstSlide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 190, 95);
    shape.getFillFormat().setFillType(FillType.Picture);

    IImage sourceImage = Images.fromFile("lotus.png");
    IPPImage presentationImage;
    try {
        presentationImage = presentation.getImages().addImage(sourceImage);
    } finally {
        sourceImage.dispose();
    }

    IPictureFillFormat pictureFillFormat = shape.getFillFormat().getPictureFillFormat();
    pictureFillFormat.getPicture().setImage(presentationImage);
    pictureFillFormat.setPictureFillMode(PictureFillMode.Tile);
    pictureFillFormat.setTileOffsetX(-32);
    pictureFillFormat.setTileOffsetY(-32);
    pictureFillFormat.setTileScaleX(50);
    pictureFillFormat.setTileScaleY(50);
    pictureFillFormat.setTileAlignment(RectangleAlignment.BottomRight);
    pictureFillFormat.setTileFlip(TileFlip.FlipBoth);

    presentation.save("tile.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

結果: 平鋪選項

純色填色

純色填色為圖形賦予單一均勻的顏色。此範例使用IFillFormat.setFillType 選取純色填色模式,並透過IFillFormat.getSolidFillColor 指定黃色,然後將矩形儲存至簡報。

import com.aspose.slides.*;
import android.graphics.Color;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 150, 75);
    shape.getFillFormat().setFillType(FillType.Solid);
    shape.getFillFormat().getSolidFillColor().setColor(Color.YELLOW);

    presentation.save("solid_color_fill.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

結果: 套用純色填色的圖形

複製顏色同時保留主題參照

使用IColorFormat.copyFrom 複製顏色定義,包括其顏色類型、主題參照與變換。當在形狀之間轉移填色且仍需遵循簡報主題時,此方法很有用。此方法僅複製顏色:不會複製填色類型、輪廓或其他圖形格式。請使用IFillFormat.setFillType 明確設定目的地的填色類型。

此範例建立三個矩形,從左至右分別為:來源、具主題感知的複本,以及固定 RGB 複本。來源使用IColorFormat.setSchemeColor 參照第一個強調色,並透過IColorOperationCollection.add 加入淡色。RGB 複本使用來源的有效填色,使三者最初看起來相同。呼叫IColorFormat.setColor 設定固定的 RGB 值,並清除任何既有的顏色變換。

import com.aspose.slides.*;
import android.graphics.Color;

Presentation presentation = new Presentation();
try {
    presentation.getSlideSize().setSize(660, 220, SlideSizeScaleType.DoNotScale);
    ISlide slide = presentation.getSlides().get_Item(0);

    presentation.getMasterTheme().getColorScheme().getAccent1().setColor(Color.rgb(65, 105, 225));

    IAutoShape source = slide.getShapes().addAutoShape(ShapeType.Rectangle, 30, 40, 180, 140);
    IAutoShape themeCopy = slide.getShapes().addAutoShape(ShapeType.Rectangle, 240, 40, 180, 140);
    IAutoShape rgbCopy = slide.getShapes().addAutoShape(ShapeType.Rectangle, 450, 40, 180, 140);

    for (IAutoShape shape : new IAutoShape[] { source, themeCopy, rgbCopy }) {
        shape.getFillFormat().setFillType(FillType.Solid);
        shape.getLineFormat().getFillFormat().setFillType(FillType.NoFill);
    }

    IColorFormat sourceColor = source.getFillFormat().getSolidFillColor();
    sourceColor.setSchemeColor(SchemeColor.Accent1);
    sourceColor.getColorTransform().add(ColorTransformOperation.Tint, 0.75f);

    themeCopy.getFillFormat().getSolidFillColor().copyFrom(sourceColor);
    rgbCopy.getFillFormat().getSolidFillColor().setColor(source.getFillFormat().getEffective().getSolidFillColor());

    IImage before = slide.getImage(1f, 1f);
    try {
        before.save("color-copy-before.png", ImageFormat.Png);
    } finally {
        before.dispose();
    }

    // 主題參照會遵循新的強調色;固定的 RGB 值不會。
    presentation.getMasterTheme().getColorScheme().getAccent1().setColor(Color.rgb(255, 69, 0));

    IImage after = slide.getImage(1f, 1f);
    try {
        after.save("color-copy-after.png", ImageFormat.Png);
    } finally {
        after.dispose();
    }
    presentation.save("copied_colors.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

主題變更前,三個填色均相同: 來源、copyFrom 目的地與固定 RGB 目的地,皆為相同的藍色填色

主題變更後,來源與具主題感知的複本會跟隨新強調色並保留其淡色。固定 RGB 複本則保持藍色: 來源與 copyFrom 目的地為橙色填色,而固定 RGB 目的地仍為藍色

複製的定義與來源獨立。之後變更來源的主題參照、指定其他顏色,或新增或移除其變換,都不會影響目的地。主題變更仍會影響兩個圖形,因為它們各自獨立參考相同的主題顏色。

儲存並重新開啟 PPTX 會保留複製顏色的類型(由IColorFormat.getColorType 回傳)、主題參照,以及變換集合(由IColorFormat.getColorTransform 回傳);不會將定義扁平化為 RGB。

在不同主題環境之間複製,例如在不同強調色盤的簡報,複製的參照會依目的地的主題解析。因此相同的參照與變換可能產生不同的可見顏色。請參閱簡報主題 以了解更多主題顏色設定。

使用投影片背景作為圖形填色

當圖形應與投影片背景相匹配時(例如在簡報範本中),請使用IAutoShape.setUseBackgroundFill。先使用IFillFormat.setFillType 設為FillType.NotDefined 以移除任何明確的填色。此組合下,圖形會使用投影片背景而非其樣式填色。僅啟用此旗標並不會移除現有的明確填色。

這與FillType.NoFill 不同:背景填色會以投影片相應區域覆蓋圖形後方的物件,而無填色則讓這些物件保持可見。漸層會與投影片對齊,而不是在每個圖形內重新開始。

以下範例建立一個由投影片繼承的母片背景,並在三個矩形後方放置一條品紅色條紋。左側矩形使用背景,中央矩形無填色,右側矩形則從背景填色切換為明確的橙色填色。

import com.aspose.slides.*;
import android.graphics.Color;

Presentation presentation = new Presentation();
try {
    presentation.getSlideSize().setSize(720, 400, SlideSizeScaleType.DoNotScale);
    ISlide slide = presentation.getSlides().get_Item(0);

    // 從母片繼承漸層。
    slide.getBackground().setType(BackgroundType.NotDefined);
    slide.getLayoutSlide().getBackground().setType(BackgroundType.NotDefined);
    IBackground background = slide.getLayoutSlide().getMasterSlide().getBackground();
    background.setType(BackgroundType.OwnBackground);
    background.getFillFormat().setFillType(FillType.Gradient);
    IGradientFormat gradient = background.getFillFormat().getGradientFormat();
    gradient.setGradientShape(GradientShape.Linear);
    gradient.setLinearGradientAngle(0);
    gradient.getGradientStops().clear();
    gradient.getGradientStops().add(0, Color.rgb(0, 0, 139));
    gradient.getGradientStops().add(1, Color.rgb(135, 206, 250));

    // 首先加入此物件,使其顯示在矩形之後。
    IAutoShape band = slide.getShapes().addAutoShape(ShapeType.Rectangle, 25, 150, 670, 100);
    band.getFillFormat().setFillType(FillType.Solid);
    band.getFillFormat().getSolidFillColor().setColor(Color.MAGENTA);
    band.getLineFormat().getFillFormat().setFillType(FillType.NoFill);

    IAutoShape backgroundShape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 100, 160, 200);
    backgroundShape.getFillFormat().setFillType(FillType.NotDefined);
    backgroundShape.setUseBackgroundFill(true);

    IAutoShape noFillShape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 280, 100, 160, 200);
    noFillShape.getFillFormat().setFillType(FillType.NoFill);

    IAutoShape explicitShape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 510, 100, 160, 200);
    explicitShape.getFillFormat().setFillType(FillType.NotDefined);
    explicitShape.setUseBackgroundFill(true);

    // 在套用明確填色前先停用背景填色。
    explicitShape.setUseBackgroundFill(false);
    explicitShape.getFillFormat().setFillType(FillType.Solid);
    explicitShape.getFillFormat().getSolidFillColor().setColor(Color.rgb(255, 165, 0));

    presentation.save("background_fill.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

結果,從左至右:背景填色、無填色 與 明確填色。品紅條紋被左側與右側矩形遮蔽,但仍透過中間矩形可見。 背景填色、無填色與明確的橙色填色覆蓋於品紅條紋上

相同的行為適用於直接在投影片上定義或從版面繼承的背景。圖形使用投影片解析後的背景;此範例未定義投影片與版面的背景,使母片提供背景。

設定透明度

對於純色填色,顏色的 alpha 成分控制不透明度:0 表示完全透明,255 表示完全不透明。此範例對黃色矩形使用 alpha 值 128,使其約 50% 透明,讓底下的矩形仍可見。顏色透過IFillFormat.getSolidFillColor 指派。

import com.aspose.slides.*;
import android.graphics.Color;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    IAutoShape solidShape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 150, 75);

    IAutoShape transparentShape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 80, 80, 150, 75);
    transparentShape.getFillFormat().setFillType(FillType.Solid);
    transparentShape.getFillFormat().getSolidFillColor().setColor(Color.argb(128, 255, 255, 0));

    presentation.save("shape_transparency.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

結果: 透明的圖形

旋轉圖形

使用IShape.setRotation 於投影片平面旋轉圖形。角度以度為單位;此範例將矩形順時針旋轉 5 度,並儲存簡報。

import com.aspose.slides.*;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 150, 75);
    shape.setRotation(5);

    presentation.save("shape_rotation.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

結果: 圖形的旋轉

新增 3D 倒角效果

使用IShape.getThreeDFormat 為圖形賦予深度與倒角邊緣。此範例建立綠色橢圓並加上橙色輪廓,使用IThreeDFormat.setDepth 將深度設為 4 點,並透過IThreeDFormat.getBevelTop 套用寬高皆為 6 點的圓形上倒角。正面正交相機與三點光源使倒角可見。

import com.aspose.slides.*;
import android.graphics.Color;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Ellipse, 50, 50, 100, 100);
    shape.getFillFormat().setFillType(FillType.Solid);
    shape.getFillFormat().getSolidFillColor().setColor(Color.rgb(0, 128, 0));
    shape.getLineFormat().getFillFormat().setFillType(FillType.Solid);
    shape.getLineFormat().getFillFormat().getSolidFillColor().setColor(Color.rgb(255, 165, 0));
    shape.getLineFormat().setWidth(2.0);
    shape.getThreeDFormat().setDepth(4);
    shape.getThreeDFormat().getBevelTop().setBevelType(BevelPresetType.Circle);
    shape.getThreeDFormat().getBevelTop().setHeight(6);
    shape.getThreeDFormat().getBevelTop().setWidth(6);
    shape.getThreeDFormat().getCamera().setCameraType(CameraPresetType.OrthographicFront);
    shape.getThreeDFormat().getLightRig().setLightType(LightRigPresetType.ThreePt);
    shape.getThreeDFormat().getLightRig().setDirection(LightingDirection.Top);

    presentation.save("3D_bevel_effect.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

結果: 3D 倒角效果

新增 3D 旋轉效果

圖形的 3D 觀點由相機設定控制。此範例使用ICamera.setRotation 設定緯度、經度與旋轉角度分別為 40、35、20 度。透過ICamera.setCameraType 選取等距相機,並使用ILightRig.setLightType 設定平衡光源,然後儲存格式化的矩形。

import com.aspose.slides.*;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    IAutoShape autoShape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 150, 75);
    autoShape.getTextFrame().setText("Hello, Aspose!");
    autoShape.getThreeDFormat().getCamera().setCameraType(CameraPresetType.IsometricLeftUp);
    autoShape.getThreeDFormat().getCamera().setRotation(40, 35, 20);
    autoShape.getThreeDFormat().getLightRig().setLightType(LightRigPresetType.Balanced);

    presentation.save("3D_rotation_effect.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

結果: 3D 旋轉效果

控制圖形的黑白渲染

IShape.setBlackWhiteMode 方法指定在以黑白模式檢視或處理簡報時個別圖形的渲染方式。它本身不會啟用黑白顯示,也不會在正常彩色模式下改變圖形的填色、線條或其他格式。

使用BlackWhiteMode 類別中的值以選取所需行為。例如,Automatic 讓渲染應用程式自行決定轉換,Gray 與 LightGray 使用灰色,BlackWhite 只使用黑白,Black 與 White 強制單色,Color 保持正常顏色,Hidden 在黑白模式下省略圖形。NotDefined 表示未指派圖形層級的模式。

此範例建立橙色矩形,並使用IShape.setBlackWhiteMode 選取灰色著色。儲存的圖形在正常彩色顯示時保留橙色填色,並向遵守黑白顯示設定的應用程式請求灰色著色:

import com.aspose.slides.*;
import android.graphics.Color;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 200, 100);
    shape.getFillFormat().setFillType(FillType.Solid);
    shape.getFillFormat().getSolidFillColor().setColor(Color.rgb(255, 165, 0));
    shape.setBlackWhiteMode(BlackWhiteMode.Gray);

    presentation.save("shape_black_white_mode.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

在正常彩色模式下,矩形保留橙色填色。於黑白顯示工作流程中,因模式設定為 Gray,它會使用灰色著色。這讓您在保留全彩投影片的同時,為列印、預覽或其他遵循簡報黑白顯示設定的工作流程定義不同的外觀。

重設格式

使用ISlide.reset 可從相應的版面配置占位元復原投影片占位元的位置、大小與格式。此範例需要一個已存在且包含版面連結占位元的簡報,並在儲存前重設每張投影片。沒有相應版面占位元的圖形不會被重設。

import com.aspose.slides.*;

Presentation presentation = new Presentation("sample.pptx");
try {
    for (ISlide slide : presentation.getSlides()) {
        slide.reset();
    }

    presentation.save("reset_formatting.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

常見問題

圖形格式設定會影響最終簡報檔案大小嗎?

相較於嵌入的圖像與媒體,顏色、線條樣式與效果設定通常只會添加少量資料。圖片填色可能會因其圖像儲存在簡報中而增加檔案大小。

如何偵測投影片上具有相同格式的圖形,以便將它們分組?

比較每個圖形的關鍵格式屬性——填色、線條與效果設定。若所有對應值相符,則視為相同樣式,並在邏輯上將這些圖形分組,這可簡化之後的樣式管理。

我可以將一組自訂圖形樣式儲存至單獨檔案,以便在其他簡報中重複使用嗎?

可以。將具備所需樣式的範例圖形儲存於範本投影片或 .POTX 範本檔。建立新簡報時,開啟該範本,複製您需要的已樣式化圖形,並在需要的地方重新套用其格式設定。