【问题标题】:Insert multiple lines of text into a Rich Text content control with OpenXML使用 OpenXML 将多行文本插入富文本内容控件
【发布时间】:2018-03-03 09:55:15
【问题描述】:

我很难让内容控件遵循多行格式。它似乎从字面上解释了我给它的一切。我是 OpenXML 的新手,我觉得我一定缺少一些简单的东西。

我正在使用这个函数转换我的多行字符串。

    private static void parseTextForOpenXML(Run run, string text)
    {
        string[] newLineArray = { Environment.NewLine, "<br/>", "<br />", "\r\n" };
        string[] textArray = text.Split(newLineArray, StringSplitOptions.None);

        bool first = true;

        foreach (string line in textArray)
        {
            if (!first)
            {
                run.Append(new Break());
            }

            first = false;

            Text txt = new Text { Text = line };
            run.Append(txt);
        }
    }

我用这个把它插入到控件中

    public static WordprocessingDocument InsertText(this WordprocessingDocument doc, string contentControlTag, string text)
    {
        SdtElement element = doc.MainDocumentPart.Document.Body.Descendants<SdtElement>().FirstOrDefault(sdt => sdt.SdtProperties.GetFirstChild<Tag>().Val == contentControlTag);

        if (element == null)
            throw new ArgumentException("ContentControlTag " + contentControlTag + " doesn't exist.");

        element.Descendants<Text>().First().Text = text;
        element.Descendants<Text>().Skip(1).ToList().ForEach(t => t.Remove());

        return doc;
    }

我用类似...的东西来称呼它

doc.InsertText("Primary", primaryRun.InnerText);

虽然我也尝试过 InnerXML 和 OuterXML。结果看起来像

Example AttnExample CompanyExample AddressNew York, NY 12345 或

<w:r xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:t>Example Attn</w:t><w:br /><w:t>Example Company</w:t><w:br /><w:t>Example Address</w:t><w:br /><w:t>New York, NY 12345</w:t></w:r>

该方法适用于简单的文本插入。只是当我需要它来解释对我不起作用的 XML 时。

我觉得我必须非常接近得到我需要的东西,但我的摆弄让我无处可去。有什么想法吗?谢谢。

【问题讨论】:

    标签: openxml multiline contentcontrol


    【解决方案1】:

    我相信我尝试这样做的方式注定要失败。设置元素的 Text 属性总是会被解释为要显示的文本。我最终不得不采取稍微不同的策略。我创建了一个新的插入方法。

        public static WordprocessingDocument InsertText(this WordprocessingDocument doc, string contentControlTag, Paragraph paragraph)
        {
            SdtElement element = doc.MainDocumentPart.Document.Body.Descendants<SdtElement>().FirstOrDefault(sdt => sdt.SdtProperties.GetFirstChild<Tag>().Val == contentControlTag);
    
            if (element == null)
                throw new ArgumentException("ContentControlTag " + contentControlTag + " doesn't exist.");
    
            OpenXmlElement cc = element.Descendants<Text>().First().Parent;
            cc.RemoveAllChildren();
            cc.Append(paragraph);
    
            return doc;
        }
    

    它以同样的方式开始,并通过搜索它的 Tag 来获取 Content Control。但后来我得到它的父级,删除那里的 Content Control 元素并用段落元素替换它们。

    这与我的设想不完全一样,但它似乎可以满足我的需求。

    【讨论】:

    • 您好,感谢分享答案,您是如何将回车类型的文本转换为段落的?
    • 我试过这个stackoverflow.com/a/31755783/5955233,但它给了我损坏的 docx 文件。
    • 在我原来的问题中,我认为我展示了一个 ParseTextForOpenXML 方法,它可以满足您的要求。
    • 以及您自己的答案。当您在传递给 WordprocessingDocument 方法的文本中有回车时,如何在 CC 中添加多行文本?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-22
    • 1970-01-01
    • 2015-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-26
    相关资源
    最近更新 更多