所需jar包
- aspose-slides-1.0.0.jar
- aspose-cells-1.0.0.jar
- aspose-words-1.0.0.jar
配置文件
放在resources资源文件夹下
<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>
Word文档生成预览图片
import com.aspose.words.Document;
import com.aspose.words.ImageSaveOptions;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
public class WordToPDF {
HttpServletResponse response = null;
public static boolean getLicense() {
boolean result = false;
try {
InputStream is = WordToPDF.class.getClassLoader().getResourceAsStream("license.xml");
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 文档转图片
* @param inPath 传入文档地址
* @param outDir 输出的图片文件夹地址
*/
public static void doc2Img(String inPath, String outDir){
try {
if (!getLicense()) {
throw new Exception("com.aspose.words lic ERROR!");
}
Document doc = new Document(inPath);
ImageSaveOptions options = new ImageSaveOptions(SaveFormat.PNG);
int pageCount = 1;
for (int i = 0; i < pageCount; i++) {
File file = new File(outDir);
FileOutputStream os = new FileOutputStream(file);
options.setPageIndex(i);
doc.save(os, options);
os.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Excel生成预览图片
import com.aspose.cells.*;
import java.io.*;
public class ExcelToPDF {
public static boolean getLicense() {
boolean result = false;
try {
InputStream is = ExcelToPDF.class.getClassLoader().getResourceAsStream("license.xml");
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* Excel转图片
* @param inPath 传入文档地址
* @param outDir 输出的图片文件夹地址
*/
public static void exc2Img(String inPath, String outDir){
try {
if (!getLicense()) {
throw new Exception("com.aspose.words lic ERROR!");
}
// Excel文档
Workbook book = new Workbook(inPath);
Worksheet sheet = book.getWorksheets().get(0);
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
imgOptions.setImageFormat(ImageFormat.getJpeg());
imgOptions.setCellAutoFit(true);
imgOptions.setOnePagePerSheet(true);
SheetRender render = new SheetRender(sheet, imgOptions);
render.toImage(0, outDir);
} catch (Exception e) {
e.printStackTrace();
}
}
}
PPT生成预览图片
import com.aspose.slides.ISlide;
import com.aspose.slides.License;
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
public class PptToPDF {
public static boolean getLicense() {
boolean result = false;
try {
InputStream is = PptToPDF.class.getClassLoader().getResourceAsStream("license.xml");
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/** ppt转图片 */
public static void ppt2Img(String inPath, String outDir){
try {
if (!getLicense()) {
throw new Exception("com.aspose.ppt lic ERROR!");
}
// ppt文档
Presentation pres = new Presentation(inPath);
ISlide slide = pres.getSlides().get_Item(0);
int height = (int)(pres.getSlideSize().getSize().getHeight()-150);
int width = (int)(pres.getSlideSize().getSize().getWidth()-150);
BufferedImage image = slide.getThumbnail(new java.awt.Dimension(width, height));
File outImage = new File(outDir);
ImageIO.write(image, "png", outImage);
} catch (Exception e) {
e.printStackTrace();
}
}
}
\