This code work in asp.net and winforms, but I test it on Windows XP Pro, I don't know it work on Windows Server
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;
I will use in ASP.net and Windows Vista and Windows Server. I have tested that code already, it can take the images into Clipboard but could't get it from Clipboard. I think that the Clipboard is not good solution. Please give me suggestion, Thanks.
Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).
Hean Samboeu...
Member
2 Points
15 Posts
How To Exctract images from Doc (Word) file in C#?
Sep 25, 2009 07:17 AM|LINK
HI, I have a word file (docx) I want to read whole file then extract all images in it to another folder.
Please tell me how can I do and point me the simple code of C#.
Thanks in advanced,
Sam
DavidKiff
Star
8660 Points
1733 Posts
Re: How To Exctract images from Doc (Word) file in C#?
Sep 28, 2009 06:30 PM|LINK
You might be able to do this with OpenXML (http://en.wikipedia.org/wiki/Office_Open_XML). I dont have any sample code though :)
This is microsofts main resource site: http://openxmldeveloper.org/default.aspx
Visit my site
Follow me on Twitter
Hean Samboeu...
Member
2 Points
15 Posts
Re: How To Exctract images from Doc (Word) file in C#?
Sep 29, 2009 05:30 AM|LINK
Thanks for useful recommendation, it's a source for my researches, I'm going to check it now.
Chibitko
Member
84 Points
22 Posts
Re: How To Exctract images from Doc (Word) file in C#?
Sep 29, 2009 08:41 AM|LINK
You need it in Asp.net or WinForms?
Chibitko
Member
84 Points
22 Posts
Re: How To Exctract images from Doc (Word) file in C#?
Sep 29, 2009 09:02 AM|LINK
Use Microsoft.Office.Interop
This code work in asp.net and winforms, but I test it on Windows XP Pro, I don't know it work on Windows Server
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();
/*...*/
}
}
Hean Samboeu...
Member
2 Points
15 Posts
Re: How To Exctract images from Doc (Word) file in C#?
Sep 30, 2009 11:14 AM|LINK
I will use in ASP.net and Windows Vista and Windows Server. I have tested that code already, it can take the images into Clipboard but could't get it from Clipboard. I think that the Clipboard is not good solution. Please give me suggestion, Thanks.
Chibitko
Member
84 Points
22 Posts
Re: How To Exctract images from Doc (Word) file in C#?
Sep 30, 2009 11:35 AM|LINK
I don't know another solution
Try this
[STAThread] protected void CopyFromClipboardInlineShape() { ...Hean Samboeu...
Member
2 Points
15 Posts
Re: How To Exctract images from Doc (Word) file in C#?
Sep 30, 2009 03:59 PM|LINK
Dear Chibitko and All,
And thank Chibitko for ur kind.
Now it's working well about extracting the images from docx like code below:
using System;
using System.Drawing;
using System.IO;
using System.Threading;
using Page = System.Web.UI.Page;
using Microsoft.Office.Interop.Word;
using Microsoft.VisualBasic.Devices;
public partial class ReadIMG : System.Web.UI.Page
{
private Application m_word;
private int m_i;
protected void Page_Load(object sender, EventArgs e)
{
object missing = Type.Missing;
object FileName = Server.MapPath("~/LectureOrig/Word.docx");
object readOnly = true;
m_word = new Application();
m_word.Documents.Open(ref FileName,
ref missing, ref readOnly, 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 = 1; i <= m_word.ActiveDocument.InlineShapes.Count; i++)
{
m_i = i;
// CopyFromClipboardShape();
Thread thread = new Thread(CopyFromClipbordInlineShape);
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 CopyFromClipbordInlineShape()
{
InlineShape inlineShape = m_word.ActiveDocument.InlineShapes[m_i];
inlineShape.Select();
m_word.Selection.Copy();
Computer computer = new Computer();
//Image img = computer.Clipboard.GetImage();
if (computer.Clipboard.GetDataObject() != null)
{
System.Windows.Forms.IDataObject data = computer.Clipboard.GetDataObject();
if (data.GetDataPresent(System.Windows.Forms.DataFormats.Bitmap))
{
Image image = (Image)data.GetData(System.Windows.Forms.DataFormats.Bitmap, true);
image.Save(Server.MapPath("~/ImagesGet/image.gif"), System.Drawing.Imaging.ImageFormat.Gif);
image.Save(Server.MapPath("~/ImagesGet/image.jpg"), System.Drawing.Imaging.ImageFormat.Jpeg);
}
else
{
LabelMessage.Text="The Data In Clipboard is not as image format";
}
}
else
{
LabelMessage.Text="The Clipboard was empty";
}
}
togius
Member
35 Points
57 Posts
Re: How To Exctract images from Doc (Word) file in C#?
Dec 01, 2010 11:46 AM|LINK
i am getting error like this.
Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).
togius
Member
35 Points
57 Posts
Re: How To Exctract images from Doc (Word) file in C#?
Dec 01, 2010 12:29 PM|LINK
we need to make sure that both Microsoft Office and Visual Studio Tools For Office (VSTO) are installed on the server.