gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 10:20:30 26 4
gpt4 key购买 nike

我是java初学者

我尝试使用 apache poiItext 库将 PPT 文件转换为 PDF 文件,但出现了 NoSuchMethodError 错误

我尝试了所有版本的 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)

最佳答案

参见 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 is a new command-line tool added since JDK 8 for developers to use to understand the static dependencies of their applications and libraries. jdeps is a static analysis tool on the given class files

关于java - 在java中将ppt文件转换为PDF时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48163194/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com