[英]How to add text to existing paragraph without breaking the style in C#?
I have been trying to solve one problem in C# regarding updating paragraph text with some additional new text info:我一直在尝试解决 C# 中的一个问题,即使用一些额外的新文本信息更新段落文本:
I am not a C# developer, forgive me if the question is silly or easy to solve.我不是 C# 开发人员,如果问题很愚蠢或容易解决,请原谅我。
I have several paragraphs like this:我有几个这样的段落:
Alice is going to do some shopping.
爱丽丝要去购物。
Bob is a good guy.
鲍勃是个好人。
Let's say, these paragraphs are written in Arial font with 11 pts.比方说,这些段落是用 Arial 字体写的,有 11 pts。 So I want to add some text after each paragraph.
所以我想在每个段落之后添加一些文字。
The end result would be:最终结果将是:
Alice is going to do some shopping.SomeText0
爱丽丝要去购物。SomeText0
Bob is a good guy.SomeText1
Bob 是个好人。SomeText1
I have tried this:我试过这个:
using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(document, true))
{
List<Paragraph> paragraphs = paragraphService.GetParagraphs(wordDoc);
foreach (Paragraph par in paragraphs)
{
string paragraphText = paragraphService.ParagraphToText(par);
paragraphText = textService.DeleteDoubleSpace(paragraphText);
if (paragraphText.Length != 0)
{
if (paragraphText == targetParagraph)
{
//Here I know that the added text will be corresponding to the my target paragraph.
//This paragraph comes from a JSON file but for simplicity I did not add that part.
par.Append(new Run(new Text("SomeText0")));
par.ParagraphProperties.CloneNode(true);
}
}
}
}
Adding the text works, but the style is not the same and some random style that I don't want.添加文本有效,但样式不一样,并且有一些我不想要的随机样式。 I want the newly added text to have the same font and size as the paragraph.
我希望新添加的文本与段落具有相同的字体和大小。
I have also tried several options, to make it Paragraph, just text, etc. But I could not find a solution.我也尝试了几个选项,使其成为段落,只是文本等。但我找不到解决方案。
Any help would be appreciated.任何帮助,将不胜感激。
The open xml format stores paragraphs like the following开放的 xml 格式存储的段落如下
<w:p>
<w:r>
<w:t>String from WriteToWordDoc method.</w:t>
</w:r>
</w:p>
Here,这里,
Paragraph
class, Paragraph
class 表示的元素,Run
class, and, Run
class 表示的元素,并且,Text
class. Text
class 表示的元素。 So you are appending a new <w:r> => Run
element which has its own format settings, and since you don't specify any formatting, defaults are used.因此,您要附加一个新的
<w:r> => Run
元素,该元素具有自己的格式设置,并且由于您未指定任何格式,因此使用默认值。
EDIT 1: And as it seems, when there are parts in this paragraph that are formatted differently, there can be multiple Run elements under a paragraph.编辑1:看起来,当本段中有不同格式的部分时,一段下可以有多个 Run 元素。
So, instead you can find the last Run element containing a Text element and modify its text.因此,您可以找到包含 Text 元素的最后一个 Run 元素并修改其文本。
foreach (Paragraph par in paragraphs)
{
Run[] runs = par.OfType<Run>().ToArray();
if (runs.Length == 0) continue;
Run[] runsWithText = runs.Where(x => x.OfType<Text>().ToArray().Length > 0).ToArray();
if (runsWithText.Length == 0) continue;
Text lastText = runsWithText.Last().OfType<Text>().Last();
lastText.Text += " Some Text 0";
}
Hope this helps.希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.