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);