简体   繁体   English

如何在C#中将文本从RTF插入Word文档

[英]How to insert text from RTF to Word Document in c#

I am using winservice for creating Word docs. 我正在使用winservice创建Word文档。
The only problem I have is to Paste rtf to word Selection. 我唯一的问题是将rtf粘贴到单词选择中。

I have this code: 我有以下代码:

private static void PasteRtf(object bookmarkName, 
                             OFFICE.Application wordApplication, 
                             Document wordDocument, string rtfText, bool winservice)
    {
        if(bookmarkName == null ||
           wordApplication == null ||
           wordDocument == null) return;

        if (!winservice)
        {
            Clipboard.Clear();
            Clipboard.SetText(rtfText, TextDataFormat.Rtf);
            IDataObject formatedText = Clipboard.GetDataObject();
            if (formatedText != null)
            {
                wordDocument.Bookmarks[bookmarkName].Range.Select();
                Selection sel = wordApplication.Selection;
                sel.Paste();
            }
            Clipboard.Clear();
        }
        else
        {
            ????

        }
    }

Do you have any idea how to do that without using Clipboard? 您有没有在不使用剪贴板的情况下怎么做的想法?

Solution: 解:

wordDocument.Bookmarks[bookmarkName].Range.Select();
Selection sel = wordApplication.Selection;

wf.RichTextBox tb = new wf.RichTextBox();
tb.Rtf = rtfText;

string fileName = Path.Combine(UserInfo.User.TempPath,
                                                   Guid.NewGuid() + ".rtf");

tb.SaveFile(fileName, wf.RichTextBoxStreamType.RichText);

object ConfirmConversions = false;
object Link = false;
object Attachment = false;
object lMissing = System.Reflection.Missing.Value;
sel.InsertFile(fileName, ref lMissing, ref ConfirmConversions, ref Link, ref Attachment);

File.Delete(fileName);

If your RTF text (or Word, or any other compatible format) is located in a file, you may use Range.InsertFile or Bookmark.InsertFile method: 如果您的RTF文本(或Word或任何其他兼容格式)位于文件中,则可以使用Range.InsertFile或Bookmark.InsertFile方法:

string FileName = "C:\\Sales.docx";
object ConfirmConversions = false;
object Link = false;
object Attachment = false;
bookmark1.InsertFile(FileName, ref missing, ref ConfirmConversions, ref Link, ref Attachment);

Range.InsertFile Method (Word) Range.InsertFile方法(Word)
Bookmark.InsertFile Method Bookmark.InsertFile方法

Word requires a converter in order to integrate RTF into a document. Word需要一个转换器才能将RTF集成到文档中。 Converters run only when 转换器仅在以下情况下运行

  1. Content is pasted from the Clipboard (which you say you don't want) 内容是从剪贴板粘贴的(您说不需要)
  2. A file is opened or inserted (which SashaDu suggested and you also don't want) 文件被打开或插入(SashaDu建议,您也不想这样做)

There is no option for "streaming" RTF (or HTML, for that matter) into a Word document. 没有将“ RTF”(或HTML)“流式传输”到Word文档中的选项。

The only other possibility is for you to first convert the RTF to valid WordOpenXML in the OOPC flat-file format. 唯一的其他可能性是您首先将RTF转换为OOPC平面文件格式的有效WordOpenXML。 That can be inserted into the document using Word's Range.InsertXml method. 可以使用Word的Range.InsertXml方法将其插入文档。 There are probably third-party tools out there that could do this for you. 可能有第三方工具可以为您完成此任务。 Or you'd need to write your own RTF->WordOpenXML converter. 或者,您需要编写自己的RTF-> WordOpenXML转换器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM