Reputation: 744
Currently I am developing a project of online exam and I want to read a word file which contains images. I have a code to read word file line by line but I don't know how to read images from word file.
My Code:
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();
}
Upvotes: 3
Views: 411
Reputation: 661
referring to this link you can find this code that maybe help you to get the image from a word document
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();
/*...*/
}
}
Upvotes: 2