在java中将ppt文件转换为PDF时出错

时间:2018-01-09 07:26:18

标签: java pdf apache-poi powerpoint

我只是java中的初学者

我尝试使用 apache poi Itext 库将PPT文件转换为PDF文件,但我收到 NoSuchMethodError <的错误/ p>

我尝试了所有版本的Apache poi和poi-ooxml,但我得到了相同的错误 请帮我找出此错误的解决方法

这是我的代码:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.usermodel.SlideShow;
import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
public class PPTtoPDF {

    public PPTtoPDF() {

    }
    @SuppressWarnings("resource")
    public void convertPPTToPDF(String sourcepath, String destinationPath) 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;

            SlideShow ppt = new SlideShow(inputStream);
            pgsize = ppt.getPageSize();
            Slide slide[] = ppt.getSlides();
            System.out.println("Length----> "+slide.length);
            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));
                System.out.println(table);
            }

        pdfDocument.add(table);
        pdfDocument.close();
        pdfWriter.close();
        System.out.println("Powerpoint file converted to PDF successfully");
    }


    public static void main(String[] args) throws Exception
    {
        PPTtoPDF pp = new PPTtoPDF();
        pp.convertPPTToPDF("D:\\tp\\slides.ppt", "D:\\tp\\1.pdf");
    }
}

,错误是

Exception in thread "main" java.lang.NoSuchMethodError: org.apache.poi.POIDocument.<init>(Lorg/apache/poi/poifs/filesystem/DirectoryNode;Lorg/apache/poi/poifs/filesystem/POIFSFileSystem;)V
    at org.apache.poi.hslf.HSLFSlideShow.<init>(HSLFSlideShow.java:134)
    at org.apache.poi.hslf.HSLFSlideShow.<init>(HSLFSlideShow.java:120)
    at org.apache.poi.hslf.HSLFSlideShow.<init>(HSLFSlideShow.java:107)
    at org.apache.poi.hslf.usermodel.SlideShow.<init>(SlideShow.java:122)
    at convertService.PPTtoPDF.convertPPTToPDF(PPTtoPDF.java:44)
    at convertService.PPTtoPDF.main(PPTtoPDF.java:75)

1 个答案:

答案 0 :(得分:1)

参见Apache POI FAQ entry on this very topic.几乎可以肯定的是,你已经在你的类路径中添加了一个新的POI副本,但旧的版本已经存在(来自之前的需求,你的框架等),Java是现在对使用哪一个感到困惑。

您可以使用工具:

  • mvn依赖
  • 树JDK 8:jdeps

https://wiki.openjdk.java.net/display/JDK8/Java+Dependency+Analysis+Tool

  

jdeps是自JDK 8以来为开发人员添加的新命令行工具   用于理解其应用程序的静态依赖性   库。 jdeps是给定类文件的静态分析工具