现代 API

介绍

历史上,Aspose Slides 依赖于 java.awt,并在公共 API 中包含以下类:

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

为了摆脱对这些类的依赖,我们添加了所谓的 “现代 API” - 即应使用替代已弃用 API 的 API,签名中包含对 Bitmap 的依赖。Canvas 被声明为已弃用,其支持从公共 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 Size(1920, 1080));
    try {
        // 将图像保存到磁盘。
        slideImage.save("slide1.jpeg", ImageFormat.Jpeg);
    } finally {
        if (slideImage != null) slideImage.dispose();
    }
} finally {
    if (pres != null) pres.dispose();
}

用现代 API 替换旧代码

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

旧版:

Presentation pres = new Presentation();
try {
    Bitmap slideImage = pres.getSlides().get_Item(0).getThumbnail(new Size(1920, 1080));
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream("image.png");
        slideImage.compress(android.graphics.Bitmap.CompressFormat.PNG, 100, fos);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } finally {
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
} finally {
    if (pres != null) pres.dispose();
}

新版:

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

获取幻灯片缩略图

使用已弃用 API 的代码:

Presentation pres = new Presentation("pres.pptx");
try {
    Bitmap slideImage = pres.getSlides().get_Item(0).getThumbnail();
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream("slide1.png");
        slideImage.compress(android.graphics.Bitmap.CompressFormat.PNG, 100, fos);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } finally {
        if (fos != null) {
            try {
                fos.close();
            } 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 {
    Bitmap shapeImage = pres.getSlides().get_Item(0).getShapes().get_Item(0).getThumbnail();
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream("shape.png");
        shapeImage.compress(android.graphics.Bitmap.CompressFormat.PNG, 100, fos);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } finally {
        if (fos != null) {
            try {
                fos.close();
            } 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 {
    Bitmap[] bitmaps = pres.getThumbnails(new RenderingOptions(), new Size(1980, 1028));
    for (int index = 0; index < bitmaps.length; index++)
    {
        android.graphics.Bitmap thumbnail = bitmaps[index];
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream("slide" + index + ".png");
            thumbnail.compress(android.graphics.Bitmap.CompressFormat.PNG, 100, fos);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } 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 Size(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;
    File file = new File("image.png");
    Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
    ppImage = pres.getImages().addImage(bitmap);

    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 中的替代

演示文稿

方法签名 替代方法签名
public final Bitmap[] getThumbnails(IRenderingOptions options) public final IImage[] getImages(IRenderingOptions options)
public final Bitmap[] getThumbnails(IRenderingOptions options, Size imageSize) public final IImage[] getImages(IRenderingOptions options, Size imageSize)
public final Bitmap[] getThumbnails(IRenderingOptions options, float scaleX, float scaleY) public final IImage[] getImages(IRenderingOptions options, float scaleX, float scaleY)
public final Bitmap[] getThumbnails(IRenderingOptions options, int[] slides) public final IImage[] getImages(IRenderingOptions options, int[] slides)
public final Bitmap[] getThumbnails(IRenderingOptions options, int[] slides, Size imageSize) public final IImage[] getImages(IRenderingOptions options, int[] slides, Size imageSize)
public final Bitmap[] getThumbnails(IRenderingOptions options, int[] slides, float scaleX, float scaleY) public final IImage[] getImages(IRenderingOptions options, int[] slides, float scaleX, float scaleY)

形状

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

幻灯片

方法签名 替代方法签名
public final Bitmap getThumbnail() public final IImage getImage()
public final Bitmap getThumbnail(Size imageSize) public final IImage getImage(Size imageSize)
public final Bitmap getThumbnail(float scaleX, float scaleY) public final IImage getImage(float scaleX, float scaleY)
public final Bitmap getThumbnail(IRenderingOptions options) public final IImage getImage(IRenderingOptions options)
public final Bitmap getThumbnail(IRenderingOptions options, Size imageSize) public final IImage getImage(IRenderingOptions options, Size imageSize)
public final Bitmap getThumbnail(IRenderingOptions options, float scaleX, float scaleY) public final IImage getImage(IRenderingOptions options, float scaleX, float scaleY)
public final Bitmap getThumbnail(ITiffOptions options) public final IImage getImage(ITiffOptions options)
public final void renderToGraphics(IRenderingOptions options, Canvas graphics) 将被完全删除
public final void renderToGraphics(IRenderingOptions options, Canvas graphics, Size renderingSize) 将被完全删除
public final void renderToGraphics(IRenderingOptions options, Canvas graphics, float scaleX, float scaleY) 将被完全删除

输出

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

ImageCollection

方法签名 替代方法签名
public final IPPImage addImage(Bitmap image) public final IPPImage addImage(IImage image)

PPImage

方法签名 替代方法签名
public final Bitmap getSystemImage() public final IImage getImage()

PatternFormat

方法签名 替代方法签名
public final Bitmap getTileImage(Integer styleColor) public final IImage getTile(Integer styleColor)
public final Bitmap getTileImage(Integer background, Integer foreground) public final IImage getTile(Integer background, Integer foreground)

PatternFormatEffectiveData

方法签名 替代方法签名
public final Bitmap getTileImage(Integer background, Integer foreground) public final IImage getTileIImage(Integer background, Integer foreground)

将停止对 Canvas 的 API 支持

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

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

幻灯片