使用iText和PdfGraphics2D的PPTX到PDF错误

时间:2018-11-21 15:40:37

标签: java apache itext apache-poi apache-tika

我试图将pptx文件转换为图像,然后使用apache poi和iText从其中创建PDF。

Dimension pgsize = ppt.getPageSize();
XSLFSlide slide = ppt.getSlides()[0];
PdfWriter pdfWriter = null;
Document document = new Document()

pdfWriter = PdfWriter.getInstance(document, new FileOutputStream(targetPath));
document.setPageSize(new Rectangle((float) pgsize.getWidth(), (float) pgsize.getHeight()));
document.open();

PdfGraphics2D graphics = (PdfGraphics2D) pdfWriter.getDirectContent().createGraphics((float) pgsize.getWidth(),
                            (float) pgsize.getHeight());

slide.draw(graphics);
graphics.dispose();
document.close();

但是我得到了错误:

java.lang.AbstractMethodError: org.apache.poi.xslf.usermodel.XSLFTextRun.getFontInfo(Lorg/apache/poi/common/usermodel/fonts/FontGroup;)Lorg/apache/poi/common/usermodel/fonts/FontInfo;
    at org.apache.poi.sl.draw.DrawTextParagraph.getAttributedString(DrawTextParagraph.java:584) ~[poi-3.17.jar:3.17]
    at org.apache.poi.sl.draw.DrawTextParagraph.breakText(DrawTextParagraph.java:259) ~[poi-3.17.jar:3.17]
    at org.apache.poi.sl.draw.DrawTextShape.drawParagraphs(DrawTextShape.java:156) ~[poi-3.17.jar:3.17]
    at org.apache.poi.sl.draw.DrawTextShape.drawContent(DrawTextShape.java:125) ~[poi-3.17.jar:3.17]
    at org.apache.poi.sl.draw.DrawSimpleShape.draw(DrawSimpleShape.java:97) ~[poi-3.17.jar:3.17]
    at org.apache.poi.sl.draw.DrawSheet.draw(DrawSheet.java:71) ~[poi-3.17.jar:3.17]
    at org.apache.poi.sl.draw.DrawSlide.draw(DrawSlide.java:41) ~[poi-3.17.jar:3.17]
    at org.apache.poi.xslf.usermodel.XSLFSlide.draw(XSLFSlide.java:307) ~[tika-server-1.16.jar:1.16]

1 个答案:

答案 0 :(得分:1)

解决方案是不混合POI版本。 Apache Tika已包含POI