簡體   English   中英

Word open xml的富文本內容控件中的新行

[英]New Line in Rich Text Content Control of Word open xml

我有一個包含Rich text Content Control的 Word 文檔。

我想用新行添加文本。

using (WordprocessingDocument theDoc = WordprocessingDocument.Open(docName, true))
 {
   MainDocumentPart mainPart = theDoc.MainDocumentPart;
   foreach (SdtElement sdt in mainPart.Document.Descendants<SdtElement>())
     {
        SdtAlias alias = sdt.Descendants<SdtAlias>().FirstOrDefault();
        if (alias != null)
          {
            string sdtTitle = alias.Val.Value;
            var t = sdt.Descendants<Text>().FirstOrDefault();
             if (sdtTitle == "Body")
               {
                 t.Text = "Welcome to Yazd With its winding lanes, forest of badgirs,\r\n mud-brick houses and delightful places to stay, Yazd is a 'don't miss' destination. On a flat plain ringed by mountains, \r\nthe city is wedged between the northern Dasht-e Kavir and southern Dasht-e Lut and is every inch a city of the desert." }
         }
     }
}

我使用帶有\r\n的文本,但不添加新行。

一些字符,如制表符、換行符等,是用特殊的 XML 元素定義的。
對於您的情況,您需要<w:br/>元素,因此請嘗試以下操作:

using (WordprocessingDocument theDoc = WordprocessingDocument.Open(docName, true))
{
    MainDocumentPart mainPart = theDoc.MainDocumentPart;
    foreach (SdtElement sdt in mainPart.Document.Descendants<SdtElement>())
    {
        SdtAlias alias = sdt.Descendants<SdtAlias>().FirstOrDefault();
        if (alias != null && alias.Val.Value == "Body")
        {
            var run = sdt.Descendants<Run>().FirstOrDefault();
            run.RemoveAllChildren<Text>();

            var text = "Welcome to Yazd With its winding lanes, forest of badgirs,\r\n mud-brick houses and delightful places to stay, Yazd is a 'don't miss' destination. On a flat plain ringed by mountains, \r\nthe city is wedged between the northern Dasht-e Kavir and southern Dasht-e Lut and is every inch a city of the desert.";
            var lines = text.Split(new string[] { "\r\n" }, StringSplitOptions.None);

            foreach (var line in lines)
            {
                run.AppendChild(new Text(line));
                run.AppendChild(new Break());
            }

            run.Elements<Break>().Last().Remove();
        }
    }
}

我希望這有幫助。

假設您確實希望將 function 限制為富文本內容控件,即塊級結構化文檔標簽,您將尋找SdtBlock的實例。 使用SdtElement ,您還會發現內聯級 ( SdtRun )、行級 ( SdtRow ) 和單元級 ( SdtCell ) 結構化文檔標簽,因為SdtBlockSdtRunSdtRowSdtCellSdtElement的子類。

下面是一個簡單的實現,假設富文本內容控件應該只包含多行文本。 它修剪線條,因為示例文本包含看起來像無關空格的內容。

public void AddMultiLineTextToRichTextContentControlsUsingRuns()
{
    // Produce the list of lines from the text separated by "\r\n"
    // in the question, trimming leading and trailing whitespace.
    const string text = "Welcome to Yazd With its winding lanes, forest of badgirs,\r\n mud-brick houses and delightful places to stay, Yazd is a 'don't miss' destination. On a flat plain ringed by mountains, \r\nthe city is wedged between the northern Dasht-e Kavir and southern Dasht-e Lut and is every inch a city of the desert.";
    string[] separator = { "\r\n" };

    List<string> lines = text
        .Split(separator, StringSplitOptions.None)
        .Select(line => line.Trim())
        .ToList();

    // Get the w:document element.
    const string path = @"path\to\your\document.docx";
    using WordprocessingDocument wordDocument = WordprocessingDocument.Open(path, true);
    Document document = wordDocument.MainDocumentPart.Document;

    // Get all Rich Text (i.e., block) w:sdt elements having a w:alias
    // descendant with w:val="Body".
    IEnumerable<SdtBlock> sdts = document
        .Descendants<SdtBlock>()
        .Where(sdt => sdt.Descendants<SdtAlias>().Any(alias => alias.Val == "Body"));

    foreach (SdtBlock sdt in sdts)
    {
        // Create one w:r element per line, prepending a w:br to the
        // second and following runs.
        IEnumerable<Run> runs = lines
            .Select((line, index) =>
                index == 0
                    ? new Run(new Text(line))
                    : new Run(new Break(), new Text(line)));

        // Create or replace the w:sdtContent element with one that has
        // a single w:p with one or more w:r children.
        sdt.SdtContentBlock = new SdtContentBlock(new Paragraph(runs));
    }
}

暫無
暫無

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

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