package com.jtyjy.pdf2pptx.converter;

import com.jtyjy.pdf2pptx.support.FileUtil;
import org.apache.commons.io.FileUtils;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFPictureData;
import org.apache.poi.xslf.usermodel.XSLFPictureShape;
import org.apache.poi.xslf.usermodel.XSLFSlide;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.awt.geom.Rectangle2D;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

public class Png2Pptx {

    private static final Logger logger = LoggerFactory.getLogger(Png2Pptx.class);

    public static void main(String[] args) throws Exception {

    }

    public static void createPicturePptx(List<String> filePathNames,File dir) {
        XMLSlideShow ppt = new XMLSlideShow();
        String imageName = null;
        try {
            for (int i = 1; i <= filePathNames.size(); i++) {
                imageName = dir.getName().replace("_图片","")+"_"+i+".png";
                //创建幻灯片
                XSLFSlide slide = ppt.createSlide();
                //插入图片
                byte[] bt = FileUtils.readFileToByteArray(new File(dir.getAbsolutePath() + "\\" + imageName));
                XSLFPictureData xslfPictureData = ppt.addPicture(bt, XSLFPictureData.PictureType.PNG);
                XSLFPictureShape pic = slide.createPicture(xslfPictureData);
                pic.setAnchor(new Rectangle2D.Double(45.7, 50.8, 629, 439));
            }

            ppt.write(new FileOutputStream(dir.getParent() +"\\"+ dir.getName().replace("_图片", ".pptx")));
            logger.info("转换：{} 完成", dir.getName());
        } catch (Exception e) {
            logger.error("转换：{} 错误：{}", imageName, e);
        }

    }


}
