C# Word Interop,将剪贴板粘贴到段落中

发布于 2024-12-05 16:20:08 字数 1477 浏览 1 评论 0原文

我正在制作一个报告生成器,它可以在 Word 中构建报告的开头。然后 Word 保持打开状态,供用户继续处理报告。报告生成器从数据库获取 rtf,并将其用作段落。对于每个 rtf,都会在文档中创建一个段落,并将 rtf 添加到其中。

我读到的内容似乎暗示,如果我想将 rtf 插入到 Word 文档中,那么我将其放入剪贴板中,然后将其从剪贴板粘贴到 Word 中。这工作得很好,只是这实际上并没有按照我想要的方式将其放入段落中。当调用粘贴方法时,它实际上并不将 rtf 放置在段落范围中,它只是将其粘贴到段落开始的位置。它不会覆盖该段落,该段落仍然存在于我粘贴的 rtf 块下方。将范围移动一位以容纳段落标记不起作用,它只是将其粘贴到段落下方。我认为它需要使用的是 Insert() 方法而不是 Paste() 方法,但是我不知道如何进行此操作,也找不到任何如何进行操作的信息。我可以从构建块插入或插入纯文本,但是这里插入的内容需要是格式化文本。格式化文本具有不同用户制作的样式等的混合。

复制到剪贴板并粘贴的代码如下:

Clipboard.SetText(richTextBox1.Rtf, TextDataFormat.Rtf);
oPara[i].Range.Paste();

我知道您可以将构建块插入段落中,从而保持段落格式,具体如下:

tTemplate.BuildingBlockEntries.Item(foundList[i]).Insert(oPara[i].Range.FormattedText);

但我找不到在我的场景中如何实现这一点。

我想将其插入段落的原因是编辑格式的某些方面,并确保不要跨页换行等。

我当前用于创建和插入的代码是这样的:

Word.Paragraph[] oPara = new Word.Paragraph[foundList.Count];

for (int i = 0; i < foundList.Count; i++)
{
    oPara[i] = oDoc.Content.Paragraphs.Add();
    Clipboard.SetText(foundList[i].Paragraph, TextDataFormat.Rtf);
    oPara[i].Range.InsertParagraphAfter();
    oPara[i].Range.Paste();
    oPara[i].KeepTogether = -1;
    oPara[i].Range.Font.Size = 10;
    oPara[i].Range.Font.Name = "Arial";
}

我检查了使用

oPara[0].Range.Select(); //To see first paragraph

“The 结果”将 rtf 粘贴到段落开始的位置,并且该段落位于粘贴的 rtf 下方。 你会如何按照我想要的方式将 rtf 插入到 MS-Word 的段落中?

编辑:调用折叠方法并没有达到我想要的效果

I have a report builder I'm making that builds the start of a report in word. Word is then kept open for the user to carry on with the report. The report builder gets rtf from a database that is to be used as paragraphs. For each rtf a paragraph is created within the document and the rtf added to it.

What I've read seems to imply that if I want to insert rtf into a word document then I place it in the clipboard then paste it from the clipboard into word. This works fine except this doesn't actually put it into the paragraph how I want it. When the paste method is called it doesn't actually place the rtf in the paragraph range, it just pastes it where the paragraph starts. It doesn't overwrite the paragraph, the paragraph still exists below the block of rtf I pasted is. Moving the range by one to accomodate the paragraph marks doesn't work it just pastes it below the paragraph instead. What I think it needs to use is an Insert() method somehow rather than a Paste() method however I don't know how to go about this and I can't find any information how to go about it. I can insert from building blocks or inserting plain text fine, but what is being inserted here needs to be formatted text. The formatted text has a mix of styles etc that a different user makes.

The code for copying to the clipboard and pasting is as follows:

Clipboard.SetText(richTextBox1.Rtf, TextDataFormat.Rtf);
oPara[i].Range.Paste();

I know you can insert building blocks into paragraphs, which keeps the paragraph formatting, with the following:

tTemplate.BuildingBlockEntries.Item(foundList[i]).Insert(oPara[i].Range.FormattedText);

but I can't find how you would achieve this in my scenario.

The reason I'd like to insert it into a paragraph is to edit certain aspects of the format and ensure things such as don't break lines across pages etc.

The code I have currently for the creating and inserting is this:

Word.Paragraph[] oPara = new Word.Paragraph[foundList.Count];

for (int i = 0; i < foundList.Count; i++)
{
    oPara[i] = oDoc.Content.Paragraphs.Add();
    Clipboard.SetText(foundList[i].Paragraph, TextDataFormat.Rtf);
    oPara[i].Range.InsertParagraphAfter();
    oPara[i].Range.Paste();
    oPara[i].KeepTogether = -1;
    oPara[i].Range.Font.Size = 10;
    oPara[i].Range.Font.Name = "Arial";
}

I checked to see where a paragraph was exactly visually with the line

oPara[0].Range.Select(); //To see first paragraph

The result has the rtf pasted where the paragraph begins and the paragraph is just below the rtf pasted.
How would you go about inserting rtf the way I want to into a paragraph in MS-Word?

EDIT: calling the collapse method doesn't do what I want to happen

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

毁我热情 2024-12-12 16:20:08

找到解决方案,粘贴方法实际上粘贴在段落标签内,问题是当文本输入到富文本框中时,富文本框会自动添加段落标签,无论是否有任何开始。由于 rtf 有段落标签,因此将其视为不同的段落。因此,数据库中的 rtf 具有由富文本框而不是用户添加的段落标签。为了解决这个问题,我刚刚从数据库中获取的 rtf 中删除了段落标签。这是我最终得到的代码:

for (int i = 0; i < foundList.Count; i++)
{
    oPara[i] = oDoc.Content.Paragraphs.Add();
    string tempS = foundList[i].Paragraph;
    tempS = tempS.Replace("\\pard", "");
    tempS = tempS.Replace("\\par", "");
    Clipboard.SetText(tempS, TextDataFormat.Rtf);
    oPara[i].Range.InsertParagraphAfter();
    oPara[i].Range.Paste();
    oPara[i].KeepTogether = -1;
    oPara[i].Range.Font.Size = 10;
    oPara[i].Range.Font.Name = "Arial";
}

Found the solution, the paste method does actually paste inside the paragraph tags, the problem is that when text is entered into a rich text box, the rich text box automatically adds paragraph tags whether there was any to begin with or not. As the rtf had paragraph tags it counted it as a different paragraph. So the rtf from the database had paragraph tags that were added by a rich text box and not the user. To fix this I just removed the paragraph tags from the rtf taken from the database. Heres the code I ended up with:

for (int i = 0; i < foundList.Count; i++)
{
    oPara[i] = oDoc.Content.Paragraphs.Add();
    string tempS = foundList[i].Paragraph;
    tempS = tempS.Replace("\\pard", "");
    tempS = tempS.Replace("\\par", "");
    Clipboard.SetText(tempS, TextDataFormat.Rtf);
    oPara[i].Range.InsertParagraphAfter();
    oPara[i].Range.Paste();
    oPara[i].KeepTogether = -1;
    oPara[i].Range.Font.Size = 10;
    oPara[i].Range.Font.Name = "Arial";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文