word转pdf+word转html

第一步 引入 aspose-words-15.8.0-jdk16.jar 包

在这里插入图片描述

<dependency>
    <groupId>com.aspose.words</groupId>
    <artifactId>aspose-words</artifactId>
    <version>words-15.8.0-jdk16</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/src/main/resources/lib/aspose-words-15.8.0-jdk16.jar</systemPath>
</dependency>

第二步 存入凭证license.xml

<License>
    <Data>
        <Products>
            <Product>Aspose.Total for Java</Product>
            <Product>Aspose.Words for Java</Product>
        </Products>
        <EditionType>Enterprise</EditionType>
        <SubscriptionExpiry>20991231</SubscriptionExpiry>
        <LicenseExpiry>20991231</LicenseExpiry>
        <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>
    </Data>
    <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>
</License>

在这里插入图片描述

第三步 工具类


import com.aspose.words.*;
import com.aspose.words.Shape;
import lombok.extern.slf4j.Slf4j;

import java.awt.*;
import java.io.*;

/**
 * @author ztc
 * @create 2023-03-17 10:35
 */
@Slf4j
public class DocumentUtils {
    private static final String str = "Evaluation Only. Created with Aspose.Words. Copyright 2003-2015 Aspose Pty Ltd.";

    private static boolean getLicense() {
        boolean result = false;
        try {
            InputStream is = DocumentUtils.class.getClassLoader().getResourceAsStream("license.xml");
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * word转pdf
     * @param wordPath word文件路径
     * @param pdfPath pdf文件路径
     */
    public static void docToPdf(String wordPath, String pdfPath) {
        if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
            return;
        }
        try {
            long old = System.currentTimeMillis();
            //新建一个pdf文档
            File file = new File(pdfPath);
            FileOutputStream os = new FileOutputStream(file);
            //Address是将要被转化的word文档
            Document doc = new Document(wordPath);
            //为doc文档添加水印
//            insertWatermarkText(doc, "水印文字");
            //全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB
            doc.save(os, com.aspose.words.SaveFormat.PDF);
            // XPS, SWF 相互转换
            long now = System.currentTimeMillis();
            os.close();
            //转化用时
            System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 为word文档添加水印
     * @param doc word文档模型
     * @param watermarkText 需要添加的水印字段
     * @throws Exception
     */
    private static void insertWatermarkText(Document doc, String watermarkText) throws Exception {
        Shape watermark = new Shape(doc, ShapeType.TEXT_PLAIN_TEXT);
        //水印内容
        watermark.getTextPath().setText(watermarkText);
        //水印字体
        watermark.getTextPath().setFontFamily("宋体");
        //水印宽度
        watermark.setWidth(500);
        //水印高度
        watermark.setHeight(100);
        //旋转水印
        watermark.setRotation(-40);
        //水印颜色
        watermark.getFill().setColor(Color.lightGray);
        watermark.setStrokeColor(Color.lightGray);
        watermark.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
        watermark.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
        watermark.setWrapType(WrapType.NONE);
        watermark.setVerticalAlignment(VerticalAlignment.CENTER);
        watermark.setHorizontalAlignment(HorizontalAlignment.CENTER);
        Paragraph watermarkPara = new Paragraph(doc);
        watermarkPara.appendChild(watermark);
        for (Section sect : doc.getSections())
        {
            insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_PRIMARY);
            insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_FIRST);
            insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_EVEN);
        }
        System.out.println("Watermark Set");
    }

    /**
     * 在页眉中插入水印
     * @param watermarkPara
     * @param sect
     * @param headerType
     * @throws Exception
     */
    private static void insertWatermarkIntoHeader(Paragraph watermarkPara, Section sect, int headerType) throws Exception{
        HeaderFooter header = sect.getHeadersFooters().getByHeaderFooterType(headerType);
        if (header == null)
        {
            header = new HeaderFooter(sect.getDocument(), headerType);
            sect.getHeadersFooters().add(header);
        }
        header.appendChild(watermarkPara.deepClone(true));
    }

    /**
     * word转html文本
     * @param wordPath word文件路径
     * @return
     */
    public static String parseWord2Html(String wordPath) throws Exception {
        Document doc = new Document(wordPath);
        HtmlSaveOptions saveOptions = new HtmlSaveOptions();
        saveOptions.setExportHeadersFootersMode(ExportHeadersFootersMode.NONE); // HtmlSaveOptions的其他设置信息请参考相关API
        ByteArrayOutputStream htmlStream = new ByteArrayOutputStream();
        String htmlText = "";
        try {
            doc.save(htmlStream, saveOptions);
            htmlText = new String(htmlStream.toByteArray(),"UTF-8");
            htmlStream.close();
        } catch (Exception e) {
            log.error("word文件转换失败,详细错误信息:{}",e.getMessage());
        }
        return htmlText.replace(str,"");
    }

    public static void main(String[] args) {
        String wordUrl = "C:\\Users\\ztc\\Desktop\\新建文件夹\\xxx(1).doc";
        String wordUrl2 = "C:\\Users\\ztc\\Desktop\\新建文件夹\\xxx(1).docx";
        //word->pdf
//        String pdfUrl = "C:\\Users\\ztc\\Desktop\\新建文件夹\\pdf111.pdf";
//        WordToPdfUtils.docToPdf(wordUrl,pdfUrl);
        try {
            String s = parseWord2Html(wordUrl);
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("C:\\Users\\ztc\\Desktop\\新建文件夹\\1.html")));
            bos.write(s.getBytes());
            bos.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
}

结尾

不太方便放效果图。 可以直接照搬用。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值