Word轉爲PDF,並在線預覽。

接上一篇文章,寫到一半突然有事,就分兩篇發了。
上一篇已經將word轉爲了pdf格式的文件,不知道小夥伴有沒有使用成功的? 失敗的話可以留言,我會盡量解決。
下面開始word轉pdf.
第一步:word轉爲pdf
下面是工具類源碼:

package demo;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

import com.aspose.cells.License;
import com.aspose.words.Document;

/**
 * 
 * word文檔 轉換 PDF
 */
public class WordToPdf {
	
	/**
	 * 獲取license許可憑證
	 * @return
	 */
	private static boolean getLicense() {
		boolean result = false;
		try {
			String licenseStr = "<License>\n"
					+ " <Data>\n"
					+ " <Products>\n"
					+ " <Product>Aspose.Total for Java</Product>\n"
					+ " <Product>Aspose.Words for Java</Product>\n"
					+ " </Products>\n"
					+ " <EditionType>Enterprise</EditionType>\n"
					+ " <SubscriptionExpiry>20991231</SubscriptionExpiry>\n"
					+ " <LicenseExpiry>20991231</LicenseExpiry>\n"
					+ " <SerialNumber>23dcc79f-44ec-4a23-be3a-03c1632404e9</SerialNumber>\n"
					+ " </Data>\n"
					+ " <Signature>0nRuwNEddXwLfXB7pw66G71MS93gW8mNzJ7vuh3Sf4VAEOBfpxtHLCotymv1PoeukxYe31K441Ivq0Pkvx1yZZG4O1KCv3Omdbs7uqzUB4xXHlOub4VsTODzDJ5MWHqlRCB1HHcGjlyT2sVGiovLt0Grvqw5+QXBuinoBY0suX0=</Signature>\n"
					+ "</License>";
			InputStream license = new ByteArrayInputStream(licenseStr.getBytes("UTF-8"));
			License asposeLic = new License();
			asposeLic.setLicense(license);
			System.out.println("zxu");
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * word文檔  轉換爲 PDF
	 * @param inPath 源文件
	 * @param outPath 目標文件
	 */
	public static void doc2pdf(String inPath, String outPath) {
		
		//驗證License,獲取許可憑證
        if (!getLicense()) { 
        	return;
        }
        
        try {
        	
        	//新建一個PDF文檔
        	File file = new File(outPath);
        	//新建一個IO輸出流
            FileOutputStream os = new FileOutputStream(file);
            //獲取將要被轉化的word文檔
            Document doc = new Document(inPath); 
            // 全面支持DOC, DOCX,OOXML, RTF HTML,OpenDocument,PDF, EPUB, XPS,SWF 相互轉換
            doc.save(os, com.aspose.words.SaveFormat.PDF);
            os.close();
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
	
	public static void main(String[] args) {

		doc2pdf("e:/ceshi.doc", "e:ceshi----001.pdf");
		
	}
	
}

這個也是可以直接複製到工具類中, 獲取license許可憑證,這個無需多管。
所需要的jar包在這裏拿
鏈接: https://pan.baidu.com/s/1QNl2Xs43FbERx2ZF5piIHQ
提取碼: 23ib
提取到jar包,引入工具類。程序應該就可以運行了,可以再本地測試一下,剛纔生成的word文檔能不能轉成pdf呢?
測試方法中 doc2pdf("e:/ceshi.doc", "e:ceshi----001.pdf");
第一個參數是word文檔的路徑,第二個參數是生成pdf的路徑。注意後綴名要寫對。

**第二步:**pdf在線預覽功能
這個功能在網上找了好久都沒有合適的,我這個還是很實用的,只需要調用一個路徑即可。
適合web項目頁面的使用。並且兼容各大瀏覽器和手機。
在這裏需要倆個文件夾,js文件。
鏈接: https://pan.baidu.com/s/154mPz2-5bukQ7Qh5rbyZ_g
提取碼: x128
這兩個文件夾要放到你生成pdf文件的同一級路徑。
之後調用在頁面上調用 window.open(‘<%=basePath%>uploadFiles/uploadFile/web/viewer.html?file=../ceshi.pdf’)
ceshi.pdf就是你pdf的文件名稱。
如此就可以打開一個新的頁面,pdf預覽頁面。
大家可以親測一下,除了在線預覽有文件沒上傳,寫入word和word轉pdf這兩個功能是可以實現的。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章