日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

aspose文档格式转换

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

文章目錄

  • Word轉Pdf
  • html轉pdf

Word轉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轉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轉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 格式/*** 調用aspose.words方式將 html 轉為 pdf 文件** @param content html 內容* @param outPath pdf存儲路徑* @return true 轉換成功,false 轉換失敗*/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是將要被轉化的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 相互轉換log.info("html轉pdf共耗時:{} ms", System.currentTimeMillis() - start); // 轉化用時os.close();return true;} catch (Exception e) {log.error("html轉pdf異常: {}", e);} finally {if (os != null) {IOUtils.closeQuietly(os);}}return false;}/*** 調用aspose.words方式將 html 轉成 pdf 文件流** @param content 文件內容* @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是將要被轉化的word文檔doc.save(os, SaveFormat.PDF);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,// EPUB, XPS, SWF 相互轉換log.info("html轉pdf共耗時:{} ms", System.currentTimeMillis() - start); // 轉化用時} catch (Exception e) {log.error("html轉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();} }

總結

以上是生活随笔為你收集整理的aspose文档格式转换的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。