【问题标题】:How to insert text from RTF to Word Document in c#如何在 C# 中将文本从 RTF 插入 Word 文档
【发布时间】:2016-11-09 23:54:59
【问题描述】:

我正在使用 winservice 创建 Word 文档。
我唯一的问题是将 rtf 粘贴到单词选择中。

我有这个代码:

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
        {
            ????

        }
    }

你知道如何在不使用剪贴板的情况下做到这一点吗?

【问题讨论】:

    标签: c# ms-word rtf


    【解决方案1】:

    解决办法:

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

    【讨论】:

      【解决方案2】:

      如果您的 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)
      Bookmark.InsertFile Method

      【讨论】:

      • 不,我在字符串中有 rtf。我将它保存在 NCLOB 列的数据库中。然后我需要插入到文档中...
      • 所以,最后它似乎工作正常。我这样做了: - 使用 winforms 的 RichTextBox (tb) 控件,并将 rtfstring 设置为它的属性 tb.Rtf。 - 保存 rtf 文件:tb.SaveFile(fileName, wf.RichTextBoxStreamType.RichText); ... 文件的扩展名 = .rtf - 供您的建议使用 selection.InsertFile
      【解决方案3】:

      Word 需要转换器才能将 RTF 集成到文档中。转换器仅在以下情况下运行

      1. 内容是从剪贴板粘贴的(你说你不想要的)
      2. 打开或插入了一个文件(SashaDu 建议但您也不希望这样做)

      没有将 RTF(或 HTML,就此而言)“流式传输”到 Word 文档的选项。

      唯一的另一种可能性是您首先将 RTF 转换为 OOPC 平面文件格式的有效 WordOpenXML。可以使用 Word 的 Range.InsertXml 方法将其插入到文档中。可能有第三方工具可以为您做到这一点。或者您需要编写自己的 RTF->WordOpenXML 转换器。

      【讨论】:

        猜你喜欢
        • 2011-12-30
        • 2010-11-12
        • 2011-07-11
        • 1970-01-01
        • 1970-01-01
        • 2010-12-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多