.NET web C#通过剪切板取出word中图片

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/MajorMayer/article/details/50435233

.NET web C#通过剪切板取出word中图片

前台代码:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="bt_readreport" runat="server" Text="Button" OnClick="bt_readreport_Click"/>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </div>
    </form>
</body>
</html>

添加引用

后台实现代码:

protected void bt_readreport_Click(object sender, EventArgs e)
        {
            //初始化控件值
            //ClearControl();
            StringBuilder reportContent = new StringBuilder();

            object Nothing = System.Reflection.Missing.Value;
            object filename = "C:/Users/周杨/Desktop/牛倌儿网合伙投资理财三方借款合同.docx";
            string savePath = "C:/Users/周杨/Desktop/";
            Application WordApp = new ApplicationClass();
            Document WordDoc = WordApp.Documents.Open(ref filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
            //循环文章中的各个章节
            foreach (Paragraph item in WordDoc.Paragraphs)
            {
                if (item != null)
                {
                    if (item.Range.Text.Trim() != "")
                    {
                        //判断该范围内是否存在图片
                        if (item.Range.InlineShapes.Count != 0)
                        {
                            foreach (InlineShape shape in item.Range.InlineShapes)
                            {
                                //判断类型
                                if (shape.Type == WdInlineShapeType.wdInlineShapePicture)
                                {
                                    //利用剪贴板保存数据
                                    shape.Select(); //选定当前图片
                                    WordApp.Selection.Copy();//copy当前图片
                                    string fileName = "";
                                    if (Clipboard.ContainsImage())
                                    {
                                        Bitmap bmp = new Bitmap(Clipboard.GetImage());
                                        fileName = Guid.NewGuid() + ".png";
                                        bmp.Save(savePath + fileName, System.Drawing.Imaging.ImageFormat.Png);
                                    }
                                }
                            }
                        }
                        //在总目录中添加相应信息
                        reportContent.AppendLine(item.Range.Text.Trim());
                    }
                }
            }
            WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
            WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
        }

后台代码using部分代码:

using System;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;
using Application = Microsoft.Office.Interop.Word.Application;

部分office版本会因为智能提示清空剪切板类容导致无法取出。
阅读更多

扫码向博主提问

MajorMayer

比如一些前端问题
去开通我的Chat快问
换一批

没有更多推荐了,返回首页