簡體   English   中英

使用 OpenXml 將 word 中的一段拆分為兩段 SDK?

[英]split a paragraph in word to two paragraphs using OpenXml SDK?

我有一種方法可以在 word 文件的同一位置將段落分成兩段。 邏輯工作正常,但我丟失了文檔格式和 styles。

foreach (Paragraph p in body.Descendants<Paragraph>())
{
//splitting the paragraph
 var part2 = p.InnerText.Substring(startIndex);
 var part1 = p.InnerText.Substring(0, startIndex);
 p.InnerText.Replace(p.InnerText, part1);
 p.InnerText.Replace(p.InnerText, part2);
 pargs.Add(part1);
 pargs.Add(part2);
}
// clean the documetn 
body.RemoveAllChildren<Paragraph>();
//re-creat the paragraphs 
for (int i = 0; i < pargs.Count; i++)
{
   Paragraph para = body.AppendChild(new Paragraph());
   Run run = para.AppendChild(new Run());
   run.AppendChild(new Text(pargs[i]));
}
   
   return paras;
    

上面是簡化的代碼我知道我的方法是導致這個問題的原因,因為我正在獲取每個段落的內部文本並創建一個新段落而不使用 styles。我的問題是。 有沒有另一種方法

如果想保持原來的風格,只需要復制運行屬性,在新建的運行中設置即可。 類似的東西(雖然還沒有測試):

var properties = paragraph
                    .Descendants<Run>()
                    .First()
                    .RunProperties
                    .Clone();
...
var run = newParagraph.AppendChild(new Run()
{
  RunProperties = (RunProperties)properties
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM