gpt4 book ai didi

c# - 如何读取带图片的word文件

转载 作者:行者123 更新时间:2023-11-30 16:39:06 25 4
gpt4 key购买 nike

目前我正在开发一个在线考试项目,我想阅读一个包含图像的word文件。我有一个逐行读取 word 文件的代码,但我不知道如何从 word 文件中读取图像。

我的代码:

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();
}

最佳答案

引用这个 link 你可以找到这段代码,它可能会帮助你获得图片来自word文档

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();
/*...*/
}
}

关于c# - 如何读取带图片的word文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53626698/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com