java解析wrod带公式带图片

直接上代码

word里面添加的图片不能浮动,这个很重要

package com.topic.util;

import com.alibaba.fastjson.JSONObject;
import com.commons.util.date.DateUtil;
import net.arnx.wmf2svg.gdi.svg.SvgGdi;
import net.arnx.wmf2svg.gdi.wmf.WmfParser;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.model.PicturesTable;
import org.apache.poi.hwpf.usermodel.*;
import org.w3c.dom.Document;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;
import java.util.*;
import java.util.List;


public class WordTest {

    public static List<Map<String,Object>> analysisWord(String  ipPath,String projectPath,FileInputStream is) throws Exception {
        List<Map<String,Object>> resultList=new ArrayList<>(16);
        HWPFDocument document = new HWPFDocument(is);
        Range range = document.getRange();
        StringBuffer data = new StringBuffer();
        // 创建图片容器
        PicturesTable pTable = document.getPicturesTable();

        for (int i = 0; i < range.numParagraphs(); i++) {
            Paragraph paragraph = range.getParagraph(i);//段落
            String text = paragraph.text();
            System.out.println(text);
            text = text.replaceAll("\u0013 EMBED Equation.DSMT4 \u0014\u0001\u0015", "\u0013");
            text = text.replaceAll("\u0013 EMBED Equation.KSEE3", "");
            text = text.replaceAll("\\* MERGEFORMAT \u0014\u0001\u0015", "\u0013");
            text = text.replaceAll("\u0001", "\u0013");
            text = text.replaceAll("\r", "<br/>");
            text = text.replaceAll("\t", "");
            text = text.replaceAll("\b", "");

            data.append(text);

        }

        List<String> picList = new ArrayList<>(16);
        String name = "";
        List<Picture> pic = pTable.getAllPictures();
        for (Picture img : pic) {
            String storePath="/word/"+DateUtil.getShortSystemDate()+"/";
            name = UUID.randomUUID().toString().replaceAll("-", "");
            String afileName = img.suggestFullFileName();
            String suffix = afileName.substring(afileName.lastIndexOf(".") + 1);
            try {
                File targetFile = new File(projectPath+storePath );
                if (!targetFile.exists()) {
                    targetFile.mkdirs();
                }
                OutputStream out = new FileOutputStream(projectPath+storePath +"/"+name+ "." + suffix);
                img.writeImageContent(out);
                out.close();
                if(suffix.equals("wmf")){
                    InputStream in = new FileInputStream(new File(projectPath  +storePath+"/"+ name + "." + suffix));
                    WmfParser parser = new WmfParser();
                    final SvgGdi gdi = new SvgGdi(false);
                    parser.parse(in, gdi);
                    Document doc = gdi.getDocument();
                    OutputStream out1 = new FileOutputStream(new File(projectPath   +storePath+"/"+ name + "." + "svg"));
                    output(doc, out1);
                    picList.add(ipPath+"/"+storePath+name + "." + "svg");
                }else{
                    Im4JavaUtils.copyImage(projectPath+storePath+"/"  + name + "." + suffix, projectPath   +storePath+"/"+ name + ".jpg");
                    picList.add(ipPath+"/"+storePath+name + "." + "jpg");
                }
            } catch (Exception e) {
                System.out.println(e);
            }

        }
        String html=data.toString();
        for(String path:picList){
            path = "<img class='image' src='"+path+"'>";
            html= html.replaceFirst("\u0013",path);
        }
        String[] datas=html.split("【结束】");
        for(String word:datas){
            Map<String,Object> result=new HashMap<>(16);
            if(!word.contains("【题干】")){
                continue;
            }
            String xuhao= word.substring(word.indexOf("【序号】")+4,word.lastIndexOf("【所在页】"));//序号
            String suozaiye= word.substring(word.indexOf("【所在页】")+5,word.lastIndexOf("【题号】"));//所在页
            String tihao= word.substring(word.indexOf("【题号】")+4,word.lastIndexOf("【标题】"));//题号
            String  topic_name=word.substring(word.indexOf("【标题】") + 4, word.indexOf("【题干】"));//题目
            String topic_dry = word.substring(word.indexOf("【题干】") + 4, word.indexOf("【答案】"));//题干
            String answers= word.substring(word.indexOf("【答案】")+4,word.lastIndexOf("【解析】"));//答案
            String parsing=word.substring(word.indexOf("【解析】") + 4, word.indexOf("【题目来源】"));//解析
            String topic_source=word.substring(word.indexOf("【题目来源】") +6, word.indexOf("【教学目的】"));//题目来源
            String teaching_aims=word.substring(word.indexOf("【教学目的】") +6, word.indexOf("【备注】"));//教学目的
            String node=word.substring(word.indexOf("【备注】") + 4);//备注
            result.put("xuhao",xuhao);
            result.put("suozaiye",suozaiye);
            result.put("tihao",tihao);
            result.put("topic_name",topic_name);
            result.put("topic_dry",topic_dry);
            result.put("answers",answers);
            result.put("parsing",parsing);
            result.put("topic_source",topic_source);
            result.put("teaching_aims",teaching_aims);
            result.put("node",node);
            resultList.add(result);


        }
        System.out.println( JSONObject.toJSONString(resultList));
        return resultList;
    }

    public static void output(Document doc, OutputStream out) throws Exception {
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC,"-//W3C//DTD SVG 1.0//EN");
        transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd");
        transformer.transform(new DOMSource(doc), new StreamResult(out));
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        transformer.transform(new DOMSource(doc), new StreamResult(bos));
        out.flush();
        out.close();
    }


    public static void main(String[] args)  throws Exception{
//        String aa="【所在页】办不办【教学目的】更杠【结束】【所在页】办不办【教学目的】更杠【结束】";
//        String[] datas=aa.split("【结束】");
//        for(String data:datas){
//           String hh=data.substring(data.indexOf("【教学目的】")+6);
//
//            System.out.println(data);
//        }
//        //aa=aa.substring(aa.indexOf("【所在页】")+5,aa.lastIndexOf("【教学目的】"));
//        System.out.println("1");

        analysisWord("http://api.xxx.com.cn/","D:\\home\\images\\",new FileInputStream(new File("D://test1.doc")));
    }
}
package com.topic.util;

import org.im4java.core.ConvertCmd;
import org.im4java.core.IMOperation;
import org.im4java.core.IdentifyCmd;
import org.im4java.core.ImageCommand;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class Im4JavaUtils {
    private static final Logger logger = LoggerFactory.getLogger(Im4JavaUtils.class);

    /** 是否使用 GraphicsMagick (Windows下为true) **/
    private static final boolean USE_GRAPHICS_MAGICK_PATH = true;
    /** ImageMagick 安装目录 **/
    private static final String IMAGE_MAGICK_PATH = "D:\\Program Files\\ImageMagick-7.0.10-Q16-HDRI";
    /** GraphicsMagick 安装目录 **/
    private static final String GRAPHICS_MAGICK_PATH = "D:\\Program Files\\GraphicsMagick-1.3.35-Q16";

    /**
     * 获取 ImageCommand
     * @param comm 命令类型(convert, identify)
     * @return
     */
    private static ImageCommand getImageCommand(String comm) {
        ImageCommand cmd = null;
        if ("convert".equalsIgnoreCase(comm)) {
            cmd = new ConvertCmd(USE_GRAPHICS_MAGICK_PATH);
        } else if ("identify".equalsIgnoreCase(comm)) {
            cmd = new IdentifyCmd(USE_GRAPHICS_MAGICK_PATH);
        } // else if....
//        Windows环境下需要配置应用路径
        if (cmd != null && System.getProperty("os.name").toLowerCase().indexOf("windows") != -1) {
            cmd.setSearchPath(USE_GRAPHICS_MAGICK_PATH ? GRAPHICS_MAGICK_PATH : IMAGE_MAGICK_PATH);
        }
        return cmd;
    }


    /**
     * 降低品质,以减小文件大小
     * @param path 原文件路径
     * @param des 目标文件路径
     * @param quality 保留品质(1-100)
     * @throws Exception
     */
    public static void reduceQuality(String path, String des, double quality) throws Exception {
        createDirectory(des);
        IMOperation op = new IMOperation();
        op.addImage(path);
        op.quality(quality);
        op.addImage(des);
        ConvertCmd cmd = (ConvertCmd) getImageCommand("convert");
        cmd.run(op);
    }

    /**
     * 改变图片大小比例
     * @param path 原文件路径
     * @param des 目标文件路径
     * @param ratio 缩放比例
     * @throws Exception
     */
    public static void resizeImage(String path, String des, String ratio) throws Exception {
//        创建目标文件
        createDirectory(des);
        IMOperation op = new IMOperation();
        op.addImage(path);
        op.addRawArgs(ratio);
        op.addImage(des);

        ConvertCmd cmd = (ConvertCmd) getImageCommand("convert");
        cmd.run(op);
    }

    /**
     * 等比缩放图片(如果width为空,则按height缩放; 如果height为空,则按width缩放)
     * @param path 原文件路径
     * @param des 目标文件路径
     * @param width 缩放后的宽度
     * @param height 缩放后的高度
     * @param sample 是否以缩放方式,而非缩略图方式
     * @throws Exception
     */
    public static void scaleResizeImage(String path, String des, Integer width, Integer height, boolean sample) throws Exception {
        createDirectory(des);
        IMOperation op = new IMOperation();
        op.addImage(path);
        if (sample) op.resize(width, height);
        else op.sample(width, height);
        op.addImage(des);
        ConvertCmd cmd = (ConvertCmd) getImageCommand("convert");
        cmd.run(op);
    }

    /**
     * 从原图中裁剪出新图
     * @param path 原文件路径
     * @param des 目标文件路径
     * @param x 原图左上角
     * @param y 原图左上角
     * @param width 新图片宽度
     * @param height 新图片高度
     * @throws Exception
     */
    public static void cropImage(String path, String des, int x, int y, int width, int height) throws Exception {
        createDirectory(des);
        IMOperation op = new IMOperation();
        op.addImage(path);
        op.crop(width, height, x, y);
        op.addImage(des);
        ConvertCmd cmd = (ConvertCmd) getImageCommand("convert");
        cmd.run(op);
    }

    /**
     * 将图片分割为若干小图
     * @param path 原文件路径
     * @param des 目标文件路径
     * @param width 指定宽度(默认为完整宽度)
     * @param height 指定高度(默认为完整高度)
     * @return 小图路径
     * @throws Exception
     */
    public static List<String> subsectionImage(String path, String des, Integer width, Integer height) throws Exception {
        createDirectory(des);
        IMOperation op = new IMOperation();
        op.addImage(path);
        op.crop(width, height);
        op.addImage(des);

        ConvertCmd cmd = (ConvertCmd) getImageCommand("convert");
        cmd.run(op);

        return getSubImages(des);
    }

    /**
     * <pre>
     * <li>去除Exif信息</li>
     * <li>按指定的宽度等比缩放图片</li>
     * <li>降低图片品质</li>
     * <li>将图片分割分指定高度的小图</li>
     *
     * @param path 原文件路径
     * @param des 目标文件路径
     * @param width 指定宽度
     * @param subImageHeight 指定高度
     * @param quality 保留品质
     * @return 小图路径
     * @throws Exception
     * </pre>
     */
    public static List<String> resizeAndCropImage(String path, String des, int width, int subImageHeight, double quality) throws Exception {
        createDirectory(des);
        IMOperation op = new IMOperation();
        op.addImage(path);

//        op.profile("*");
        op.resize(width, null);
        op.quality(quality);
        op.crop(null, subImageHeight);

        op.addImage(des);
        ConvertCmd cmd = (ConvertCmd) getImageCommand("convert");
        cmd.run(op);

        return getSubImages(des);
    }

    /***
     * 切图
     * @param source path       源图片路径
     * @param target            目标图片路径
     * @param width             限宽
     * @param subImageHeight    最终高度
     * @param quality           图片质量(0~100)
     * @throws Exception
     */
    public static void cropImage(String source, String target, int width, int subImageHeight, double quality) throws Exception {
        createDirectory(target);
        IMOperation op = new IMOperation();
        op.addImage(source);
        op.resize(width, null);
        op.quality(quality);
        op.crop(null, subImageHeight, 0, 15);
        op.addImage(target);
        ConvertCmd cmd = (ConvertCmd) getImageCommand("convert");
        cmd.run(op);
    }
    /**
     * 拷贝图片 - 同步
     * @param source
     * @param target
     * @return
     * @throws Exception
     */
    public static boolean copyImage(String source, String target) throws Exception {
        createDirectory(target);
        IMOperation op = new IMOperation();
        op.addImage(source);
        op.addImage(target);
        ConvertCmd cmd = (ConvertCmd) getImageCommand("convert");
        cmd.setAsyncMode(false);
        cmd.run(op);
        return true;
    }
    /**
     * 拷贝图片 - 异步
     * @param source
     * @param target
     * @return
     * @throws Exception
     */
    public static void copyImageAsync(String source, String target) throws Exception {
        createDirectory(target);
        IMOperation op = new IMOperation();
        op.addImage(source);
        op.addImage(target);
        ConvertCmd cmd = (ConvertCmd) getImageCommand("convert");
        cmd.setAsyncMode(true);
        cmd.run(op);
    }

    /**
     * 创建目录
     * @param path
     */
    private static void createDirectory(String path) {
        File file = new File(path);
        if (file.exists()){
            return;
        }
        file.getParentFile().mkdirs();
    }

    /**
     * 获取图片分割后的小图路径
     * @param des 目录路径
     * @return 小图路径
     */
    private static List<String> getSubImages(String des) {
        String fileDir = des.substring(0, des.lastIndexOf(File.separatorChar)); // 文件所在目录
        String fileName = des.substring(des.lastIndexOf(File.separatorChar) + 1); // 文件名称
        String n1 = fileName.substring(0, fileName.lastIndexOf(".")); // 文件名(无后缀)
        String n2 = fileName.replace(n1, ""); // 后缀

        List<String> fileList = new ArrayList<String>();
        String path = null;
        for (int i = 0;; i++) {
            path = fileDir + File.separatorChar + n1 + "-" + i + n2;
            if (new File(path).exists()) fileList.add(path);
            else break;
        }
        return fileList;
    }

    /**
     * 获取GIF图片一帧图片 - 同步执行
     * @param src
     * @param target
     * @param frame
     * @throws Exception
     */
    public static boolean getGifOneFrame(String src, String target, int frame) throws Exception {
        if(!src.endsWith(".gif")){
            return false;
        }
        createDirectory(target);
        IMOperation op = new IMOperation();
        op.addImage(src + "["+frame+"]");
        op.addImage(target);
        ConvertCmd cmd = (ConvertCmd) getImageCommand("convert");
        cmd.setAsyncMode(false);
        cmd.run(op);
        return true;
    }
    /**
     * 获取GIF图片一帧图片 - 异步执行
     * @param src
     * @param target
     * @param frame
     * @throws Exception
     */
    public static void getGifOneFrameAsync(String src, String target, int frame) throws Exception {
        if(!src.endsWith(".gif")){
            return;
        }
        createDirectory(target);
        IMOperation op = new IMOperation();
        op.addImage(src + "["+frame+"]");
        op.addImage(target);
        ConvertCmd cmd = (ConvertCmd) getImageCommand("convert");
        cmd.setAsyncMode(true);
        cmd.run(op);
    }

    public static void main(String[] args) throws Exception {

        String src = "D:\\test\\0.emf";
        String target = "D:\\test\\0.jpg";
        copyImage(src, target);
        logger.info("处理完成");
    }
}
  <dependency>
		  <groupId>org.im4java</groupId>
		  <artifactId>im4java</artifactId>
		  <version>1.4.0</version>
	  </dependency>
  <dependency>
          <groupId>org.xhtmlrenderer</groupId>
          <artifactId>core-renderer</artifactId>
          <version>R8</version>
      </dependency>
	  <dependency>
		  <groupId>com.itextpdf.tool</groupId>
		  <artifactId>xmlworker</artifactId>
		  <version>5.5.9</version>
	  </dependency>

	  <dependency>
		  <groupId>com.itextpdf</groupId>
		  <artifactId>itext-asian</artifactId>
		  <version>5.2.0</version>
	  </dependency>
	  <!-- https://mvnrepository.com/artifact/com.lowagie/itext -->
	  <dependency>
		  <groupId>com.lowagie</groupId>
		  <artifactId>itext</artifactId>
		  <version>2.0.8</version>
		  <type>pom</type>
	  </dependency>


	  <!-- https://mvnrepository.com/artifact/com.itextpdf/itext-asian -->
	  <dependency>
		  <groupId>com.itextpdf</groupId>
		  <artifactId>itext-asian</artifactId>
		  <version>5.2.0</version>
	  </dependency>


	  <dependency>
		  <groupId>org.xhtmlrenderer</groupId>
		  <artifactId>flying-saucer-pdf-itext5</artifactId>
		  <version>9.0.3</version>
	  </dependency>

处理图片主要参考这个https://blog.csdn.net/chwshuang/article/details/64921665

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值