使用aspose.word.for.java解析word文档图片并替换

/**
*需要import的包有如下
*import com.aspose.words.Document;
*import com.aspose.words.DocumentBuilder;
*import com.aspose.words.ImageData;
*import com.aspose.words.Node;
*import com.aspose.words.NodeCollection;
*import com.aspose.words.NodeType;
*import com.aspose.words.Shape;
*import com.aspose.words.ShapeType;
*
*aspose.words.for.java 版本 3.1.0
*
* 将word文档中wmf图片导出并替换为标签
*
* @param fileName
*            文档完整名
* @return 修改后的文件名
*/
public String exportWmfFromDoc(String fileName) {
        try {
            Document doc = new Document(filePath + fileName);// 新建文档对象
            NodeCollection shapeCollection = doc.getChildNodes(NodeType.SHAPE,
                    true);// 查询文档中所有wmf图片
            Node[] shapes = shapeCollection.toArray();// 序列化
            String imgPath = "";
            if (shapes.length > 0) {// 如果文档存在图片
                File file = new File(imagePath
                        + fileName.substring(0, fileName.lastIndexOf(".")));
                if (file != null) {
                    if (file.exists() || file.mkdir()) {// 创建文档图片保存文件夹
                        imgPath = file.getAbsolutePath() + "\\";
                    } else {
                        throw new Exception("文档图片保存路径不可写,请检查路径:\"" + imagePath
                                + "\"");
                    }
                }
                for (Node node : shapes) {
                    Shape shape = (Shape) node;
                    if (shape.getShapeType() == ShapeType.OLE_OBJECT) {// 如果shape类型是ole类型
                        ImageData i = shape.getImageData();// 获得图片数据
                        String imageName = imageName() + ".wmf";
                        i.save(imgPath + imageName);// 导出图片
                        File f = new File(imgPath + imageName);
                        if (f.exists()) {
                            imageName = wmfToPNG(f.getAbsolutePath());
                            log.debug("f.path--->" + f.getAbsolutePath());
                            Exec.saveMinPhoto(imageName, imageName, (double)38, (double)0);
                            if (f.canWrite()) {
                                f.delete();
                            }
                        } else {
                            log.error("图片不存在!");
                            continue;
                        }
                        // log.info("f.name--->" + f.getName());
                        DocumentBuilder builder = new DocumentBuilder(doc);// 新建文档节点
                        builder.moveTo(shape);// 移动到图片位置
                        builder.write("[img]"
                                + fileName.substring(0, fileName
                                        .lastIndexOf("."))
                                + "/"
                                + f.getName().substring(0,
                                        f.getName().lastIndexOf(".")) + ".png"
                                + "[/img]");// 插入替换文本
                        shape.remove();// 移除图形
                    } else if (shape.getShapeType() == ShapeType.IMAGE) {// 如果shape类型是ole类型
                        ImageData i = shape.getImageData();// 获得图片数据
                        String imageName = imageName() + ".png";
                        i.save(imgPath + imageName);// 导出图片
                        File f = new File(imgPath + imageName);
                        if (f.exists()) {
                            DocumentBuilder builder = new DocumentBuilder(doc);// 新建文档节点
                            builder.moveTo(shape);// 移动到图片位置
                            builder.write("[img]"
                                    + fileName.substring(0, fileName
                                            .lastIndexOf("."))
                                    + "/"
                                    + f.getName().substring(0,
                                            f.getName().lastIndexOf("."))
                                    + ".png" + "[/img]");// 插入替换文本
                            shape.remove();// 移除图形
                        } else {
                            log.error("图片不存在!");
                            continue;
                        }
                    }
                }
                String extName = fileName.substring(fileName.lastIndexOf("."));
                String mainName = fileName.substring(0, fileName
                        .lastIndexOf("."));
                doc.save(filePath + mainName + "_done" + extName);// 保存修改后的文档
                // log.info("filename---->" + mainName + "_done" + extName);
                return mainName + "_done" + extName;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
原帖:http://www.oschina.net/code/snippet_97818_8443
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值