aspose文档格式转换

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();

    }
}

在这里插入图片描述

pdf转word

    String input="C:\\Users\\HHH\\Desktop\\会议记录.pdf";
    String output="C:\\Users\\HHH\\Desktop\\会议记录123.docx";

    @Test
    public void test1() throws Exception {


        // Load source PDF file
        Document doc = new Document(input);

        // Save resultant DOCX file
        doc.save(output, SaveFormat.DocX);

        System.out.println("--------------------------------");
    }

格式有问题,错位,并且文字是文本框,使用DocSaveOptions解决
在这里插入图片描述
DocSaveOptions设置,官方文档:https://reference.aspose.com/pdf/zh/java/com.aspose.pdf/docsaveoptions/

    @Test
    public void test2() throws Exception {
        // Load source PDF file
        Document doc = new Document(input);

// Instantiate DocSaveOptions instance
        DocSaveOptions saveOptions = new DocSaveOptions();

// Set output format
        saveOptions.setFormat(DocSaveOptions.DocFormat.DocX);

// Set the recognition mode as Flow
        saveOptions.setMode(DocSaveOptions.RecognitionMode.Flow);

// Save resultant DOCX file
        doc.save(output, saveOptions);
    }

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值