aspose转换xlsx and doc and docx to pdf去水印无页数限制

aspose转换xlsx and doc and docx and ppt to pdf去水印无页数限制

所需jar包在百度网盘以及蓝奏云 最下边

java转换代码

//pom文件引入
<dependency>
	<groupId>com.aspose</groupId>
	<artifactId>aspose-cells</artifactId>
	<version>21.11</version>
	<scope>system</scope>
	<systemPath>${project.basedir}/src/main/resources/lib/aspose-cells.jar</systemPath>
</dependency>

<dependency>
	<groupId>com.aspose</groupId>
	<artifactId>aspose-words</artifactId>
	<version>21.1.0</version>
	<scope>system</scope>
	<systemPath>${project.basedir}/src/main/resources/lib/aspose-words.jar</systemPath><!-- jar包路径 -->
</dependency>
<dependency>
	<groupId>com.aspose</groupId>
	<artifactId>aspose-slides</artifactId>
	<version>21.10</version>
	<scope>system</scope>
	<systemPath>${project.basedir}/src/main/resources/lib/aspose-slides.jar</systemPath>
</dependency>

导入包
import com.aspose.cells.License;
import com.aspose.cells.PdfSaveOptions;
import com.aspose.cells.Workbook;
import com.aspose.slides.*;
import com.aspose.words.Document;
import com.aspose.words.SaveFormat;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.Date;

Java代码

/*
*去水印
*/
public static boolean getLicense() {
	boolean result = false;
	try {
			//ExcelToPdf是工具类名
		InputStream is = ExcelToPdf.class.getClassLoader().getResourceAsStream("license.xml"); //  license.xml应放在..\WebRoot\WEB-INF\classes路径下

		License aposeLic = new License();
		aposeLic.setLicense(is);
		result = true;
	} catch (Exception e) {
		e.printStackTrace();
	}
	return result;
}
/**
 *  xlsx to pdf
 */
public static boolean xlsxToPdf(String excelPath, String pdfPath) {
	try {
		getLicense();
		long old = System.currentTimeMillis();
		Workbook wb = new Workbook(excelPath);
		FileOutputStream fileOS = new FileOutputStream(new File(pdfPath));
		wb.save(fileOS, SaveFormat.PDF);
		fileOS.close();
		long now = System.currentTimeMillis();
		System.out.println("Conversion time: " + ((now - old) / 1000.0) + " seconds");
		return true;
	} catch (Exception e) {
		String errorMessage =  e.getMessage();
		throw new RuntimeException(errorMessage);
	}

}

/**
 * xlsx to pdf
 */
public static String xlsxToPdf(MultipartFile file, String pdfPath) {
	getLicense();
	if (file == null || file.isEmpty()) {
		throw new RuntimeException("Excel文件不能为空");
	}
	try {
		// 创建工作簿以加载Excel文件
		Workbook workbook = new Workbook(file.getInputStream());

		// 创建PDF选项
		PdfSaveOptions options = new PdfSaveOptions();
		options.setOnePagePerSheet(true);


		// 将文档保存为PDF格式
		workbook.save(pdfPath, options);
	} catch (Exception e) {
		e.printStackTrace();
	}

	return pdfPath;
}


/**
 * @param wordPath 需要被转换的word全路径带文件名
 * @param pdfPath 转换之后pdf的全路径带文件名
 */
public static boolean docTopdf(String wordPath, String pdfPath) {
	try {
		getLicense();

			long old = System.currentTimeMillis();
			File file = new File(pdfPath); //新建一个pdf文档
			FileOutputStream os = new FileOutputStream(file);
			Document doc = new Document(wordPath); //Address是将要被转化的word文档
			doc.save(os, SaveFormat.PDF);//全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
			long now = System.currentTimeMillis();
			os.close();
			System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒"); //转化用时
			return true;


	} catch (Exception e) {
		String errorMessage =  e.getMessage();
		throw new RuntimeException(errorMessage);
	}

}

/**
 * doc and docx to pdf
 */
public static String docxToPdf(MultipartFile file, String pdfDir) {
	getLicense();
	if (file == null || file.isEmpty()) {
		throw new RuntimeException("Word文档不能为空");
	}
	if (StringUtils.isEmpty(pdfDir)) {
		throw new RuntimeException("PDF目录不能为空");
	}

	String pdfPath = pdfDir;
	try {
		// 加载Word文档
		Document doc = new Document(file.getInputStream());

		// 将文档保存为PDF格式
		doc.save(pdfPath, SaveFormat.PDF);
	} catch (Exception e) {
		e.printStackTrace();
	}

	return pdfPath;
}


public static boolean getLicensePPT() {
	boolean result = false;
	InputStream is = null;
	try {
		ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
		org.springframework.core.io.Resource[] resources = resolver.getResources("classpath:license.xml");
		is = resources[0].getInputStream();
		com.aspose.slides.License aposeLic = new com.aspose.slides.License();
		aposeLic.setLicense(is);
		result = true;
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		if (is != null) {
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	return result;
}

/**
 *ppt路径 to pdf路径
 */
public static boolean ppt2Pdf(String inPath,String outPath){
	try {
		// 验证License 去除水印
		if (!getLicensePPT()) {
			return false;
		}
		long start = new Date().getTime();

		FileInputStream fileInput = new FileInputStream(inPath);
		Presentation pres = new Presentation(fileInput);
		FileOutputStream out = new FileOutputStream(new File(outPath));
		pres.save(out, com.aspose.slides.SaveFormat.Pdf);
		out.close();
		long end = new Date().getTime();
		System.out.println("pdf转换成功,共耗时:" + ((end - start) / 1000.0) + "秒"); // 转化用时
		return true;
	} catch (Exception e) {
		String errorMessage =  e.getMessage();
		throw new RuntimeException(errorMessage);
	}
}
/**
 *ppt路径 to pdf路径
 */
public static boolean ppt2Pdf(MultipartFile inFile, String outPath) {
	try {
		// 验证License 去除水印
		if (!getLicensePPT()) {
			return false;
		}
		long start = new Date().getTime();

		InputStream fileInput = inFile.getInputStream();
		Presentation pres = new Presentation(fileInput);
		FileOutputStream out = new FileOutputStream(new File(outPath));
		pres.save(out, com.aspose.slides.SaveFormat.Pdf);
		out.close();
		long end = new Date().getTime();
		System.out.println("pdf转换成功,共耗时:" + ((end - start) / 1000.0) + "秒"); // 转化用时
		return true;
	} catch (Exception e) {
		String errorMessage = e.getMessage();
		throw new RuntimeException(errorMessage);
	}
}

相关jar包

有什么不了解的可以私信或者评论
**

如果时间过长比较着急可以添加vx获取

**
在这里插入图片描述

评论 25
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李某海

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值