日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

aspose文档格式转换

發(fā)布時間:2024/1/1 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 aspose文档格式转换 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文章目錄

  • 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\\安全問題.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工具類* 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存儲路徑* @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); // 新建一個空白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共耗時:{} ms", System.currentTimeMillis() - start); // 轉(zhuǎn)化用時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共耗時:{} ms", System.currentTimeMillis() - start); // 轉(zhuǎn)化用時} 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文档格式转换的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。