system-oa/src/main/java/cn/com/tenlion/systemoa/utils/OfficeToPDFUtils.java
2022-04-12 14:23:42 +08:00

195 lines
6.4 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package cn.com.tenlion.systemoa.utils;
import com.aspose.cells.Workbook;
import com.aspose.slides.Presentation;
import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveOutputParameters;
import java.io.*;
public class OfficeToPDFUtils {
private static InputStream license;
public static boolean judgeLicense() {
boolean result = false;
try {
license = OfficeToPDFUtils.class.getClassLoader().getResourceAsStream("static/assets/js/pdf/license"); //文件路径
if (license != null) {
License aposeLic = new License();
aposeLic.setLicense(license);
result = true;
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
// 转换
public static void trans(String filePath, String pdfPath, String type, String folderPath) {
if (!judgeLicense()) {
System.out.println("license错误");
}
try {
System.out.println("as开始" + filePath);
long old = System.currentTimeMillis();
File folder = new File(folderPath);
if(!folder.exists() && !folder.isDirectory()) {
folder.mkdirs();
}
File file = new File(pdfPath);
toPdf(file, filePath, pdfPath, type);
long now = System.currentTimeMillis();
System.out.println("完成:" + pdfPath);
System.out.println("共耗时:" + ((now - old) / 1000.0) + "");
} catch (Exception e) {
e.printStackTrace();
}
}
private static void toPdf(File file, String filePath, String pdfPath, String type) {
if ("doc".equals(type) || "docx".equals(type) || "txt".equals(type)) {
wordToPdf(file, filePath);
} else if ("xls".equals(type) || "xlsx".equals(type)) {
excelToPdf(file, filePath, pdfPath);
} else if ("ppt".equals(type) || "pptx".equals(type)) {
pptToPdf(file, filePath);
}else{
System.out.println("暂不支持该类型:"+type);
}
}
private static void wordToPdf(File file, String filePath) {
FileOutputStream os = null;
Document doc;
try {
os = new FileOutputStream(file);
doc = new Document(filePath);
SaveOutputParameters saveOutputParameters = doc.save(os, com.aspose.words.SaveFormat.PDF);
System.out.println("转换完成");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/*private static void excelToPdf(File file, String filePath) {
FileOutputStream os = null;
try {
os = new FileOutputStream(file);
com.aspose.cells.Workbook wb = new com.aspose.cells.Workbook(filePath);
wb.save(os, com.aspose.cells.SaveFormat.PDF);
System.out.println("转换完成");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}*/
private static void excelToPdf(File file, String filePath, String pdfPath) {
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
try {
File excelFile = new File(filePath);
if (excelFile.exists()) {
fileInputStream = new FileInputStream(excelFile);
Workbook workbook = new Workbook(fileInputStream);
File pdfFile = new File(pdfPath);
com.aspose.cells.PdfSaveOptions pdfSaveOptions = new com.aspose.cells.PdfSaveOptions();
pdfSaveOptions.setOnePagePerSheet(true);
FileOutputStream fileOS = new FileOutputStream(pdfFile);
workbook.save(fileOS, pdfSaveOptions);
} else {
throw new Exception("文件不存在");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private static void pptToPdf(File file, String filePath) {
FileOutputStream os = null;
try {
os = new FileOutputStream(file);
Presentation pres = new Presentation(filePath);// 输入pdf路径
pres.save(os, com.aspose.slides.SaveFormat.Pdf);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 设置打印的sheet 自动拉伸比例
* @param wb
* @param page 自动拉伸的页的sheet数组
*/
public static void autoDraw(Workbook wb,int[] page){
if(null!=page&&page.length>0){
for (int i = 0; i < page.length; i++) {
wb.getWorksheets().get(i).getHorizontalPageBreaks().clear();
wb.getWorksheets().get(i).getVerticalPageBreaks().clear();
}
}
}
/**
* 隐藏workbook中不需要的sheet页。
* @param wb
* @param page 显示页的sheet数组
*/
public static void printSheetPage(Workbook wb,int[] page){
for (int i= 1; i < wb.getWorksheets().getCount(); i++) {
wb.getWorksheets().get(i).setVisible(false);
}
if(null==page||page.length==0){
wb.getWorksheets().get(0).setVisible(true);
}else{
for (int i = 0; i < page.length; i++) {
wb.getWorksheets().get(i).setVisible(true);
}
}
}
/*public static void main(String[] args) {
String filePath = "C:\\Users\\29492\\Desktop\\test.pdf";
String PdfFilePath = "C:\\Users\\29492\\Desktop\\test111.pdf";
trans(filePath, PdfFilePath, "pdf");
}*/
}