aspose pdf等转成图片

    public static void wordToImage(String wordPath, String imagePath) throws Exception {
        Document doc = new Document(wordPath);
        File file = new File(wordPath);
        String filename = file.getName();
        String pathPre = imagePath + File.separator + filename.substring(0, filename.lastIndexOf("."));
        for (int i = 0; i < doc.getPageCount(); i++) {
            Document extractedPage = doc.extractPages(i, 1);
            String path = pathPre + (i + 1) + ".png";
            extractedPage.save(path, SaveFormat.PNG);
        }
    }

    public static void txtToImage(String txtPath, String imagePath) throws Exception {
        wordToImage(txtPath, imagePath);
    }


    public static void pptToImage(String pptPath, String imagePath) throws Exception {
        File file = new File(pptPath);
        String filename = file.getName();
        String pathPre = imagePath + File.separator + filename.substring(0, filename.lastIndexOf("."));
        Presentation presentation = new Presentation(pptPath);
        for (int i = 0; i < presentation.getSlides().size(); i++) {
            ISlide slide = presentation.getSlides().get_Item(i);
            BufferedImage image = slide.getThumbnail(1f, 1f);
            String path = pathPre + (i + 1) + ".png";
            ImageIO.write(image, "png", new File(path));
        }
    }


    public static void excelToPic(String excelpath, String picpath) {
        try {
            Workbook wb = new Workbook(excelpath);
            for (int i = 0; i < wb.getWorksheets().getCount(); i++) {
                Worksheet sheet = wb.getWorksheets().get(i);
                ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
                imgOptions.setImageType(ImageType.PNG);
                imgOptions.setCellAutoFit(true);
                imgOptions.setOnePagePerSheet(true);
                SheetRender render = new SheetRender(sheet, imgOptions);
                for (int page = 0; page < render.getPageCount(); page++) {
                    // Generate an image for the worksheet
                    render.toImage(page, picpath + File.separator+String.format("%04d", i)+String.format("%04d", page)+ ".png");
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void pdfToImage(String pdfPath, String imagePath) throws Exception {
        com.aspose.pdf.Document pdf = new com.aspose.pdf.Document(pdfPath);
        PageCollection pages = pdf.getPages();
        File file = new File(pdfPath);
        String filename = file.getName();
        int size = pages.size();
        PngDevice pngDevice = new PngDevice();
        String pathPre = imagePath + File.separator + filename.substring(0, filename.lastIndexOf("."));
        for (int index = 1; index < size+1; index++) {
            // 输出路径
            String path = pathPre + (index) + ".png";
            pngDevice.process(pages.get_Item(index), path);
        }

    }


}

调用方式:

InputStream fis = ResourceUtil.getStream("license.xml");
com.aspose.pdf.License license = new com.aspose.pdf.License();
license.setLicense(fis);
String wordPath = " ";
String imagePath = " ";
pdfToImage(wordPath, imagePath);
  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值