如何转换PPT文件转换成PDF文件中的Java? [英] How to Convert a ppt file into pdf file in Java?

查看:201
本文介绍了如何转换PPT文件转换成PDF文件中的Java?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要一个懂MS PowerPoint(PPT)文件转换为PDF。我搜索了一些罐子像 officetools.jar ,但这需要购买。有没有什么办法。我们可以把它转换到的iText 阿帕奇 POI像我们的文档为PDF吗?请告诉我们,如果有人知道。

I want to convert an MS powerpoint (ppt) document to pdf. I searched some jars like officetools.jar, but that requires purchase. Is there any way. can we convert it through itext and apache POI like we do for doc to pdf? Please tell if anyone knows.

推荐答案

我使用的iText和Apache POI:

I am using iText and apache poi:

    public void convertPPTToPDF(String sourcepath, String destinationPath, String fileType) throws Exception {
    FileInputStream inputStream = new FileInputStream(sourcepath);
    double zoom = 2;
    AffineTransform at = new AffineTransform();
    at.setToScale(zoom, zoom);
    Document pdfDocument = new Document();
    PdfWriter pdfWriter = PdfWriter.getInstance(pdfDocument, new FileOutputStream(destinationPath));
    PdfPTable table = new PdfPTable(1);
    pdfWriter.open();
    pdfDocument.open();
    Dimension pgsize = null;
    Image slideImage = null;
    BufferedImage img = null;
    if (fileType.equalsIgnoreCase(".ppt")) {
        SlideShow ppt = new SlideShow(inputStream);
        pgsize = ppt.getPageSize();
        Slide slide[] = ppt.getSlides();
        pdfDocument.setPageSize(new Rectangle((float) pgsize.getWidth(), (float) pgsize.getHeight()));
        pdfWriter.open();
        pdfDocument.open();
        for (int i = 0; i < slide.length; i++) {
            img = new BufferedImage((int) Math.ceil(pgsize.width * zoom), (int) Math.ceil(pgsize.height * zoom), BufferedImage.TYPE_INT_RGB);
            Graphics2D graphics = img.createGraphics();
            graphics.setTransform(at);

            graphics.setPaint(Color.white);
            graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
            slide[i].draw(graphics);
            graphics.getPaint();
            slideImage = Image.getInstance(img, null);
            table.addCell(new PdfPCell(slideImage, true));
        }
    }
    if (fileType.equalsIgnoreCase(".pptx")) {
        XMLSlideShow ppt = new XMLSlideShow(inputStream);
        pgsize = ppt.getPageSize();
        XSLFSlide slide[] = ppt.getSlides();
        pdfDocument.setPageSize(new Rectangle((float) pgsize.getWidth(), (float) pgsize.getHeight()));
        pdfWriter.open();
        pdfDocument.open();
        for (int i = 0; i < slide.length; i++) {
            img = new BufferedImage((int) Math.ceil(pgsize.width * zoom), (int) Math.ceil(pgsize.height * zoom), BufferedImage.TYPE_INT_RGB);
            Graphics2D graphics = img.createGraphics();
            graphics.setTransform(at);

            graphics.setPaint(Color.white);
            graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
            slide[i].draw(graphics);
            graphics.getPaint();
            slideImage = Image.getInstance(img, null);
            table.addCell(new PdfPCell(slideImage, true));
        }
    }
    pdfDocument.add(table);
    pdfDocument.close();
    pdfWriter.close();
    System.out.println("Powerpoint file converted to PDF successfully");
}

这篇关于如何转换PPT文件转换成PDF文件中的Java?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆