【问题标题】:Storing math equations from .doc MS Word file & restoring them从 .doc MS Word 文件存储数学方程式并恢复它们
【发布时间】:2012-08-15 09:48:50
【问题描述】:

我正在处理包含数学方程式和图像的 Microsoft Word 文件。 我需要获取这些 InlineShapes 对象并按原样存储它们,然后在需要时恢复它们。

我知道如何从文件中获取InlineShape 对象,问题是如何存储它们。不能将它们保存为图像!

【问题讨论】:

  • 您是否考虑过将它们存储在资源文件或数据库中?
  • 能否请您展示一些您目前正在使用的相关代码。这样你的问题就会更容易理解了。
  • 你能把内联形状对象存储在内存中吗?
  • @DJKRAZE ,如果可能的话,我想将它们存储在数据库中
  • Chingi3 我添加了一个不同的解决方案请看下面的例子

标签: c# math ms-word .doc


【解决方案1】:
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";
        }
    }

来自How To Exctract images from Doc (Word) file in C#?的代码副本

Converting Images from Word Doc to Bitmap

【讨论】:

  • 我认为不能将内联形状保存为图像?
  • @jJack 你说得对,这不是一个选项。@DJKRAZE 感谢代码,但我可以将内联形状存储为位图,我需要以某种方式将它们按原样存储。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-10
  • 1970-01-01
  • 2015-02-24
  • 1970-01-01
  • 2023-03-12
相关资源
最近更新 更多