2

I'm processing Microsoft Word files which contain math equations and images. I need to get these InlineShapes objects and store them as is & then restore them when needed.

I know how to get InlineShape objects from file, the problem is how can I store them. Saving them as images is not an option!

Petr Abdulin
  • 33,883
  • 9
  • 62
  • 96
chingi3
  • 91
  • 1
  • 10
  • have you thought about storing them in Resource file or in a Database.? – MethodMan Aug 20 '12 at 01:31
  • Could you please show some relevant code you are using so far. It will be easier to undersand your problem then. – Petr Abdulin Aug 20 '12 at 02:23
  • Can you store the inlineshape objects in memory? – JohnZaj Aug 20 '12 at 03:01
  • @DJKRAZE , I want to store them in database if it's possible – chingi3 Aug 20 '12 at 12:39
  • Chingi3 I added a different solution please see the example below – MethodMan Aug 20 '12 at 12:59
  • @DJKRAZE thanks again.look is there any other way to store inlineshape besides saving them as images?? – chingi3 Aug 20 '12 at 13:06
  • is this for a winforms or webforms application..? you can store and upload the file as imagebytes also do you have a database setup with the proper DataType..? varBinaryMax would work in regards to setting up the database type – MethodMan Aug 20 '12 at 13:18
  • anyway the result is image,i meant can i store the 'InlineShape' object as object with all its properties in order to restore it when i need. – chingi3 Aug 20 '12 at 15:51

1 Answers1

0
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";
        }
    }

Code copy from How To Exctract images from Doc (Word) file in C#?

Converting Images from Word Doc to Bitmap

Community
  • 1
  • 1
MethodMan
  • 18,625
  • 6
  • 34
  • 52
  • I thought saving the inline shapes as images was not an option? – JohnZaj Aug 20 '12 at 02:59
  • @jJack you right it's not an option.@DJKRAZE thanks for code but i can handle storing inlineshapes as bitmap,and I need somehow store them as is. – chingi3 Aug 20 '12 at 12:41