如何使用java代码将pptx转换为pdf文档

时间:2017-05-23 07:28:39

标签: java pdf itext apache-poi

我正在尝试使用link中的示例,但我会遇到类似的例外情况。

java.lang.IllegalArgumentException: Failed to fetch default style for otherStyle and level=0
    at org.apache.poi.xslf.usermodel.XSLFTextParagraph.getDefaultMasterStyle(XSLFTextParagraph.java:1039)
    at org.apache.poi.xslf.usermodel.XSLFTextRun.fetchCharacterProperty(XSLFTextRun.java:536)
    at org.apache.poi.xslf.usermodel.XSLFTextRun.getTextCap(XSLFTextRun.java:417)
    at org.apache.poi.xslf.usermodel.XSLFTextRun.getRenderableText(XSLFTextRun.java:67)
    at org.apache.poi.xslf.usermodel.XSLFTextParagraph.getRenderableText(XSLFTextParagraph.java:115)
    at org.apache.poi.xslf.usermodel.XSLFTextParagraph.breakText(XSLFTextParagraph.java:915)
    at org.apache.poi.xslf.usermodel.XSLFTextShape.breakText(XSLFTextShape.java:483)
    at org.apache.poi.xslf.usermodel.XSLFTextShape.drawContent(XSLFTextShape.java:491)
    at org.apache.poi.xslf.usermodel.RenderableShape.render(RenderableShape.java:540)
    at org.apache.poi.xslf.usermodel.XSLFSimpleShape.draw(XSLFSimpleShape.java:534)
    at org.apache.poi.xslf.usermodel.XSLFSheet.draw(XSLFSheet.java:506)
    at org.apache.poi.xslf.usermodel.XSLFSlide.draw(XSLFSlide.java:220)
    at test.PptxToPDFConversion.convertPPTToPDF(PptxToPDFConversion.java:87)
    at test.PptxToPDFConversion.main(PptxToPDFConversion.java:106)

1 个答案:

答案 0 :(得分:1)

是的,我使用了最新的罐子,它起作用了,

xmlbeans-2.6.0,poi-scratchpad-3.16,poi-ooxml-schemas-3.16,poi-ooxml-3.16,poi-excelant-3.16, poi-examples-3.16,poi-3.16,org.apache-servicemix-bundles-poi-3.16, itext-2.17,dom4j-1.6.1

public void convertPPTXtoImgtoPDF() throws IOException, DocumentException, InvalidFormatException {
    FileInputStream inputStream = new FileInputStream("vzw.pptx");

    XMLSlideShow ppt = new XMLSlideShow(OPCPackage.open(inputStream));

    inputStream.close();
    Dimension pgsize = ppt.getPageSize();
    float scale = 1;
    int width = (int) (pgsize.width * scale);
    int height = (int) (pgsize.height * scale);

    int i = 1;
    int totalSlides = ppt.getSlides().size();

           for (XSLFSlide slide : ppt.getSlides()) {

                BufferedImage img = new BufferedImage(pgsize.width, pgsize.height,
                        BufferedImage.TYPE_INT_RGB);
                Graphics2D graphics = img.createGraphics();
                graphics.setPaint(Color.white);
                graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width,
                        pgsize.height));
                graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
                graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
                graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
                graphics.setColor(Color.white);
                graphics.clearRect(0, 0, width, height);
                graphics.scale(scale, scale);

                slide.draw(graphics);
                FileOutputStream out = new FileOutputStream("images/"+i+".png");
                javax.imageio.ImageIO.write(img, "png", out);
                out.close();
                i++;
           }

     Document document = new Document();
     PdfWriter.getInstance(document, new FileOutputStream("filenew.pdf"));
     com.lowagie.text.pdf.PdfPTable table = new com.lowagie.text.pdf.PdfPTable(1);


    for(int j = 1; j<=totalSlides; j++){
        Image slideImage = Image.getInstance("images/"+j+".png");

        document.setPageSize(new Rectangle(slideImage.getWidth(), slideImage.getHeight()));
        document.open();
        slideImage.setAbsolutePosition(0, 0);

        table.addCell(new com.lowagie.text.pdf.PdfPCell(slideImage, true));

    }
     document.add(table);
     document.close();
}
}