Tamil Tamil - 12 days ago 5
Java Question

Apache POI pptx to image take too much time for processing

I have code to convert pptx slide into image and display the image in swing panel.

When i run my code in eclipse, it takes 10 Sec to display the panel whereas same code run via jar, it takes more than one minute to open the panel.

It happens only when user open it first time, Later it loads faster.

Any help would be appreciated and thanks in advance.

Here i included the code

// currentPage - Slide number to display
// source - pptx file path
public void Display(int currentPage, String source) {
try {

FileInputStream is = new FileInputStream(source);
XMLSlideShow ppt = new XMLSlideShow(is);
is.close();

double zoom = 1; // magnify it by 2
AffineTransform at = new AffineTransform();
at.setToScale(zoom, zoom);

Dimension pgsize = ppt.getPageSize();

XSLFSlide[] slides = ppt.getSlides();
all = slides.length;
lblPage.setText(currentPage + " / " + all);

current = currentPage;

BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB);

Graphics2D graphics = img.createGraphics();
graphics.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
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.transform(at);

graphics.setColor(Color.white);
graphics.clearRect(0, 0, pgsize.width, pgsize.height);
graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));

System.out.println("Before draw: " + new Date());

slides[currentPage - 1].draw(graphics);

System.out.println("After draw: " + new Date());

// save the output
Image newImg = img.getScaledInstance(lblPresentasi.getWidth(), lblPresentasi.getHeight(), Image.SCALE_SMOOTH);
final ImageIcon icon = new ImageIcon(newImg);
lblPresentasi.setIcon(icon);
lblPresentasi.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
JLabel label = (JLabel) e.getComponent();
Dimension size = label.getSize();
Image resized = icon.getImage().getScaledInstance(size.width-10, size.height-10, Image.SCALE_FAST);
label.setIcon(new ImageIcon(resized));
}
});
graphics.dispose();
newImg.flush();
} catch (Exception e) {
e.printStackTrace();
}
}// end of method Display()

Answer

This delay caused by One-Jar custom class loader. At first time, it takes more time to load the dependency libraries and load it classes.

So, I removed one-jar and using this link to load the dependency libraries classes, it works great.