word,ppt,pdf 转图片

打印 上一主题 下一主题

主题 1613|帖子 1613|积分 4839

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. @Override
  2. //word
  3.         public Result<List<String>> call(MultipartFile file, String fileName,String bizPath) {
  4.                                 InputStream inputStream=null;
  5.                          List<String> imgList= new ArrayList<>();
  6.                 try {
  7.                          inputStream= file.getInputStream();
  8.                          Document doc= new Document(inputStream);
  9.                       for (int i = 0; i < doc.getPageCount(); i++) {
  10.                                        Document page = doc.extractPages(i, 1);
  11.                     ByteArrayOutputStream dstStream = new ByteArrayOutputStream();
  12.                                        page.save(dstStream, SaveFormat.PNG);
  13.                                     ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(dstStream.toByteArray());
  14.                                     BufferedImage img = ImageIO.read(byteArrayInputStream);
  15.                                     MultipartFile files = ImageUtil.multipartFile(img, System.currentTimeMillis()+"", "png");
  16.                     //上传minio
  17.                     Result<?> upload = uploadUtil.upload(files, bizPath, false);
  18.                     dstStream.flush();
  19.                     dstStream.close();
  20.                     imgList.add(upload.getMessage());
  21.               }
  22.                 } catch (Exception e) {
  23.                     e.printStackTrace();
  24.                 }finally {
  25.                         if(inputStream!=null) {
  26.                                   try {
  27.                                                 inputStream.close();
  28.                                         } catch (IOException e) {
  29.                                                 e.printStackTrace();
  30.                                         }
  31.                         }
  32.                         }
  33.                 return Result.OK(imgList);
  34.         }
  35.         @Override
  36.         public Result<List<String>> callPpt(MultipartFile file, String fileName, String bizPath) {
  37.                 InputStream        inputStream= null;
  38.                 List<String> imgList= new ArrayList<>();
  39.                 try {
  40.                         inputStream= file.getInputStream();
  41.                         HSLFSlideShow ppt = new HSLFSlideShow(inputStream);
  42.                         Dimension pgsize = ppt.getPageSize();
  43.                         for (int idx = 0; idx < ppt.getSlides().size(); idx++) {
  44.                                 HSLFSlide slide=ppt.getSlides().get(idx);
  45.                                 BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB);
  46.                                 Graphics2D graphics = img.createGraphics();
  47.                                 graphics.setPaint(Color.white);
  48.                                 graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
  49.                                 slide.draw(graphics);
  50.                                 ByteArrayOutputStream dstStream = new ByteArrayOutputStream();
  51.                                 boolean png = ImageIO.write(img, "png", dstStream);
  52.                                 if(png){
  53.                                         MultipartFile files = ImageUtil.multipartFile(img, System.currentTimeMillis()+"", "png");
  54.                                         Result<?> upload = uploadUtil.upload(files, bizPath, false);
  55.                                         dstStream.flush();
  56.                                         dstStream.close();
  57.                                         imgList.add(upload.getMessage());
  58.                                 }
  59.                         }
  60.                 } catch (Exception e) {
  61.                         e.printStackTrace();
  62.                 }finally {
  63.                         if(inputStream!=null) {
  64.                                 try {
  65.                                         inputStream.close();
  66.                                 } catch (IOException e) {
  67.                                         e.printStackTrace();
  68.                                 }
  69.                         }
  70.                 }
  71.                 return Result.OK(imgList);
  72.         }
  73.         @Override
  74.         public Result<List<String>> callPptx(MultipartFile file, String fileName, String bizPath) {
  75.                 InputStream        inputStream= null;
  76.                 List<String> imgList= new ArrayList<>();
  77.                 try {
  78.                         inputStream= file.getInputStream();
  79.                         XMLSlideShow ppt = new XMLSlideShow(inputStream);
  80.                         Dimension pgsize = ppt.getPageSize();
  81.                         for (int idx = 0; idx < ppt.getSlides().size(); idx++) {
  82.                                 XSLFSlide slide=ppt.getSlides().get(idx);
  83.                                 BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB);
  84.                                 Graphics2D graphics = img.createGraphics();
  85.                                 graphics.setPaint(Color.white);
  86.                                 graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
  87.                                 slide.draw(graphics);
  88.                                 ByteArrayOutputStream dstStream = new ByteArrayOutputStream();
  89.                                 boolean png = ImageIO.write(img, "png", dstStream);
  90.                                 if(png){
  91.                                         MultipartFile files = ImageUtil.multipartFile(img, System.currentTimeMillis()+"", "png");
  92.                                         Result<?> upload = uploadUtil.upload(files, bizPath, false);
  93.                                         dstStream.flush();
  94.                                         dstStream.close();
  95.                                         imgList.add(upload.getMessage());
  96.                                 }
  97.                         }
  98.                 } catch (Exception e) {
  99.                         e.printStackTrace();
  100.                 }finally {
  101.                         if(inputStream!=null) {
  102.                                 try {
  103.                                         inputStream.close();
  104.                                 } catch (IOException e) {
  105.                                         e.printStackTrace();
  106.                                 }
  107.                         }
  108.                 }
  109.                 return Result.OK(imgList);
  110.         }
  111.         @Override
  112.         public Result<List<String>> callPdf(MultipartFile file, String fileName, String bizPath) {
  113.                 InputStream inputStream=null;
  114.                 PDDocument doc = null;
  115.                 List<String> imgList= new ArrayList<>();
  116.                 try {
  117.                         inputStream = file.getInputStream();
  118.                         doc = PDDocument.load(inputStream);
  119.                         PDFRenderer renderer = new PDFRenderer(doc);
  120.                         for (int i = 0; i <doc.getNumberOfPages(); i++) {
  121.                                 BufferedImage img = renderer.renderImageWithDPI(i, 100);
  122.                                 PDFTextStripper pdfStripper = new PDFTextStripper();
  123.                                 pdfStripper.setStartPage(i);
  124.                                 pdfStripper.setEndPage(i);
  125.                                 ByteArrayOutputStream dstStream = new ByteArrayOutputStream();
  126.                                 boolean png = ImageIO.write(img, "png", dstStream);
  127.                                 if(png){
  128.                                         MultipartFile files = ImageUtil.multipartFile(img, System.currentTimeMillis()+"", "png");
  129.                                         Result<?> upload = uploadUtil.upload(files, bizPath, false);
  130.                                         dstStream.flush();
  131.                                         dstStream.close();
  132.                                         imgList.add(upload.getMessage());
  133.                                 }
  134.                         }
  135.                 }catch (Exception e) {
  136.                         e.printStackTrace();
  137.                 }finally {
  138.                         if(doc!=null) {
  139.                                 try {
  140.                                         doc.close();
  141.                                 } catch (IOException e) {
  142.                                         e.printStackTrace();
  143.                                 }
  144.                         }
  145.                 }
  146.                 return Result.OK(imgList);
  147.         }
复制代码
maven
  1. !--word转pdf -->
  2.         <dependency>
  3.                    <groupId>com.aspose</groupId>
  4.                    <artifactId>aspose-words</artifactId>
  5.                    <version>15.12.0</version>
  6.                    <scope>system</scope>
  7.                    <systemPath>${project.basedir}/src/main/resources/lib/aspose-words-15.12.0-jdk16.jar</systemPath>
  8.                 </dependency>
  9.         <!--ppt转pdf -->
  10.         <dependency>
  11.                    <groupId>com.aspose</groupId>
  12.                    <artifactId>aspose-slides</artifactId>
  13.                    <version>15.9.0</version>
  14.                    <scope>system</scope>
  15.                    <systemPath>${project.basedir}/src/main/resources/lib/aspose.slides-15.9.0.jar</systemPath>
  16.                 </dependency>
  17.                 <dependency>
  18.                         <groupId>com.aspose</groupId>
  19.                         <artifactId>aspose-words</artifactId>
  20.                         <version>21.1.0</version>
  21.                 </dependency>
  22.                 <!-- Apache PDFBox -->
  23.                 <dependency>
  24.                         <groupId>org.apache.pdfbox</groupId>
  25.                         <artifactId>pdfbox</artifactId>
  26.                         <version>2.0.27</version> <!-- 确保使用最新版本 -->
  27.                 </dependency>
  28.                 <!-- 如果需要额外的功能,如字体处理,可以添加以下依赖 -->
  29.                 <dependency>
  30.                         <groupId>org.apache.pdfbox</groupId>
  31.                         <artifactId>fontbox</artifactId>
  32.                         <version>2.0.27</version> <!-- 确保版本与pdfbox一致 -->
  33.                 </dependency>
  34.         <!--excel转pdf -->
  35.         <dependency>
  36.                    <groupId>com.aspose</groupId>
  37.                    <artifactId>aspose-cells</artifactId>
  38.                    <version>8.5.2</version>
  39.                    <scope>system</scope>
  40.                    <systemPath>${project.basedir}/src/main/resources/lib/aspose-cells-8.5.2.jar</systemPath>
  41.                 </dependency>
  42.                 <!-- https://mvnrepository.com/artifact/com.lowagie/itext -->
  43.                 <dependency>
  44.                         <groupId>com.lowagie</groupId>
  45.                         <artifactId>itext</artifactId>
  46.                         <version>2.1.7</version>
  47.                 </dependency>
  48.                 <dependency>
  49.                         <groupId>commons-fileupload</groupId>
  50.                         <artifactId>commons-fileupload</artifactId>
  51.                         <version>1.3.3</version>
  52.                 </dependency>
复制代码
ImageUtil
  1. public static MultipartFile multipartFile(Image image,String fileName,String suffix){
  2.         //创建一个ByteArrayOutputStream
  3.         ByteArrayOutputStream os = new ByteArrayOutputStream();
  4.         //把BufferedImage写入ByteArrayOutputStream
  5.         ImgUtil.write(image, suffix, os);
  6.         //ByteArrayOutputStream转成InputStream
  7.         InputStream input = new ByteArrayInputStream(os.toByteArray());
  8.         //InputStream转成MultipartFile
  9.         try {
  10.             return new MockMultipartFile(fileName, fileName+"."+suffix, ContentType.contentType(suffix), input);
  11.         } catch (IOException e) {
  12.             e.printStackTrace();
  13.         }
  14.         return null;
  15.     }
复制代码

工具类和jar包找不到的私信

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

羊蹓狼

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表