Extracting images from pptx with apache poi

后端 未结 2 1400
遇见更好的自我
遇见更好的自我 2020-12-12 05:31

I\'m trying to extract slides from a ppt file with Apache POI, there is no problem in that, but now I intend to open pptx files and do the same, do

相关标签:
2条回答
  • 2020-12-12 05:49

    Here's the way to do it in VBS, Maybe you can convert :

    Sub SaveAllPictures()
        Dim ap As Presentation: Set ap = ActivePresentation
        Dim savePath As String
        savePath = "C:\Users\me\Desktop\files\"
        Dim i As Integer
        Dim sl As Slide
        Dim sh As Shape
        For Each sl In ap.Slides
            For Each sh In sl.Shapes
                If sh.Type = msoPicture Then
                    sh.Export PathName:=savePath & sh.Name & CStr(i) & ".png", Filter:=ppShapeFormatPNG
                    i = i + 1
                End If
            Next
        Next
    End Sub
    
    0 讨论(0)
  • 2020-12-12 05:57

    Although you can refer to some example code from the POI project itself below is what you should be looking for; Hope this helps;

    private ImageIcon generateFromPPTX(int index) {
        ImageIcon icon = null;
        XMLSlideShow slideShowPPTX = null;
        FileInputStream pptInputStream = null;
        XSLFSlide [] allSlides = null;
        XSLFSlide singleSlide = null;
        BufferedImage memoryImage = null;
        Graphics2D graphics = null;
        try{
            pptInputStream = new FileInputStream("somepptx-file.pptx");
            slideShowPPTX = new XMLSlideShow(pptInputStream);
            allSlides = slideShowPPTX.getSlides();
            if(allSlides == null || allSlides.length == 0) {
                System.out.println("Empty presentation!");
                return null;
            }
    
            singleSlide = allSlides [index];
            memoryImage = new BufferedImage(slideShowPPTX.getPageSize().width, slideShowPPTX.getPageSize().height, BufferedImage.TYPE_INT_ARGB);
            graphics = memoryImage.createGraphics();
            // Only few rendering hints set but you can set as many as you need depending on your need
            graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            graphics.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
            graphics.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
            graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
            singleSlide.draw(graphics);
            icon = new ImageIcon(memoryImage);
        }
        catch(IOException exception){
            System.err.println("Input/output Exception:");
            exception.printStackTrace();
        }
        finally{
            slideShowPPTX = null;
            allSlides = null;
            singleSlide = null;
            memoryImage = null;
            graphics = null;
            if(pptInputStream != null){
                try {
                    pptInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                pptInputStream = null;
            }
        }
        return icon;
    }
    
    0 讨论(0)
提交回复
热议问题