如何在富文本框中或带有文本,图像和项目符号的文本框中打开doc/docx文件 [英] how to open doc/docx file in rich text box or text box with texts,images and bullets

查看:176
本文介绍了如何在富文本框中或带有文本,图像和项目符号的文本框中打开doc/docx文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在带有图像,项目符号和文本的富文本框中打开Word文件
然后我要逐行句子.
我正在使用此代码-

Hi, i want to open word file in a rich text box with images,bullets and texts
and then i want line by line sentences.
I am using this code-

Microsoft.Office.Interop.Word.ApplicationClass wordObject = new Microsoft.Office.Interop.Word.ApplicationClass(); 
object File = txtfilepath.Text; //this is the path
 object nullobject = System.Reflection.Missing.Value; Microsoft.Office.Interop.Word.Application wordobject = new Microsoft.Office.Interop.Word.Application();
 wordobject.DisplayAlerts = Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsNone; Microsoft.Office.Interop.Word._Document docs = wordObject.Documents.Open(ref File, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject); docs.ActiveWindow.Selection.WholeStory();
 docs.ActiveWindow.Selection.Copy(); 
IDataObject data = Clipboard.GetDataObject(); 
rtbgermanfile.Text = data.GetData(DataFormats.Rtf).ToString();
 string name = rtbgermanfile.Text.Trim(); name = String.Join(Environment.NewLine, name.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries)); 
string newtext = name.Replace("\n\n", "\n");
 string newtext2 = newtext.Replace("\n\n", "\n");
 rtbgermanfile.Text = newtext2.ToString(); 
txtwriteingerman.Text = rtbgermanfile.Lines[0]; 
docs.Close(ref nullobject, ref nullobject, ref nullobject);
 wordobject.Quit(ref nullobject, ref nullobject, ref nullobject);
 //wordobject.Quit(SaveChanges:false, OriginalFormat: false, RouteDocument: false); 
System.Runtime.InteropServices.Marshal.ReleaseComObject(wordobject); txtwriteinenglish.Focus();


它工作正常,但无法打开图像.
迫切
请帮帮我.

在此先感谢.


it works fine but not able to open images.
its urgent
please help me out.

Thanks in Advance.

推荐答案

RichTextBox仅支持RTF格式,并且完全仅支持RTF格式.有一种使用剪贴板将图片插入其中的肮脏方法:

例如:
The class RichTextBox supports only RTF format, and pretty purely. There is a dirty way to insert a picture in it using clipboard:

For example:
Image img = Image.FromFile("myImage.jpg");
Clipboard.SetImage(img);
myRichTextBox.SelectionStart = 0; //for example; you need some position to insert it
myRichTextBox.SelectionLength = 0;
myRichTextBox.Paste();

但是,您可能会面对太多的问题.

我建议您使用其他替代方法.您可以使用HTML代替RTF格式.我会推荐此CodeProject文章中提供的精彩组件:

您将使用的专业HTML渲染器 [ ^ ].

我在上面引用的作品非常出色!为什么您需要RTF这样的大脑损伤,因为您可以使用非常丰富的HTML?这项工作的作者开发了一种奇妙的东西,它支持特殊的URL模式,以包含来自可执行文件中嵌入的资源的图像和其他资源.这是在不使用WebBrowser控件的情况下呈现结构化只读文档的一种几乎完美的方法.

另一种选择:使用WPF.它对Rich文档和XPS具有出色的支持.请在XPS查看器上查看我最近的回答:
Intersoft ClientUI中的XPS文档查看器示例 [ ^ ].

—SA

However, you can face too many problems with that.

I would advice you to use some alternative. Instead of Rich Text Format, you could use HTML. I would recommend this wonderful component offered in this CodeProject article:

A Professional HTML Renderer You Will Use[^].

The work I referenced above is excellent! Why do you need such brain damage as RTF is you can use very rich HTML? The author of this work developed such a wonderful thing as a support for special URL schema to include images and other resources from the resource embedded in the executable. This is a nearly perfect method of presentation of structured read-only documents without using of the WebBrowser control.

One more alternative: use WPF. It has excellent support for Rich documents and XPS. Please see my recent answer on XPS viewer:
The XPS Document Viewer Sample In Intersoft ClientUI[^].

—SA


这篇关于如何在富文本框中或带有文本,图像和项目符号的文本框中打开doc/docx文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆