word,ppt,pdf 转图片

java 复制代码
@Override
//word
	public Result<List<String>> call(MultipartFile file, String fileName,String bizPath) {
				InputStream inputStream=null;
	 	        List<String> imgList= new ArrayList<>();
	        try {
	        	 inputStream= file.getInputStream();
		         Document doc= new Document(inputStream);
		      for (int i = 0; i < doc.getPageCount(); i++) {
	   		    	Document page = doc.extractPages(i, 1);
                    ByteArrayOutputStream dstStream = new ByteArrayOutputStream();
	   		    	page.save(dstStream, SaveFormat.PNG);
				    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(dstStream.toByteArray());
				    BufferedImage img = ImageIO.read(byteArrayInputStream);
				    MultipartFile files = ImageUtil.multipartFile(img, System.currentTimeMillis()+"", "png");
                    //上传minio
                    Result<?> upload = uploadUtil.upload(files, bizPath, false);
                    dstStream.flush();
                    dstStream.close();
                    imgList.add(upload.getMessage());
              }
	        } catch (Exception e) {
	            e.printStackTrace();
	        }finally {
	        	if(inputStream!=null) {
	        		  try {
						inputStream.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
	        	}
			}
	        return Result.OK(imgList);
	}

	@Override
	public Result<List<String>> callPpt(MultipartFile file, String fileName, String bizPath) {
		InputStream	inputStream= null;
		List<String> imgList= new ArrayList<>();
		try {
			inputStream= file.getInputStream();
			HSLFSlideShow ppt = new HSLFSlideShow(inputStream);
			Dimension pgsize = ppt.getPageSize();
			for (int idx = 0; idx < ppt.getSlides().size(); idx++) {
				HSLFSlide slide=ppt.getSlides().get(idx);
				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));
				slide.draw(graphics);
				ByteArrayOutputStream dstStream = new ByteArrayOutputStream();
				boolean png = ImageIO.write(img, "png", dstStream);
				if(png){
					MultipartFile files = ImageUtil.multipartFile(img, System.currentTimeMillis()+"", "png");
					Result<?> upload = uploadUtil.upload(files, bizPath, false);
					dstStream.flush();
					dstStream.close();
					imgList.add(upload.getMessage());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			if(inputStream!=null) {
				try {
					inputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return Result.OK(imgList);
	}

	@Override
	public Result<List<String>> callPptx(MultipartFile file, String fileName, String bizPath) {
		InputStream	inputStream= null;
		List<String> imgList= new ArrayList<>();
		try {
			inputStream= file.getInputStream();
			XMLSlideShow ppt = new XMLSlideShow(inputStream);
			Dimension pgsize = ppt.getPageSize();
			for (int idx = 0; idx < ppt.getSlides().size(); idx++) {
				XSLFSlide slide=ppt.getSlides().get(idx);
				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));
				slide.draw(graphics);
				ByteArrayOutputStream dstStream = new ByteArrayOutputStream();
				boolean png = ImageIO.write(img, "png", dstStream);
				if(png){
					MultipartFile files = ImageUtil.multipartFile(img, System.currentTimeMillis()+"", "png");
					Result<?> upload = uploadUtil.upload(files, bizPath, false);
					dstStream.flush();
					dstStream.close();
					imgList.add(upload.getMessage());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			if(inputStream!=null) {
				try {
					inputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return Result.OK(imgList);
	}


	@Override
	public Result<List<String>> callPdf(MultipartFile file, String fileName, String bizPath) {
		InputStream inputStream=null;
		PDDocument doc = null;
		List<String> imgList= new ArrayList<>();
		try {
			inputStream = file.getInputStream();
			doc = PDDocument.load(inputStream);
			PDFRenderer renderer = new PDFRenderer(doc);
			for (int i = 0; i <doc.getNumberOfPages(); i++) {
				BufferedImage img = renderer.renderImageWithDPI(i, 100);
				PDFTextStripper pdfStripper = new PDFTextStripper();
				pdfStripper.setStartPage(i);
				pdfStripper.setEndPage(i);
				ByteArrayOutputStream dstStream = new ByteArrayOutputStream();
				boolean png = ImageIO.write(img, "png", dstStream);
				if(png){
					MultipartFile files = ImageUtil.multipartFile(img, System.currentTimeMillis()+"", "png");
					Result<?> upload = uploadUtil.upload(files, bizPath, false);
					dstStream.flush();
					dstStream.close();
					imgList.add(upload.getMessage());
				}
			}
		}catch (Exception e) {
			e.printStackTrace();
		}finally {
			if(doc!=null) {
				try {
					doc.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		return Result.OK(imgList);
	}

maven

java 复制代码
!--word转pdf -->
        <dependency>
		   <groupId>com.aspose</groupId>
		   <artifactId>aspose-words</artifactId>
		   <version>15.12.0</version>
		   <scope>system</scope>
		   <systemPath>${project.basedir}/src/main/resources/lib/aspose-words-15.12.0-jdk16.jar</systemPath>
		</dependency>
        <!--ppt转pdf -->
        <dependency>
		   <groupId>com.aspose</groupId>
		   <artifactId>aspose-slides</artifactId>
		   <version>15.9.0</version>
		   <scope>system</scope>
		   <systemPath>${project.basedir}/src/main/resources/lib/aspose.slides-15.9.0.jar</systemPath>
		</dependency>
		<dependency>
			<groupId>com.aspose</groupId>
			<artifactId>aspose-words</artifactId>
			<version>21.1.0</version>
		</dependency>

		<!-- Apache PDFBox -->
		<dependency>
			<groupId>org.apache.pdfbox</groupId>
			<artifactId>pdfbox</artifactId>
			<version>2.0.27</version> <!-- 确保使用最新版本 -->
		</dependency>

		<!-- 如果需要额外的功能,如字体处理,可以添加以下依赖 -->
		<dependency>
			<groupId>org.apache.pdfbox</groupId>
			<artifactId>fontbox</artifactId>
			<version>2.0.27</version> <!-- 确保版本与pdfbox一致 -->
		</dependency>


        <!--excel转pdf -->
        <dependency>
		   <groupId>com.aspose</groupId>
		   <artifactId>aspose-cells</artifactId>
		   <version>8.5.2</version>
		   <scope>system</scope>
		   <systemPath>${project.basedir}/src/main/resources/lib/aspose-cells-8.5.2.jar</systemPath>
		</dependency>
		<!-- https://mvnrepository.com/artifact/com.lowagie/itext -->
		<dependency>
			<groupId>com.lowagie</groupId>
			<artifactId>itext</artifactId>
			<version>2.1.7</version>
		</dependency>
		<dependency>
			<groupId>commons-fileupload</groupId>
			<artifactId>commons-fileupload</artifactId>
			<version>1.3.3</version>
		</dependency>

ImageUtil

java 复制代码
 public static MultipartFile multipartFile(Image image,String fileName,String suffix){
        //创建一个ByteArrayOutputStream
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        //把BufferedImage写入ByteArrayOutputStream
        ImgUtil.write(image, suffix, os);
        //ByteArrayOutputStream转成InputStream
        InputStream input = new ByteArrayInputStream(os.toByteArray());
        //InputStream转成MultipartFile
        try {
            return new MockMultipartFile(fileName, fileName+"."+suffix, ContentType.contentType(suffix), input);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

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

相关推荐
zhonyu鱼18 小时前
Saber:专为触控笔设计的开源手写笔记与 PDF 批注工具
笔记·pdf·开源
AI导出鸭PC端18 小时前
文心怎样生成word文档?一键智能排版,AI导出鸭解决格式错乱痛点
人工智能·ai·word·豆包·deepseek·ai导出鸭
winfredzhang20 小时前
用 Python + wxPython 造一个照片工具箱:PDF / 加密ZIP / MP4 / 归档,以及我在这过程中踩到的 4 个坑
python·pdf·zip·mp4·移动
AcaDesign1 天前
国家/省部市级科技奖答辩PPT_科技进步奖_自然科学奖_技术发明奖|WordinPPT
大数据·人工智能·科技·powerpoint
2501_930707781 天前
使用C#代码在 PDF 文档中创建列表
pdf
AI导出鸭2 天前
Claude的LaTeX生成PDF文件复制后数学公式乱码,怎样修改?专业用户首选“AI导出鸭”
人工智能·pdf·ai导出鸭
●VON2 天前
芯稿 MarkDeck 使用指南:用 Markdown 写出可编辑的 PPT
华为·编辑器·powerpoint·harmonyos·鸿蒙
AI导出鸭2 天前
怎么让文心做表格?AI导出鸭苹果版将文心输出的管道表格智能解析为二维结构,一键导出为Excel或Word标准表格
人工智能·chatgpt·word·excel·ai导出鸭
wujian83112 天前
AI手机版怎么直接生成word?用“AI 导出鸭”把碎片时间变成专业文档
人工智能·ai·chatgpt·智能手机·word·ai导出鸭
鲲穹AI种草3 天前
多文档批量整理如何选?多款 Word 处理工具能力客观记录
word·文档处理