现代 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 的典型场景可能如下所示:

Presentation pres = new Presentation();
try {
    IPPImage ppImage;
    // 从磁盘上的文件实例化 IImage 的可处理实例。
    IImage image = Images.fromFile("image.png");
    try {
        // 通过将 IImage 的实例添加到演示文稿的图像来创建 PowerPoint 图像。
        ppImage = pres.getImages().addImage(image);
    } finally {
        if (image != null) image.dispose();
    }

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

    // 获取表示幻灯片 #1 的 IImage 实例。
    IImage slideImage = pres.getSlides().get_Item(0).getImage(new Dimension(1920, 1080));
    try {
        // 在磁盘上保存图像。
        slideImage.save("slide1.jpeg", ImageFormat.Jpeg);
    } finally {
        if (slideImage != null) slideImage.dispose();
    }
} finally {
    if (pres != null) pres.dispose();
}

用现代 API 替换旧代码

一般来说,您需要用新方法替换对旧方法的调用,旧方法使用 ImageIO。

旧:

BufferedImage slideImage = pres.getSlides().get_Item(0).getThumbnail(new Dimension(1920, 1080));
try {
    ImageIO.write(slideImage, "PNG", new File("image.png"));
} catch (IOException e) {
    e.printStackTrace();
}

新:

IImage slideImage = pres.getSlides().get_Item(0).getImage(new Dimension(1920, 1080));
try {
    slideImage.save("image.png", ImageFormat.Png);
} finally {
    if (slideImage != null) slideImage.dispose();
}

获取幻灯片缩略图

使用已弃用 API 的代码:

Presentation pres = new Presentation("pres.pptx");
try {
    BufferedImage slideImage = pres.getSlides().get_Item(0).getThumbnail();
    try {
        ImageIO.write(slideImage, "PNG", new File("slide1.png"));
    } catch (IOException e) {
        e.printStackTrace();
    }
} finally {
    if (pres != null) pres.dispose();
}

现代 API:

Presentation pres = new Presentation("pres.pptx");
try {
    IImage slideImage = pres.getSlides().get_Item(0).getImage();
    try {
        slideImage.save("slide1.png", ImageFormat.Png);
    } finally {
        if (slideImage != null) slideImage.dispose();
    }
} finally {
    if (pres != null) pres.dispose();
}

获取形状缩略图

使用已弃用 API 的代码:

Presentation pres = new Presentation("pres.pptx");
try {
    BufferedImage shapeImage = pres.getSlides().get_Item(0).getShapes().get_Item(0).getThumbnail();
    try {
        ImageIO.write(shapeImage, "PNG", new File("shape.png"));
    } catch (IOException e) {
        e.printStackTrace();
    }
} finally {
    if (pres != null) pres.dispose();
}

现代 API:

Presentation pres = new Presentation("pres.pptx");
try {
    IImage shapeImage = pres.getSlides().get_Item(0).getShapes().get_Item(0).getImage();
    try {
        shapeImage.save("shape.png");
    } finally {
        if (shapeImage != null) shapeImage.dispose();
    }
} finally {
    if (pres != null) pres.dispose();
}

获取演示文稿缩略图

使用已弃用 API 的代码:

Presentation pres = new Presentation("pres.pptx");
try {
    BufferedImage[] bitmaps = pres.getThumbnails(new RenderingOptions(), new Dimension(1980, 1028));
    for (int index = 0; index < bitmaps.length; index++)
    {
        try 
        {
            BufferedImage thumbnail = bitmaps[index];
            ImageIO.write(thumbnail, "PNG", new File("slide" + index + ".png"));
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }
    }
} finally {
    if (pres != null) pres.dispose();
}

现代 API:

Presentation pres = new Presentation("pres.pptx");
try {
    IImage[] images = pres.getImages(new RenderingOptions(), new Dimension(1980, 1028));
    try
    {
        for (int index = 0; index < images.length; index++)
        {
            IImage thumbnail = images[index];
            thumbnail.save("slide" + index + ".png", ImageFormat.Png);
        }
    }
    finally
    {
        for (IImage image : images)
        {
            image.dispose();
        }
    }
} finally {
    if (pres != null) pres.dispose();
}

向演示文稿添加图片

使用已弃用 API 的代码:

Presentation pres = new Presentation();
try {
    IPPImage ppImage = null;
    try {
        BufferedImage bufferedImages = ImageIO.read(new File("image.png"));
        ppImage = pres.getImages().addImage(bufferedImages);
    } catch (IOException e) {
        e.printStackTrace();
    }

    pres.getSlides().get_Item(0).getShapes().addPictureFrame(ShapeType.Rectangle, 10, 10, 100, 100, ppImage);
} finally {
    if (pres != null) pres.dispose();
}

现代 API:

Presentation pres = new Presentation();
try {
    IPPImage ppImage;
    IImage image = Images.fromFile("image.png");
    try {
        ppImage = pres.getImages().addImage(image);
    } finally {
        if (image != null) image.dispose();
    }

    pres.getSlides().get_Item(0).getShapes().addPictureFrame(ShapeType.Rectangle, 10, 10, 100, 100, ppImage);
} finally {
    if (pres != null) 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