消防做ccc去那个网站奶茶网络营销策划方案
大家好,我是阿赵。
阿赵我写博客的时候的习惯是,先用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方法替代一下,或者以后我再分享。
把文档拖到工具上:
工具会自动文档路径和保存路径
点击提取所有图片,就提取完成了:
在原来的文档旁边会新生成一个文件夹
里面就是提取完的图片: