0

I am using the below code to convert ppt slides to image.

BufferedImage imBuff = new BufferedImage(pgsize.width, (pgsize.height) * slides.size(), BufferedImage.TYPE_INT_RGB);
Graphics g = imBuff.getGraphics();
Graphics2D graphics = img.createGraphics();
graphics.setRenderingHint(Drawable.FONT_MAP, fontMap);
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
graphics.setPaint(TRANSPARENT);
graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
slides.get(i).draw(graphics);
g.drawImage(img, 0, i * (pgsize.height), null);

For some .ppt files, the background appears as black. Does anyone know what is the reason and how to fix this? I suspect that the office software used to create the .ppt files might be a factor, but I am not able to confirm.

nanoticket
  • 63
  • 6
  • 1
    My guess is, it's transparent ... but to verify it, can you provide the .ppt somehow? (hint: add an entry to our [bugzilla](https://bz.apache.org/bugzilla/enter_bug.cgi?product=POI) ...) – kiwiwings May 15 '17 at 16:39
  • I removed "graphics.setPaint(TRANSPARENT);" but the result is the same. – nanoticket May 16 '17 at 10:57

1 Answers1

0

Thank you for taking the time to upload your example to our bugzilla - see #61112

The transparent parts of the image can be replaced as described in Stripping Alpha Channel from Images, of course you need to use the ARGB color model first.

I've added the Arial Unicode font to the example, but you probably need a different font which covers all the used characters.

Complete example:

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;

import org.apache.poi.sl.usermodel.SlideShow;
import org.apache.poi.sl.usermodel.SlideShowFactory;
import org.junit.Test;

public class TestRendering {

    @Test
    public void bug61112() throws Exception {
        Font font = Font.createFont(Font.TRUETYPE_FONT, new File("arialuni.ttf"));
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        ge.registerFont(font);

        SlideShow<?,?> ppt = SlideShowFactory.create(new File("bug61112.ppt"), null, true);
        double scale = 1;
        Dimension pgsize = ppt.getPageSize();
        int width = (int) (pgsize.width * scale);
        int height = (int) (pgsize.height * scale);

        BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D graphics = img.createGraphics();
        DrawFactory.getInstance(graphics).fixFonts(graphics);

        // default rendering options
        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);

        Map<String,String> fallbackMap = new HashMap<String,String>();
        fallbackMap.put("*", font.getFamily());
        graphics.setRenderingHint(Drawable.FONT_FALLBACK, fallbackMap);

        ppt.getSlides().get(0).draw(graphics);

        // Replace the transparent parts of the image
        // Set composite rules to paint "behind"
        graphics.setComposite(AlphaComposite.DstOver);
        graphics.setPaint(Color.WHITE);
        graphics.fillRect(0, 0, width, height);

        ImageIO.write(img, "PNG", new File("bla.png"));

        graphics.dispose();
        img.flush();


    }
}
kiwiwings
  • 3,386
  • 1
  • 21
  • 57