如何用图像读取Word文件

时间:2018-12-05 06:51:24

标签: c# .net

目前,我正在开发一个在线考试项目,我想读取一个包含图像的Word文件。我有一个代码可以逐行读取Word文件,但是我不知道如何从Word文件读取图像。

我的代码:

word = new Microsoft.Office.Interop.Word.Application();
object miss = System.Reflection.Missing.Value;
OpenFileDialog file = new OpenFileDialog();
if (file.ShowDialog() == DialogResult.OK) 
{
    object path = file.FileName; //get the path of the file  
    MessageBox.Show(path.ToString());
    object readOnly = true;
    Microsoft.Office.Interop.Word.Document docs = word.Documents.Open(ref path, 
        ref miss, 
        ref readOnly, 
        ref miss, 
        ref miss, 
        ref miss, 
        ref miss, 
        ref miss, 
        ref miss, 
        ref miss, 
        ref miss, 
        ref miss, 
        ref miss, 
        ref miss, 
        ref miss, 
        ref miss);
    string totaltext = "";
    for (int i = 0; i < docs.Paragraphs.Count; i++)
    {
        totaltext += " \r\n " + docs.Paragraphs[i + 1].Range.Text.ToString();
    }
    Console.WriteLine(totaltext);
    docs.Close();
    word.Quit();
}

1 个答案:

答案 0 :(得分:1)

参考此link,您可以找到此代码,也许可以帮助您获取 Word文档中的图片

using System;
using System.Drawing;
using System.IO;
using System.Threading;
using Microsoft.Office.Interop.Word;
using Microsoft.VisualBasic.Devices;
using Page=System.Web.UI.Page;

public partial class DefaultPage : Page {
    private Application m_word;
    private int m_i;

    protected void Page_Load(object sender, EventArgs e) {
        object missing = Type.Missing;
        object fileName = "C:\\Test.docx";
        m_word = new Application();
        m_word.Documents.Open(ref fileName,
                              ref missing, ref missing, ref missing, ref missing, ref missing,
                              ref missing, ref missing, ref missing, ref missing, ref missing,
                              ref missing, ref missing, ref missing, ref missing, ref missing);

        try {
            for (int i = 0; i < m_word.ActiveDocument.InlineShapes.Count; i++) {
                m_i = i;
                Thread thread = new Thread(CopyFromClipboardInlineShape);
                thread.SetApartmentState(ApartmentState.STA);
                thread.Start();
                thread.Join();
            }
            for (int i = 0; i < m_word.ActiveDocument.Shapes.Count; i++) {
                m_i = i;
                Thread thread = new Thread(CopyFromClipboardShape);
                thread.SetApartmentState(ApartmentState.STA);
                thread.Start();
                thread.Join();
            }
        }
        finally {
            object save = false;
            m_word.Quit(ref save, ref missing, ref missing);
            m_word = null;
        }
    }

    protected void CopyFromClipboardInlineShape() {
        InlineShape inlineShape = m_word.ActiveDocument.InlineShapes[m_i];
        inlineShape.Select();
        m_word.Selection.Copy();
        Computer computer = new Computer();
        Image img = computer.Clipboard.GetImage();
        /*...*/
    }

    protected void CopyFromClipboardShape() {
        object missing = Type.Missing;
        object i = m_i + 1;
        Shape shape = m_word.ActiveDocument.Shapes.get_Item(ref i);
        shape.Select(ref missing);
        m_word.Selection.Copy();
        Computer computer = new Computer();
        Image img = computer.Clipboard.GetImage();
        /*...*/
    }
}