![](/img/trans.png)
[英]C# Retrieve FormFields from Word Document and Insert into Text file
[英]How to insert text from RTF to Word Document in c#
我正在使用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
{
????
}
}
您有沒有在不使用剪貼板的情況下怎么做的想法?
解:
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);
如果您的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);
Word需要一個轉換器才能將RTF集成到文檔中。 轉換器僅在以下情況下運行
沒有將“ RTF”(或HTML)“流式傳輸”到Word文檔中的選項。
唯一的其他可能性是您首先將RTF轉換為OOPC平面文件格式的有效WordOpenXML。 可以使用Word的Range.InsertXml方法將其插入文檔。 可能有第三方工具可以為您完成此任務。 或者,您需要編寫自己的RTF-> WordOpenXML轉換器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.