存储从.DOC MS Word文件,与功放的数学方程;恢复它们 [英] Storing math equations from .doc MS Word file & restoring them
本文介绍了存储从.DOC MS Word文件,与功放的数学方程;恢复它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在处理含有数学公式和图像的Microsoft Word文件。
我需要得到这些 InlineShapes
对象,并将其储存的按原样&安培;需要的时候再恢复它们。
我知道如何让 InlineShape
从文件对象,问题是我怎么能存储它们。保存为图片是不是一种选择
解决方案
使用系统!;
使用System.Drawing中;
:使用System.IO;
使用的System.Threading;使用页面= System.Web.UI.Page
;
使用的Microsoft.Office.Interop.Word;使用Microsoft.VisualBasic.Devices
;
公共部分类ReadIMG:System.Web.UI.Page
{
私人应用m_word;
私人诠释m_i;
保护无效的Page_Load(对象发件人,EventArgs五)
{
对象缺少= Type.Missing;
目标文件名=使用Server.Mappath(〜/ LectureOrig / Word.docx);
对象readOnly的= TRUE;
m_word =新的应用程序();
m_word.Documents.Open(REF文件名,
裁判失踪,裁判只读,已参考失踪,失踪参考,
参考失踪,失踪参考,参考失踪,失踪参考,
REF失踪,失踪参考,参考失踪,失踪参考,
参考失踪,失踪参考,参考失踪);
试
{
的for(int i = 1; I< = m_word.ActiveDocument.InlineShapes.Count;我++)
{
m_i = I;
// CopyFromClipboardShape();
线程线程=新主题(CopyFromClipbordInlineShape);
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
的Thread.join();
}
}
终于
{
对象保存= FALSE;
m_word.Quit(编号保存,裁判失踪,失踪参考);
m_word = NULL;
}
}
保护无效CopyFromClipbordInlineShape()
{
InlineShape inlineShape = m_word.ActiveDocument.InlineShapes [m_i]
inlineShape.Select();
m_word.Selection.Copy();
电脑电脑=新计算机();
//图片IMG = computer.Clipboard.GetImage();
如果(!computer.Clipboard.GetDataObject()= NULL)
{
System.Windows.Forms.IDataObject数据= computer.Clipboard.GetDataObject();
如果(data.GetDataPresent(System.Windows.Forms.DataFormats.Bitmap))
{
图像的图像=(图)data.GetData(System.Windows.Forms.DataFormats.Bitmap,真正);
image.Save(使用Server.Mappath(〜/ ImagesGet / Image.gif的),System.Drawing.Imaging.ImageFormat.Gif);
image.Save(使用Server.Mappath(〜/ ImagesGet / image.jpg文件),System.Drawing.Imaging.ImageFormat.Jpeg);
}
,否则
{
LabelMessage.Text =数据在剪贴板并不像图片格式;
}
}
,否则
{
LabelMessage.Text =剪贴板是空的;
}
}
代码从的如何从文件Exctract图像(字)在C#中的文件吗?
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!
解决方案
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
这篇关于存储从.DOC MS Word文件,与功放的数学方程;恢复它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文