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

var pres = new aspose.slides.Presentation();
try {
    var ppImage;
    // 从磁盘文件实例化 IImage 的可处置实例。
    var image = aspose.slides.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(aspose.slides.ShapeType.Rectangle, 10, 10, 100, 100, ppImage);

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

用现代 API 替换旧代码

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

旧:

var imageio = java.import("javax.imageio.ImageIO");
var size = java.newInstanceSync("java.awt.Dimension", 1920, 1080);
var slideImage = pres.getSlides().get_Item(0).getThumbnail(size);
var file = java.newInstanceSync("java.io.File", "image.png");
imageio.write(slideImage, "PNG", file);

新:

var size = java.newInstanceSync("java.awt.Dimension", 1920, 1080);
var slideImage = pres.getSlides().get_Item(0).getImage(size);
slideImage.save("image.png", aspose.slides.ImageFormat.Png);
slideImage.dispose();

获取幻灯片缩略图

使用过时 API 的代码:

var pres = new aspose.slides.Presentation("pres.pptx");
try {
    var slideImage = pres.getSlides().get_Item(0).getThumbnail();
    var imageio = java.import("javax.imageio.ImageIO");
    var file = java.newInstanceSync("java.io.File", "slide1.png");
    imageio.write(slideImage, "PNG", file);
} finally {
    if (pres != null) pres.dispose();
}

现代 API:

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

获取形状缩略图

使用过时 API 的代码:

var pres = new aspose.slides.Presentation("pres.pptx");
try {
    var shapeImage = pres.getSlides().get_Item(0).getShapes().get_Item(0).getThumbnail();
    var imageio = java.import("javax.imageio.ImageIO");
    var file = java.newInstanceSync("java.io.File", "shape.png");
    imageio.write(shapeImage, "PNG", file);
} finally {
    if (pres != null) pres.dispose();
}

现代 API:

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

获取演示文稿缩略图

使用过时 API 的代码:

var pres = new aspose.slides.Presentation("pres.pptx");
try {
    var size = java.newInstanceSync("java.awt.Dimension", 1980, 1028);
    var bitmaps = pres.getThumbnails(new aspose.slides.RenderingOptions(), size);
    for (var index = 0; index < bitmaps.length; index++)
    {
        var thumbnail = bitmaps[index];
        var imageio = java.import("javax.imageio.ImageIO");
        var file = java.newInstanceSync("java.io.File", "slide" + index + ".png");
        imageio.write(thumbnail, "PNG", file);
    }
} finally {
    if (pres != null) pres.dispose();
}

现代 API:

var pres = new aspose.slides.Presentation("pres.pptx");
try {
    var size = java.newInstanceSync("java.awt.Dimension", 1980, 1028);
    var images = pres.getImages(new aspose.slides.RenderingOptions(), size);
    try
    {
        for (var index = 0; index < images.length; index++)
        {
            var thumbnail = images[index];
            thumbnail.save("slide" + index + ".png", aspose.slides.ImageFormat.Png);
        }
    }
    finally
    {
        images.forEach(item => {item.dispose();});
    }
} finally {
    if (pres != null) pres.dispose();
}

向演示文稿添加图片

使用过时 API 的代码:

var pres = new aspose.slides.Presentation();
try {
    var imageio = java.import("javax.imageio.ImageIO");
    var file = java.newInstanceSync("java.io.File", "image.png");
    var bufferedImages = imageio.read(file);
    var ppImage = pres.getImages().addImage(bufferedImages);

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

现代 API:

var pres = new aspose.slides.Presentation();
try {
    var image = aspose.slides.Images.fromFile("image.png");
    var ppImage = pres.getImages().addImage(image);
    image.dispose();

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

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

演示文稿

方法签名 替换方法签名
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)

形状

方法签名 替换方法签名
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)

幻灯片

方法签名 替换方法签名
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) 将完全删除

输出

方法签名 替换方法签名
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)

API 对 Graphics2D 的支持将被中止

包含 Graphics2D 的方法被声明为过时,其支持将从公共 API 中移除。

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

幻灯片