Wednesday, 23 November 2016

How to convert pptx file to Image and pdf in Java ?

       
//Here I Use APACHE POI and ITEXT JAVA API
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import org.apache.poi.xslf.usermodel.*;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;

public class MakePPTtoPDF {
 public static void main(String args[]) throws IOException{
       
       //creating an empty presentation
       File file=new File("D:/Exam2016Kiit/HTTP Over WebSocket Proxy System.pptx");
       XMLSlideShow ppt = new XMLSlideShow(new FileInputStream(file));
       
       
       //getting the dimensions and size of the slide
       
       Dimension pgsize = ppt.getPageSize();
       List<XSLFSlide> slide = ppt.getSlides();
       BufferedImage img=null;
       Document doc=new Document();
         try {
    PdfWriter.getInstance(doc, new  FileOutputStream("C:/Users/kiit1/Desktop/aca/PPTImages.pdf"));
   
         doc.open();

       for (int i = 0; i < slide.size(); i++) {
          img = new BufferedImage(pgsize.width, pgsize.height,BufferedImage.TYPE_INT_RGB);
          Graphics2D graphics = img.createGraphics();

          //clear the drawing area
          graphics.setPaint(Color.white);
          graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));

          //render
          slide.get(i).draw(graphics);
          FileOutputStream out = new FileOutputStream("C:/Users/kiit1/Desktop/aca/"+i+".png");
        javax.imageio.ImageIO.write(img, "png", out);
        ppt.write(out);
        com.itextpdf.text.Image image =com.itextpdf.text.Image.getInstance("C:/Users/kiit1/Desktop/aca/"+i+".png");
             doc.setPageSize(new com.itextpdf.text.Rectangle(image.getScaledWidth(), image.getScaledHeight()));
             doc.newPage();
             image.setAbsolutePosition(0, 0);
             doc.add(image);

        System.out.println("Image successfully created");
        out.close();
       }
       doc.close();
         } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
       
        
    }

 
}

 


OUTPUT

AND MY OUT PUT FILE IS HERE

No comments:

Post a Comment