aspose文档格式转换
生活随笔
收集整理的這篇文章主要介紹了
aspose文档格式转换
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文章目錄
- Word轉(zhuǎn)Pdf
- html轉(zhuǎn)pdf
Word轉(zhuǎn)Pdf
public static void main(String[] args) throws Exception {Document doc = new Document("C:\\Users\\w4523\\Desktop\\安全問(wèn)題.docx");doc.save("C:\\Users\\w4523\\Desktop\\1.pdf");}html轉(zhuǎn)pdf
import com.aspose.words.*;import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.ibatis.javassist.CannotCompileException; import org.apache.ibatis.javassist.NotFoundException; import org.slf4j.Logger; import org.slf4j.LoggerFactory;import java.io.*; import java.net.URL;/*** html轉(zhuǎn)pdf工具類(lèi)* Created by liul on 2016-07-20.*/ public class Html2PdfUtils {private static final Logger log = LoggerFactory.getLogger(Html2PdfUtils.class);private static final int wdFormatPDF = 17;// PDF 格式/*** 調(diào)用aspose.words方式將 html 轉(zhuǎn)為 pdf 文件** @param content html 內(nèi)容* @param outPath pdf存儲(chǔ)路徑* @return true 轉(zhuǎn)換成功,false 轉(zhuǎn)換失敗*/public static boolean html2pdfByAspose(String content, String outPath) {FileOutputStream os = null;try {long start = System.currentTimeMillis();File file = new File(outPath); // 新建一個(gè)空白pdf文檔os = new FileOutputStream(file);LoadOptions loadOptions = new LoadOptions();loadOptions.setLoadFormat(SaveFormat.HTML);InputStream inputStream = new ByteArrayInputStream(content.getBytes());Document doc = new Document(inputStream, loadOptions); // Address是將要被轉(zhuǎn)化的word文檔DocumentBuilder builder = new DocumentBuilder(doc);PageSetup pageSetup = builder.getPageSetup();pageSetup.setLeftMargin(15.0);pageSetup.setRightMargin(15.0);pageSetup.setTopMargin(35.0);pageSetup.setBottomMargin(15.0);doc.save(os, SaveFormat.PDF);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,// EPUB, XPS, SWF 相互轉(zhuǎn)換log.info("html轉(zhuǎn)pdf共耗時(shí):{} ms", System.currentTimeMillis() - start); // 轉(zhuǎn)化用時(shí)os.close();return true;} catch (Exception e) {log.error("html轉(zhuǎn)pdf異常: {}", e);} finally {if (os != null) {IOUtils.closeQuietly(os);}}return false;}/*** 調(diào)用aspose.words方式將 html 轉(zhuǎn)成 pdf 文件流** @param content 文件內(nèi)容* @return pdf文件流*/public static ByteArrayOutputStream html2pdfStreamByAspose(String content) {ByteArrayOutputStream os = null;try {long start = System.currentTimeMillis();os = new ByteArrayOutputStream();LoadOptions loadOptions = new LoadOptions();loadOptions.setLoadFormat(SaveFormat.HTML);InputStream inputStream = new ByteArrayInputStream(content.getBytes());Document doc = new Document(inputStream, loadOptions); // Address是將要被轉(zhuǎn)化的word文檔doc.save(os, SaveFormat.PDF);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,// EPUB, XPS, SWF 相互轉(zhuǎn)換log.info("html轉(zhuǎn)pdf共耗時(shí):{} ms", System.currentTimeMillis() - start); // 轉(zhuǎn)化用時(shí)} catch (Exception e) {log.error("html轉(zhuǎn)pdf異常: {}", e);} finally {if (os != null) {IOUtils.closeQuietly(os);}}return os;}public static void main(String[] args) throws IOException, NotFoundException, CannotCompileException {InputStream inputStream = Html2PdfUtils.class.getClassLoader().getResourceAsStream("templates/pt/template/ele_seal_collection_form.html");int available = inputStream.available();byte[] bytes = new byte[available];inputStream.read(bytes);String content = new String(bytes);// String inPath = "E:\\testfile\\test_table2.html"; // String content = FileUtils.readFileToString(new File(inPath), "utf-8");content = HtmlUtils.formatPdfHtml(content);System.out.println(content);String outPath = "E:\\testfile\\test_table2.pdf";html2pdfByAspose(content, outPath); // // ByteArrayOutputStream outputStream = html2pdfStreamByAspose(content); // // FileOutputStream fileOutputStream = new FileOutputStream("E:\\testfile\\test_table22222.pdf"); // fileOutputStream.write(outputStream.toByteArray()); // fileOutputStream.flush(); // fileOutputStream.close();} }總結(jié)
以上是生活随笔為你收集整理的aspose文档格式转换的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 喷泉模型详解
- 下一篇: 代码实现PHP web服务器下绝对路径到