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包找不到的私信

相关推荐
工业3D_大熊9 天前
3D模式格式转换工具HOOPS Exchange如何将3D PDF转换为STEP格式?
3d·pdf·3d格式转换·3d模型格式转换·cad格式转换·cad数据格式转换·3d模型可视化
_oP_i9 天前
实现 “WebView2 获取word选中内容
开发语言·c#·word
IDRSolutions_CN10 天前
在 Java 中生成 PDF 缩略图(教程)
java·经验分享·pdf·软件工程·团队开发
IDRSolutions_CN10 天前
用Java将PDF转换成GIF
java·经验分享·pdf·软件工程·团队开发
LENG_Lingliang10 天前
word出现由WPS切换后公式异常无法删除的情况处理
word·wps·mathtype
贤和兄10 天前
使用docx4j 实现word转pdf(linux乱码处理)
linux·pdf·word
Eiceblue10 天前
高效打印 PDF 文档:基础操作与自动打印(含C# .NET方案)
pdf·c#·.net
沉到海底去吧Go10 天前
【工具教程】PDF指定区域OCR识别重命名工具使用教程和注意事项
pdf·ocr·图片区域识别改名·仓储物流单据识别·物流单据识别改名·pdf区域识别改名·pdf区域识别重命名
开开心心就好11 天前
高效批量转换Word到PDF的方法
javascript·安全·智能手机·pdf·word·objective-c·lisp
response_L11 天前
麒麟v10、uos系统在线批量生成pdf文件
java·pdf·word·pageoffice·在线编辑