word、excel、ppt转pdf使用记录
话不多说,直接上代码,亲测可用,但是Linux需要添加字体,包在网上搜,太大无法上传<dependency><groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>15.8.0</version>
</dependency>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-cells</artifactId>
<version>21.8.0</version>
</dependency><dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-slides</artifactId>
<version>15.9.0</version>
</dependency>import com.aspose.cells.Workbook;
import com.aspose.words.*;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfWriter;
import org.apache.tomcat.util.http.fileupload.disk.DiskFileItem;
import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory;
import org.springframework.http.MediaType;
import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
public class PdfConverUtil {
/**
* @Param inputStream 源文件输入流
* @param fileName pdf文件名称
**/
public static InputStream wordToPdf(InputStream inputStream, String fileName) throws Exception {
DiskFileItem fileItem = (DiskFileItem) new DiskFileItemFactory().createItem("file", MediaType.ALL_VALUE, true, fileName + ".pdf");
OutputStream outputStream = fileItem.getOutputStream();
// 验证License 若不验证则转化出的pdf文档会有水印产生
if (!getLicense()) {
return null;
}
// 将源文件保存在com.aspose.words.Document中,具体的转换格式依靠里面的save方法
com.aspose.words.Document doc = new com.aspose.words.Document(inputStream);
// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,EPUB, XPS, SWF 相互转换
FontSettings.setFontsFolder("/usr/share/fonts/chinese", false);
doc.save(outputStream, SaveFormat.PDF);
outputStream.flush();
outputStream.close();
return fileItem.getInputStream();
}
/**
* excel 转 pdf
*
* @param inputStream excel文件
* @param convertSheets 需要转换的sheet
* @param fileName pdf名称
*/
public static InputStream excelToPdf(InputStream inputStream, int[] convertSheets, String fileName) throws Exception {
DiskFileItem fileItem = (DiskFileItem) new DiskFileItemFactory().createItem("file", MediaType.ALL_VALUE, true, fileName + ".pdf");
OutputStream outputStream = fileItem.getOutputStream();
// 验证 License
if (!getExeclLicense()) {
return null;
}
Workbook wb = new Workbook(inputStream);
com.aspose.cells.PdfSaveOptions pdfSaveOptions = new com.aspose.cells.PdfSaveOptions();
pdfSaveOptions.setOnePagePerSheet(true);
if (null != convertSheets) {
printSheetPage(wb, convertSheets);
}
wb.save(outputStream, pdfSaveOptions);
outputStream.flush();
outputStream.close();
return fileItem.getInputStream();
}
/**
* ppt 转 pdf
*
* @param inputStream ppt文件
* @param fileName pdf名称
*/
public static InputStream pptToPdf(InputStream inputStream, String fileName) throws Exception {
DiskFileItem fileItem = (DiskFileItem) new DiskFileItemFactory().createItem("file", MediaType.ALL_VALUE, true, fileName + ".pdf");
OutputStream outputStream = fileItem.getOutputStream();
// 验证 License
if (!getPPTLicense()) {
return null;
}
com.aspose.slides.Presentation pres = new com.aspose.slides.Presentation(inputStream);//输入pdf路径
pres.save(outputStream, com.aspose.slides.SaveFormat.Pdf);
outputStream.flush();
outputStream.close();
return fileItem.getInputStream();
}
/**
* 隐藏workbook中不需要的sheet页。
*
* @param sheets 显示页的sheet数组
*/
private static void printSheetPage(Workbook wb, int[] sheets) {
for (int i = 1; i < wb.getWorksheets().getCount(); i++) {
wb.getWorksheets().get(i).setVisible(false);
}
if (null == sheets || sheets.length == 0) {
wb.getWorksheets().get(0).setVisible(true);
} else {
for (int i = 0; i < sheets.length; i++) {
wb.getWorksheets().get(i).setVisible(true);
}
}
}
// 官方文档的要求 无需理会
public static boolean getLicense() {
boolean result = false;
try {
String s = "<License><Data><Products><Product>Aspose.Total for Java</Product><Product>Aspose.Words for Java</Product></Products><EditionType>Enterprise</EditionType><SubscriptionExpiry>20991231</SubscriptionExpiry><LicenseExpiry>20991231</LicenseExpiry><SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber></Data><Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature></License>";
ByteArrayInputStream is = new ByteArrayInputStream(s.getBytes());
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static boolean getExeclLicense() {
boolean result = false;
try {
String s = "<License><Data><Products><Product>Aspose.Total for Java</Product><Product>Aspose.Words for Java</Product></Products><EditionType>Enterprise</EditionType><SubscriptionExpiry>20991231</SubscriptionExpiry><LicenseExpiry>20991231</LicenseExpiry><SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber></Data><Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature></License>";
ByteArrayInputStream is = new ByteArrayInputStream(s.getBytes());
com.aspose.cells.License aposeLic = new com.aspose.cells.License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static boolean getPPTLicense() {
boolean result = false;
try {
String s = "<License><Data><Products><Product>Aspose.Total for Java</Product><Product>Aspose.Words for Java</Product></Products><EditionType>Enterprise</EditionType><SubscriptionExpiry>20991231</SubscriptionExpiry><LicenseExpiry>20991231</LicenseExpiry><SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber></Data><Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature></License>";
ByteArrayInputStream is = new ByteArrayInputStream(s.getBytes());
com.aspose.slides.License aposeLic = new com.aspose.slides.License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static void main(String[] args) throws IOException {
Charset defaultCharset = Charset.defaultCharset();
String charsetName = defaultCharset.name();
if (charsetName.equalsIgnoreCase("UTF-8")) {
System.out.println("Java环境的编码是UTF-8");
} else if (charsetName.equalsIgnoreCase("GBK")) {
System.out.println("Java环境的编码是GBK");
} else {
System.out.println("Java环境的编码是" + charsetName);
}
long old = System.currentTimeMillis();
if (!getPPTLicense()) {
System.out.println("PDF格式转化失败");
}
FileInputStream inputStream = new FileInputStream("C:\\Users\\fanbo\\Desktop\\bbb.ppt");
FileOutputStream byteArrayOutputStream = new FileOutputStream("C:\\Users\\fanbo\\Desktop\\bbb1.pdf");
com.aspose.slides.Presentation pres = new com.aspose.slides.Presentation(inputStream);//输入pdf路径
pres.save(byteArrayOutputStream, com.aspose.slides.SaveFormat.Pdf);
byteArrayOutputStream.close();
//ArrayList<String> imageUrllist = new ArrayList<String>(); // 图片list集合
//imageUrllist.add("C:\\Users\\fanbo\\Pictures\\Screenshots\\001.png");
//imageUrllist.add("C:\\Users\\fanbo\\Pictures\\Screenshots\\不上班001.png");
//imageUrllist.add("C:\\Users\\fanbo\\Pictures\\Screenshots\\不上班002.png");
//File file = ImgToPdf(imageUrllist, "C:\\Users\\fanbo\\Desktop\\img.pdf");// 生成pdf
//file.createNewFile();
long now = System.currentTimeMillis();
System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒");
}
}
牛人,高手在论坛 yaphoo 发表于 2023-11-13 17:26
牛人,高手在论坛
谢谢些,可不敢这么说,也是网上找的,参考的,总结了一下{:1_893:}
页:
[1]