提取word文档里面的图片

  大家好,我是阿赵。
  阿赵我写博客的时候的习惯是,先用word文档写好,然后再把word文档里面的图片另存,最后再在博客里面复制正文和上传图片。
  而我写的文章一般配图都比较多,所以经常要做的一个功能就是另存图片。由于我没有买正版的Office工具,我用的是WPS工具来编辑word文档的。wps虽然是免费的,但它保存文档里面的所有图片是需要会员收费的,不然就只能一张一张图片手动保存。
  然而作为程序员,在觉得不该花钱的地方,我也是不会乱花的。我自己写了一个小程序,把文档里面的所有图片提取并保存,这里分享一下,这是一个用C#写的winform程序:
在这里插入图片描述

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System;
using System.Windows.Forms;
using Tools;

namespace PickWordTexture
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private string wordPath = "";
        private string savePath = "";
        private void ShowTips(string content)
        {
            MessageBox.Show(content);
        }

        private bool ShowTipsSelect(string content)
        {
            DialogResult result = MessageBox.Show(content, "提示", MessageBoxButtons.OKCancel);
            if(result == DialogResult.OK)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private void SetWordPath(string path)
        {
            wordPath = path;
            string fullFileName = FilePathHelper.GetFileName(path);
            string fileName = FilePathHelper.RemoveExName(fullFileName);
            savePath = path.Replace(fullFileName,"")+fileName+"\\";
            UpdateView();
        }


        private void UpdateView()
        {
            wordPathTxt.Text = wordPath;
            savePathTxt.Text = savePath;
        }

        private void PickTextureFun()
        {
            if(string.IsNullOrEmpty(wordPath)||string.IsNullOrEmpty(savePath))
            {
                ShowTips("请先把需要提取的word文档拖动到窗口内");
                return;
            }
            if(FileManager.IsDirectoryExists(savePath))
            {
                if(ShowTipsSelect("保存的文件夹已经存在,将会覆盖,原有内容将会被删除,是否继续?")==true)
                {
                    FileManager.DelFolder(savePath);
                }
                else
                {
                    return;
                }
            }
            Document document;
            string exName = FilePathHelper.GetExName(wordPath).ToLower();
            if(exName == "doc")
            {
                document = new Document(wordPath, FileFormat.Doc);
            }
            else
            {
                document = new Document(wordPath, FileFormat.Docx);
            }

            int count = 0;
            foreach(Section section in document.Sections)
            {
                foreach(Paragraph paragraph in section.Paragraphs)
                {
                    foreach(DocumentObject docObject in paragraph.ChildObjects)
                    {
                        if(docObject.DocumentObjectType == DocumentObjectType.Picture)
                        {
                            DocPicture picture = docObject as DocPicture;
                            string imgName = savePath + "Image_" + (count + 1) + ".png";
                            FileManager.CheckFileSavePath(imgName);
                            picture.Image.Save(imgName, System.Drawing.Imaging.ImageFormat.Png);
                            count++;
                        }
                    }
                }
            }
            if(count == 0)
            {
                ShowTips("文档里面没有图片");
            }
            else
            {
                ShowTips("提取到图片:" + count + "张");
            }
        }


        private void Form1_DragDrop(object sender, DragEventArgs e)
        {
            string inputPath = ((Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
            string exName = FilePathHelper.GetExName(inputPath).ToLower();
            if(exName!="doc"&&exName!="docx")
            {
                ShowTips("只能拖动word文档(扩展名是doc或者docx)");
            }
            else
            {
                SetWordPath(inputPath);
            }
        }

        private void Form1_DragEnter(object sender, DragEventArgs e)
        {

            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Link;
            }
            else
            {
                e.Effect = DragDropEffects.None;

            }
        }

        private void pickTextureBtn_Click(object sender, EventArgs e)
        {
            PickTextureFun();
        }
    }
}

  里面主要用到了Spire.Doc。然后还有一些我自己写的保存文件的工具类。这些工具类你们可以自己写IO方法替代一下,或者以后我再分享。
  把文档拖到工具上:
在这里插入图片描述

  工具会自动文档路径和保存路径
在这里插入图片描述

  点击提取所有图片,就提取完成了:
在这里插入图片描述

  在原来的文档旁边会新生成一个文件夹
在这里插入图片描述

  里面就是提取完的图片:
在这里插入图片描述
如果要在提取了图片之后,在原来的图片位置替换成图片序号,比如第一张图片替换成[image1],再把字符串保存成txt,可以改成这样:

private void PickTextureFun()
        {
            if(string.IsNullOrEmpty(wordPath)||string.IsNullOrEmpty(savePath))
            {
                ShowTips("请先把需要提取的word文档拖动到窗口内");
                return;
            }
            if(FileManager.IsDirectoryExists(savePath))
            {
                if(ShowTipsSelect("保存的文件夹已经存在,将会覆盖,原有内容将会被删除,是否继续?")==true)
                {
                    FileManager.DelFolder(savePath);
                }
                else
                {
                    return;
                }
            }
            int picIndex = 1;
            Document document;
            string exName = FilePathHelper.GetExName(wordPath).ToLower();
            if(exName == "doc")
            {
                document = new Document(wordPath, FileFormat.Doc);
            }
            else
            {
                document = new Document(wordPath, FileFormat.Docx);
            }
            List<DocumentObject> pictures = new List<DocumentObject>();
            int count = 0;
            foreach(Section section in document.Sections)
            {
                foreach(Paragraph paragraph in section.Paragraphs)
                {
                    foreach(DocumentObject docObject in paragraph.ChildObjects)
                    {
                        if(docObject.DocumentObjectType == DocumentObjectType.Picture)
                        {
                            pictures.Add(docObject);
                            DocPicture picture = docObject as DocPicture;
                            string imgName = savePath + "Image_" + (count + 1) + ".png";
                            FileManager.CheckFileSavePath(imgName);
                            picture.Image.Save(imgName, System.Drawing.Imaging.ImageFormat.Png);
                            count++;
                        }
                    }
                    for(int i = 0;i<pictures.Count;i++)
                    {
                        int index = paragraph.ChildObjects.IndexOf(pictures[i]);
                        if(index>=0)
                        {
                            TextRange range = new TextRange(document);
                            range.Text = string.Format("[image{0}]", picIndex);
                            paragraph.ChildObjects.Insert(index, range);
                            picIndex++;
                        }

                    }
                    for (int i = 0; i < pictures.Count; i++)
                    {
                        paragraph.ChildObjects.Remove(pictures[i]);
                    }
                }
            }

            string content = document.GetText();
            string contentPath = savePath + "content.txt";
            FileManager.SaveFile(contentPath, content, true);
            if(count == 0)
            {
                ShowTips("文档里面没有图片");
            }
            else
            {
                ShowTips("提取到图片:" + count + "张");
            }
        }
  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值