简体   繁体   English

如何在不破坏 C# 样式的情况下向现有段落添加文本?

[英]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,这里,

  1. p is the element represented by the Paragraph class, p 是Paragraph class 表示的元素,
  2. r is the element represented by Run class, and, r 是由Run class 表示的元素,并且,
  3. t is the element represented by the Text class. t 是由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.

相关问题 在现有pdf内容后添加新段落c# - add new paragraph after existing pdf content c# 如何在不使用 iText7 和 C# 覆盖内容的情况下向现有 pdf 添加文本? - How to add text to an existing pdf without overwriting the content with iText7 and C#? 如何在不破坏用户代码的情况下在现有API中重命名ac#namespace? - How to rename a c# namespace in an existing API without breaking users code? 如何在WPF C#中将字符串资源中的文本添加为​​段落文本? - how to add text from a string resource as a paragraph text in wpf c#? C#将文本添加到现有文本文件 - C# Add text to existing text file 如何在C#Excel Interop中将文本添加到现有形状(文本框) - How to add text to an existing shape(textbox) in C# Excel Interop 如何在不破坏客户端的情况下向C#接口中的方法添加参数? - How do I add a parameter to a method in my interface in C# without breaking clients? C#WPF DataGrid:如何在不破坏DataGrid iteslf的情况下为DataGrid单元内的文本绘制颜色? - C# WPF DataGrid: how to color paint text inside DataGrid cells without breaking the DataGrid iteslf? 如何在RTF c#段后添加空格? - How to add a space after the paragraph RTF c#? 如何在C#中缩进XML文本并保留换行符? - How to indent XML text and preserve line breaking in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM