We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

PPT幻灯片的背景读取错误

问题描述:
Aspose.Slide PPT幻灯片的背景读取错误

开发环境:Java8 Win10系统
工具包版本Aspose.Slide 19.11

使用幻灯片文件见附件:
圆.zip (227.8 KB)

原始幻灯片的背景效果如幻灯片所示,为如下效果幻灯片背景主题效果.png (64.0 KB)

读取出来的幻灯片背景效果如下图所示:
background280569240506400.jpg (32.6 KB)

可以看出两者效果对比有很大误差,使用的Java代码如下:

package com.xh.slides.examples.Slides.Background;

import com.aspose.slides.IBackground;
import com.aspose.slides.IFillFormatEffectiveData;
import com.aspose.slides.ISlide;
import com.aspose.slides.Presentation;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * @author xwj
 * @date 2019/12/11 0011 9:38
 * @description 获取幻灯片的主题信息
 */
public class GetThemeOfSlide {
    public static void main(String[] args) {
        Presentation pres = new Presentation("E:\\document\\圆.pptx");
        //读取第一张幻灯片
        ISlide slide = pres.getSlides().get_Item(0);
        //
        IBackground background = slide.getBackground();
        IFillFormatEffectiveData fillFormatEffectiveData = background.getFillFormat().getEffective();
        String picturePath = write(fillFormatEffectiveData.getPictureFillFormat().getPicture().getImage().getSystemImage());
        System.out.println(picturePath);
    }
    public static String write(BufferedImage bi) {
        String sourceName = "E:\\document\\background" + System.nanoTime() + ".jpg";
        try {
            ImageIO.write(bi, "jpeg", new File(sourceName));
        } catch (IOException e) {
            e.printStackTrace();
        }

        return sourceName;
    }
}

请问如何才能正确读取到PPT的背景效果呢?

@ciyuan9,
演示文稿上的这张幻灯片具有布局和母版,它们是自己的形状,因此这不是背景,而是母版,布局和幻灯片形状以及主题的组成。

您想要的可以这样实现:

我们创建了一个空的幻灯片并保存它,除了母版和版式之外,没有任何形状。

public static void GetBackground()
{

    Presentation pres = new Presentation("Theme.pptx");
    ISlide slide = pres.getSlides().get_Item(0);


    // User defined dimension
    int desiredX = 960;
    int desiredY = 720;

    // Getting scaled value of X and Y
    float ScaleX = (float) (1.0 / pres.getSlideSize().getSize().getWidth()) * desiredX;
    float ScaleY = (float) (1.0 / pres.getSlideSize().getSize().getHeight()) * desiredY;

    // Create a full scale image 
    ISlide emptySlide = pres.getSlides().addEmptySlide(slide.getLayoutSlide());
    BufferedImage image =  emptySlide.getThumbnail(ScaleX, ScaleY);

     // Save the image to disk in JPEG format
    try {
            ImageIO.write(image, "png", new File("SavedBackground.png"));

        }
    catch (IOException e) 
    {

    }

}
1 Like