使用 Java 在 PowerPoint 中應用 3D 效果

PowerPoint 中的 3D 效果用於使演示文稿更具吸引力並吸引用戶的注意力 因此,您可能會遇到以編程方式將 3D 對象添加到演示文稿的要求。在本文中,您將學習如何使用 Java 在 PowerPoint PPT 或 PPTX 中創建 3D 效果。我們將向您展示如何創建 3D 文本和形狀以及如何將 3D 效果應用於圖像。

在 PowerPoint PPT 中應用 3D 效果的 Java API

Aspose.Slides for Java 是一個強大的 API,封裝了廣泛的演示操作功能。使用 API,您可以創建交互式演示文稿並無縫操作現有的 PPT/PPTX 文件。要在 PowerPoint 演示文稿中創建 3D 效果,我們將使用此 API。

您可以 下載 API 的 JAR 或使用以下 Maven 配置安裝它。

存儲庫:

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>https://repository.aspose.com/repo/</url>
</repository>

依賴:

<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-slides</artifactId>
    <version>22.1</version>
    <classifier>jdk16</classifier>
</dependency>

用 Java 在 PowerPoint 中創建 3D 文本

以下是使用 Java 在 PowerPoint PPT 中創建 3D 文本片段的步驟。

下面的代碼示例顯示瞭如何在 Java 中的 PowerPoint PPT 中創建 3D 文本。

// 創建演示文稿
Presentation pres = new Presentation();
try {
    // 添加矩形
    IAutoShape shape = pres.getSlides().get_Item(0).getShapes().addAutoShape(ShapeType.Rectangle, 200, 150, 200, 200);

    // 設置文字
    shape.getFillFormat().setFillType(FillType.NoFill);
    shape.getLineFormat().getFillFormat().setFillType(FillType.NoFill);
    shape.getTextFrame().setText("3D Text");

    // 添加文本部分並設置其屬性
    Portion portion = (Portion)shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0);
    portion.getPortionFormat().getFillFormat().setFillType(FillType.Pattern);
    portion.getPortionFormat().getFillFormat().getPatternFormat().getForeColor().setColor(new Color(255, 140, 0));
    portion.getPortionFormat().getFillFormat().getPatternFormat().getBackColor().setColor(Color.WHITE);
    portion.getPortionFormat().getFillFormat().getPatternFormat().setPatternStyle(PatternStyle.LargeGrid);

    // 設置形狀文本的字體大小
    shape.getTextFrame().getParagraphs().get_Item(0).getParagraphFormat().getDefaultPortionFormat().setFontHeight(128);

    // 獲取文本框
    ITextFrame textFrame = shape.getTextFrame();

    // 設置“Arch Up”藝術字變換效果
    textFrame.getTextFrameFormat().setTransform(TextShapeType.ArchUp);

    // 應用 3D 效果
    textFrame.getTextFrameFormat().getThreeDFormat().setExtrusionHeight(3.5f);
    textFrame.getTextFrameFormat().getThreeDFormat().setDepth(3);
    textFrame.getTextFrameFormat().getThreeDFormat().setMaterial(MaterialPresetType.Plastic);
    textFrame.getTextFrameFormat().getThreeDFormat().getLightRig().setDirection(LightingDirection.Top);
    textFrame.getTextFrameFormat().getThreeDFormat().getLightRig().setLightType(LightRigPresetType.Balanced);
    textFrame.getTextFrameFormat().getThreeDFormat().getLightRig().setRotation(0, 0, 40);		 
    textFrame.getTextFrameFormat().getThreeDFormat().getCamera().setCameraType(CameraPresetType.PerspectiveContrastingRightFacing);

    // 保存演示文稿
    pres.save("3D-Text.pptx", SaveFormat.Pptx);
} finally {
    if (pres != null) pres.dispose();
}

以下屏幕截圖顯示了上述代碼示例的輸出。

用 Java 在 PowerPoint 中創建 3D 文本

用 Java 在 PowerPoint 中創建 3D 形狀

與文本類似,您可以將 3D 效果應用於 PowerPoint 演示文稿中的形狀。以下是使用 Java 在 PowerPoint PPT 中創建 3D 形狀的步驟。

以下代碼示例顯示如何使用 Java 將 3D 效果應用於 PowerPoint 中的形狀。

// 創建演示文稿
Presentation pres = new Presentation();
try {
    // 添加矩形
    IAutoShape shape = pres.getSlides().get_Item(0).getShapes().addAutoShape(ShapeType.Rectangle, 200, 150, 200, 200);

    // 為形狀設置文本
    shape.getTextFrame().setText("3D");
    shape.getTextFrame().getParagraphs().get_Item(0).getParagraphFormat().getDefaultPortionFormat().setFontHeight(64);

    // 應用 3D 效果
    shape.getThreeDFormat().getCamera().setCameraType(CameraPresetType.OrthographicFront);
    shape.getThreeDFormat().getCamera().setRotation(20, 30, 40);
    shape.getThreeDFormat().getLightRig().setLightType(LightRigPresetType.Flat);
    shape.getThreeDFormat().getLightRig().setDirection(LightingDirection.Top);
    shape.getThreeDFormat().setMaterial(MaterialPresetType.Flat);
    shape.getThreeDFormat().setExtrusionHeight(100);
    shape.getThreeDFormat().getExtrusionColor().setColor(Color.BLUE);

    // 保存演示文稿
    pres.save("3D-Shape.pptx", SaveFormat.Pptx);
} finally {
    if (pres != null) pres.dispose();
}

以下是我們執行這段代碼後得到的 3D 形狀。

用 Java 在 PowerPoint 中創建 3D 形狀

為 3D 形狀創建漸變

您還可以按照以下步驟對形狀應用漸變效果。

以下代碼示例演示如何將漸變效果應用於 PowerPoint PPT 中的形狀。

// 創建演示文稿
Presentation pres = new Presentation();
try {
    // 添加矩形
    IAutoShape shape = pres.getSlides().get_Item(0).getShapes().addAutoShape(ShapeType.Rectangle, 200, 150, 200, 200);

    // 為形狀設置文本
    shape.getTextFrame().setText("3D");
    shape.getTextFrame().getParagraphs().get_Item(0).getParagraphFormat().getDefaultPortionFormat().setFontHeight(64);

    shape.getFillFormat().setFillType(FillType.Gradient);
    shape.getFillFormat().getGradientFormat().getGradientStops().add(0, Color.BLUE);
    shape.getFillFormat().getGradientFormat().getGradientStops().add(100, Color.MAGENTA);

    // 應用 3D 效果
    shape.getThreeDFormat().getCamera().setCameraType(CameraPresetType.OrthographicFront);
    shape.getThreeDFormat().getCamera().setRotation(10, 20, 30);
    shape.getThreeDFormat().getLightRig().setLightType(LightRigPresetType.Flat);
    shape.getThreeDFormat().getLightRig().setDirection(LightingDirection.Top);
    shape.getThreeDFormat().setExtrusionHeight(150);
    shape.getThreeDFormat().getExtrusionColor().setColor(new Color(255, 140, 0));

    // 保存演示文稿
    pres.save("3D-Shape-Gradient.pptx", SaveFormat.Pptx);
} finally {
    if (pres != null) pres.dispose();
}

以下是應用漸變效果後的 3D 形狀。

在 Java 中為 PPT 中的 3D 形狀創建漸變

在 Java 中將 3D 效果應用於 PowerPoint 中的圖像

Aspose.Slides for Java 還允許您對圖像應用 3D 效果。以下是在 Java 中執行此操作的步驟。

以下是使用 Java 對 PPT 中的圖像應用 3D 效果的步驟。

// 創建演示文稿
Presentation pres = new Presentation();
try {
    // 添加矩形
    IAutoShape shape = pres.getSlides().get_Item(0).getShapes().addAutoShape(ShapeType.Rectangle, 200, 150, 200, 200);

    // 為形狀設置圖像
    shape.getFillFormat().setFillType(FillType.Picture);
    IPPImage picture = null;
    try {
        picture = pres.getImages().addImage(Files.readAllBytes(Paths.get("tiger.bmp")));
    } catch (IOException e) { }
    shape.getFillFormat().getPictureFillFormat().getPicture().setImage(picture);
    shape.getFillFormat().getPictureFillFormat().setPictureFillMode(PictureFillMode.Stretch);

    // 應用 3D 效果
    shape.getThreeDFormat().getCamera().setCameraType(CameraPresetType.OrthographicFront);
    shape.getThreeDFormat().getCamera().setRotation(10, 20, 30);
    shape.getThreeDFormat().getLightRig().setLightType(LightRigPresetType.Flat);
    shape.getThreeDFormat().getLightRig().setDirection(LightingDirection.Top);
    shape.getThreeDFormat().setExtrusionHeight(150);
    shape.getThreeDFormat().getExtrusionColor().setColor(Color.GRAY);

    // 保存演示文稿
    pres.save("3D-Image.pptx", SaveFormat.Pptx);
} finally {
    if (pres != null) pres.dispose();
}

以下是我們應用 3D 效果後得到的結果圖像。

在 Java 中將 3D 效果應用於 PowerPoint 中的圖像

獲得免費許可證

您可以獲得免費的臨時許可證,以在沒有評估限制的情況下使用 Aspose.Slides for Java。

結論

在本文中,您了解瞭如何使用 Java 在 PowerPoint PPT/PPTX 中應用 3D 效果。在代碼示例的幫助下,我們演示瞭如何創建 3D 文本或形狀以及如何將 3D 效果應用於 PPT 或 PPTX 演示文稿中的圖像。您可以訪問 文檔 來探索更多關於 Aspose.Slides for Java 的信息。此外,您可以將您的問題或疑問發佈到我們的論壇

也可以看看