![使用 Java 在 PowerPoint 中應用 3D 效果](images/Apply-3D-Effects-in-PowerPoint.psd_.png#center)
PowerPoint 中的 3D 效果用於使演示文稿更具吸引力並吸引用戶的注意力 因此,您可能會遇到以編程方式將 3D 對象添加到演示文稿的要求。在本文中,您將學習如何使用 Java 在 PowerPoint PPT 或 PPTX 中創建 3D 效果。我們將向您展示如何創建 3D 文本和形狀以及如何將 3D 效果應用於圖像。
- 用於在 PowerPoint 中創建 3D 效果的 Java API
- 用 Java 在 PowerPoint 中創建 3D 文本
- 用 Java 在 PowerPoint 中創建 3D 形狀
- 為 3D 形狀設置漸變
- 在 PowerPoint 中對圖像應用 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 文本片段的步驟。
- 首先,創建一個新的 PPT 或使用 Presentation 類加載現有的 PPT。
- 然後,使用 addAutoShape() 方法添加一個新的矩形。
- 設置形狀的屬性,例如填充類型、文本等。
- 將形狀內的文本引用到 Portion 對像中。
- 將格式應用於文本部分。
- 獲取 TextFrame 內部形狀的引用。
- 使用 TextFrame.getTextFrameFormat().getThreeDFormat() 方法返回的 IThreeDFormat 應用 3D 效果。
- 最後,使用 Presentation.save(String, SaveFormat) 方法保存演示文稿。
下面的代碼示例顯示瞭如何在 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 文本](images/Create-3D-Text-in-PPT.png#center)
用 Java 在 PowerPoint 中創建 3D 形狀
與文本類似,您可以將 3D 效果應用於 PowerPoint 演示文稿中的形狀。以下是使用 Java 在 PowerPoint PPT 中創建 3D 形狀的步驟。
- 首先,使用 Presentation 類創建一個新的 PPT。
- 使用 addAutoShape() 方法添加一個新的矩形。
- 使用 IAutoShape.getTextFrame.setText() 方法設置形狀的文本。
- 使用 IAutoShape.getThreeDFormat() 方法返回的 IThreeDFormat 將 3D 效果應用於形狀。
- 最後,使用 Presentation.save(String, SaveFormat) 方法保存演示文稿。
以下代碼示例顯示如何使用 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 形狀](images/Create-3D-Shape-in-PPT.png#center)
為 3D 形狀創建漸變
您還可以按照以下步驟對形狀應用漸變效果。
- 首先,使用 Presentation 類創建一個新的 PPT。
- 使用 addAutoShape() 方法添加一個新的矩形。
- 使用 IAutoShape.getTextFrame().setText() 屬性設置形狀的文本。
- 將形狀的填充類型設置為 FillType.Gradient 並設置漸變顏色。
- 使用 IAutoShape.getThreeDFormat() 方法返回的 IThreeDFormat 將 3D 效果應用於形狀。
- 最後,使用 Presentation.save(String, SaveFormat) 方法保存演示文稿。
以下代碼示例演示如何將漸變效果應用於 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 形狀創建漸變](images/Create-3D-Shape-Gradient-in-PPT-Slide.png#center)
在 Java 中將 3D 效果應用於 PowerPoint 中的圖像
Aspose.Slides for Java 還允許您對圖像應用 3D 效果。以下是在 Java 中執行此操作的步驟。
- 使用 Presentation 類創建一個新的 PPT。
- 使用 addAutoShape() 方法添加一個新的矩形。
- 將形狀的填充類型設置為 FillType.Picture 並添加圖像。
- 使用 IAutoShape.getThreeDFormat() 方法返回的 IThreeDFormat 將 3D 效果應用於形狀。
- 使用 Presentation.save(String, SaveFormat) 方法保存演示文稿。
以下是使用 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 中的圖像](images/Create-3D-Image-in-PPT.png#center)
獲得免費許可證
您可以獲得免費的臨時許可證,以在沒有評估限制的情況下使用 Aspose.Slides for Java。
結論
在本文中,您了解瞭如何使用 Java 在 PowerPoint PPT/PPTX 中應用 3D 效果。在代碼示例的幫助下,我們演示瞭如何創建 3D 文本或形狀以及如何將 3D 效果應用於 PPT 或 PPTX 演示文稿中的圖像。您可以訪問 文檔 來探索更多關於 Aspose.Slides for Java 的信息。此外,您可以將您的問題或疑問發佈到我們的論壇。