参考文章:PDF技术(一)-Java实现Office系列文件转PDF文件
利用aspose.jar来实现将文件转pdf,再传前台,实现预览。
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import org.apache.log4j.Logger;
import com.aspose.cells.CellsHelper;
import com.aspose.cells.Workbook;
import com.aspose.cells.Worksheet;
import com.aspose.words.FontSettings;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
import wt.log4j.LogR;
/**
* @功能描述 aspose Excel转pdf
*/
public class OfficeToPdf {
private static final String FONT = File.separator + "util" + File.separator
+ "fonts" + File.separator ;//字体文件夹
private static final Logger LOGGER = LogR.getLogger(DocController.class.getName());
public static void main(String[] args) throws IOException, DocumentException{
excel2pdf("D:/logs/SpringBoot.xls");
// doc2pdf("d:/logs/SpringBoot.docx");
// text2pdf("d://logs//123.txt","d://logs//SpringBoot.pdf");
}
/**
* 签名验证
* @return
* @throws IOException
* @throws Exception
*/
public static boolean getWordLicense() throws IOException {
boolean result = false;
InputStream is = null;
try {
is = OfficeToPdf.class.getResourceAsStream("license.xml"); // license.xml应放在..\WebRoot\WEB-INF\classes路径下
com.aspose.words.License aposeLic = new com.aspose.words.License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (is != null) {
is.close();
}
}
return result;
}
/**
* Excel签名验证
* @return
* @throws IOException
* @throws Exception
*/
public static boolean getExcelLicense() throws IOException {
boolean result = false;
InputStream is = null;
try {
is = OfficeToPdf.class.getResourceAsStream("license.xml"); // license.xml应放在..\WebRoot\WEB-INF\classes路径下
com.aspose.cells.License aposeLic = new com.aspose.cells.License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (is != null) {
is.close();
}
}
return result;
}
/**
* excel转PDF
* @return
* @throws IOException
* @throws Exception
*/
public static void excel2pdf(String fileName) throws IOException {
if (!getExcelLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
return;
}
FileOutputStream fileOS = null;
try {
long old = System.currentTimeMillis();
Workbook wb = new Workbook(fileName);// 原始excel路径
fileName = fileName.substring(0,fileName.lastIndexOf(".")+1) + "pdf";
File file = new File(fileName);// 输出路径
for (int i = 0;i<wb.getWorksheets().getCount();i++){
Worksheet worksheet = wb.getWorksheets().get(i);
worksheet.getPageSetup().setZoom(50);//设置缩放比例
}
CellsHelper.setFontDir(FONT); //这里应该是字体路径
fileOS = new FileOutputStream(file);
wb.save(fileOS, com.aspose.cells.SaveFormat.PDF);
fileOS.close();
long now = System.currentTimeMillis();
LOGGER.info("共耗时:" + ((now - old) / 1000.0) + "秒\n\n");//转化过程耗时
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fileOS!=null){
fileOS.close();
}
}
}
/**
* doc转PDF
* @param filename
* @return
* @throws IOException
* @throws Exception
*/
public static void doc2pdf(String filename) throws IOException {
if (!getWordLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
return;
}
FileOutputStream os = null;
try {
long old = System.currentTimeMillis();
com.aspose.words.Document doc = new com.aspose.words.Document(filename); //Address是将要被转化的word文档
filename = filename.substring(0,filename.lastIndexOf(".")+1) + "pdf";
File file = new File(filename); //新建一个空白pdf文档
os = new FileOutputStream(file);
//设置字体来源
FontSettings.setFontsFolder(FONT, true);
// doc.save("d:/logs/机构案例库入口配置文件.html", SaveFormat.HTML);//转html格式
doc.save(os, com.aspose.words.SaveFormat.PDF);//全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
long now = System.currentTimeMillis();
LOGGER.info("共耗时:" + ((now - old) / 1000.0) + "秒\n\n");//转化过程耗时
os.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (os != null){
os.close();
}
}
}
/**
* txt转pdf
* @param doc
* @return
* @throws Exception
*/
public static void text2pdf(String text, String pdf) throws DocumentException, IOException {
com.itextpdf.text.Document document = new com.itextpdf.text.Document();
OutputStream os = new FileOutputStream(new File(pdf));
PdfWriter.getInstance(document, os);
document.open();
BaseFont baseFont = BaseFont.createFont(FONT +"simsun.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
Font font = new Font(baseFont);
InputStreamReader isr = new InputStreamReader(new FileInputStream(new File(text)), "GBK");
BufferedReader bufferedReader = new BufferedReader(isr);
String str = "";
while ((str = bufferedReader.readLine()) != null) {
document.add(new Paragraph(str, font));
}
document.close();
bufferedReader.close();
isr.close();
os.close();
}
}