java实现office文件的在线预览

参考文章: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

 

你可能感兴趣的:(Java编程)