现代 API

介绍

从历史上看,Aspose Slides 依赖于 java.awt,并在公共 API 中包含以下来自该库的类:

从 24.4 版本开始,该公共 API 被声明为已弃用。

为了摆脱对这些类的依赖,我们添加了所谓的“现代 API”——即应使用代替已弃用 API 的新 API,其签名不再依赖于 BufferedImage。Graphics2D 被声明为已弃用,且其支持已从公共 Slides API 中移除。

带有对 System.Drawing 依赖的弃用公共 API 将在 24.8 版本中移除。

现代 API

已将以下类和枚举添加到公共 API 中:

  • IImage - 表示光栅或矢量图像。
  • ImageFormat - 表示图像的文件格式。
  • Images - 用于实例化和处理 IImage 接口的方法。

请注意,IImage 是可处置的(它实现了 IDisposable 接口,应该用 using 包裹使用或以其他方便的方式处置)。

使用新 API 的典型场景可能如下所示:

from asposeslides.api import Presentation, SaveFormat, Images, ShapeType, ImageFormat
from javax.imageio import ImageIO
from java.io import File
from java.awt import Dimension

pres = Presentation();

# 从磁盘上的文件实例化一个可处置的 IImage 实例。
image = Images.fromFile("image.png");

# 通过将 IImage 实例添加到演示文稿的图像中来创建 PowerPoint 图像。
ppImage = pres.getImages().addImage(image);
image.dispose();

# 在幻灯片 #1 上添加一个图片形状
pres.getSlides().get_Item(0).getShapes().addPictureFrame(ShapeType.Rectangle, 10, 10, 100, 100, ppImage);

# 获取表示幻灯片 #1 的 IImage 实例。
slideImage = pres.getSlides().get_Item(0).getImage(Dimension(1920, 1080));

# 将图像保存到磁盘。
slideImage.save("slide1.jpeg", ImageFormat.Jpeg);
slideImage.dispose();

pres.dispose();

用现代 API 替换旧代码

总的来说,您需要将使用 ImageIO 调用旧方法的代码替换为新方法。

旧:

image_format = "PNG"
buffImage = pres.getSlides().get_Item(0).getThumbnail(Dimension(1920, 1080))
ImageIO.write(buffImage, image_format, File("image.png"))

新:

slideImage = pres.getSlides().get_Item(0).getImage(Dimension(1920, 1080));
slideImage.save("image.png", ImageFormat.Png);

获取幻灯片缩略图

使用已弃用 API 的代码:

from asposeslides.api import Presentation
from javax.imageio import ImageIO
from java.io import File
from java.awt import Dimension


pres = Presentation("pres.pptx");

slideImage = pres.getSlides().get_Item(0).getThumbnail();
image_format = "PNG"
ImageIO.write(slideImage, image_format, File("slide1.png"))

pres.dispose();

现代 API:

from asposeslides.api import Presentation, ImageFormat


pres = Presentation("pres.pptx");

slideImage = pres.getSlides().get_Item(0).getImage();
slideImage.save("slide1.png", ImageFormat.Png);
slideImage.dispose();

pres.dispose();

获取形状缩略图

使用已弃用 API 的代码:

from asposeslides.api import Presentation
from javax.imageio import ImageIO
from java.io import File
from java.awt import Dimension


pres = Presentation("pres.pptx");

shapeImage = pres.getSlides().get_Item(0).getShapes().get_Item(0).getThumbnail();
image_format = "PNG"
ImageIO.write(shapeImage, image_format, File("shape.png"))

pres.dispose();

现代 API:

from asposeslides.api import Presentation, ImageFormat


pres = Presentation("pres.pptx");

shapeImage = pres.getSlides().get_Item(0).getShapes().get_Item(0).getImage();
shapeImage.save("shape.png", ImageFormat.Png);
shapeImage.dispose();

pres.dispose();

获取演示文稿缩略图

使用已弃用 API 的代码:

from asposeslides.api import Presentation, RenderingOptions
from javax.imageio import ImageIO
from java.io import File
from java.awt import Dimension


pres = Presentation("pres.pptx");

image_format = "PNG"
rendering_options = RenderingOptions();
bitmaps = pres.getThumbnails(rendering_options, Dimension(1980, 1028));

for index in range(bitmaps.length):
    thumbnail = bitmaps[index];
    ImageIO.write(thumbnail, "PNG", File("slide" + str(index) + ".png"));
    
pres.dispose();

现代 API:

from asposeslides.api import Presentation, RenderingOptions, ImageFormat
from java.awt import Dimension


pres = Presentation("pres.pptx");

rendering_options = RenderingOptions();
images = pres.getImages(rendering_options, Dimension(1980, 1028));

for index in range(images.length):
    thumbnail = images[index];
    thumbnail.save("slide" + str(index) + ".png", ImageFormat.Png);
    thumbnail.dispose();

pres.dispose();

向演示文稿添加图片

使用已弃用 API 的代码:

from asposeslides.api import Presentation, ShapeType
from javax.imageio import ImageIO
from java.io import File


pres = Presentation();

bufferedImages = ImageIO.read(File("image.png"));
ppImage = pres.getImages().addImage(bufferedImages);

pres.getSlides().get_Item(0).getShapes().addPictureFrame(ShapeType.Rectangle, 10, 10, 100, 100, ppImage);

pres.dispose();

现代 API:

from asposeslides.api import Presentation, ShapeType, Images
from java.awt import Dimension


pres = Presentation();

image = Images.fromFile("image.png");
ppImage = pres.getImages().addImage(image);
image.dispose();

pres.getSlides().get_Item(0).getShapes().addPictureFrame(ShapeType.Rectangle, 10, 10, 100, 100, ppImage);

pres.dispose();

要删除的方法及其在现代 API 中的替换

Presentation

方法签名 替换方法签名
public final BufferedImage[] getThumbnails(IRenderingOptions options) public final IImage[] getImages(IRenderingOptions options)
public final BufferedImage[] getThumbnails(IRenderingOptions options, float scaleX, float scaleY) public final IImage[] getImages(IRenderingOptions options, float scaleX, float scaleY)
public final BufferedImage[] getThumbnails(IRenderingOptions options, int[] slides) public final IImage[] getImages(IRenderingOptions options, int[] slides)
public final BufferedImage[] getThumbnails(IRenderingOptions options, int[] slides, float scaleX, float scaleY) public final IImage[] getImages(IRenderingOptions options, int[] slides, float scaleX, float scaleY)
public final BufferedImage[] getThumbnails(IRenderingOptions options, int[] slides, Dimension imageSize) public final IImage[] getImages(IRenderingOptions options, int[] slides, Dimension imageSize)
public final BufferedImage[] getThumbnails(IRenderingOptions options, Dimension imageSize) public final IImage[] getImages(IRenderingOptions options, Dimension imageSize)

Shape

方法签名 替换方法签名
public final BufferedImage getThumbnail() public final IImage getImage()
public final BufferedImage getThumbnail(int bounds, float scaleX, float scaleY) public final IImage getImage(int bounds, float scaleX, float scaleY)

Slide

方法签名 替换方法签名
public final BufferedImage getThumbnail() public final IImage getImage()
public final BufferedImage getThumbnail(float scaleX, float scaleY) public final IImage getImage(float scaleX, float scaleY)
public final BufferedImage getThumbnail(IRenderingOptions options) public final IImage getImage(IRenderingOptions options)
public final BufferedImage getThumbnail(IRenderingOptions options, float scaleX, float scaleY) public final IImage getImage(IRenderingOptions options)
public final BufferedImage getThumbnail(IRenderingOptions options, Dimension imageSize) public final IImage getImage(IRenderingOptions options, Dimension imageSize)
public final BufferedImage getThumbnail(ITiffOptions options) public final IImage getImage(ITiffOptions options)
public final BufferedImage getThumbnail(Dimension imageSize) public final IImage getImage(Dimension imageSize)
public final void renderToGraphics(IRenderingOptions options, Graphics2D graphics) 将会完全删除
public final void renderToGraphics(IRenderingOptions options, Graphics2D graphics, float scaleX, float scaleY) 将会完全删除
public final void renderToGraphics(IRenderingOptions options, Graphics2D graphics, Dimension renderingSize) 将会完全删除

Output

方法签名 替换方法签名
public final IOutputFile add(String path, BufferedImage image) public final IOutputFile add(String path, IImage image)

ImageCollection

方法签名 替换方法签名
public final IPPImage addImage(BufferedImage image) public final IPPImage addImage(IImage image)

PPImage

方法签名 替换方法签名
public final BufferedImage getSystemImage() public final IImage getImage()

PatternFormat

方法签名 替换方法签名
public final BufferedImage getTileImage(Color styleColor) public final IImage getTile(Color styleColor)
public final BufferedImage getTileImage(Color background, Color foreground) public final IImage getTile(Color background, Color foreground)

PatternFormatEffectiveData

方法签名 替换方法签名
public final java.awt.image.BufferedImage getTileImage(Color background, Color foreground) public final IImage getTileIImage(Color background, Color foreground)

将不再支持 Graphics2D API

带有 Graphics2D 的方法被声明为已弃用,其支持将从公共 API 中移除。

使用该 API 的部分将被移除:

Slide