此上下文中不存在名称 SpaceProcessingModeValues

The name SpaceProcessingModeValues does not exist in this context

在我的 ASP.net 程序中,我有一个 class 来打开一个 word 文档,找到一个书签并插入给定的文本

我收到的错误是:名称 SpaceProcessingModeValues 在当前上下文中不存在。

using (WordprocessingDocument document = WordprocessingDocument.Open(strFileName, true))
    {
        MainDocumentPart mainPart = document.MainDocumentPart;
        IEnumerable<BookmarkStart> res = from bm in      mainPart.Document.Body.Descendants<BookmarkStart>()
            where bm.Name == "strField"
            select bm;
        int x = 0;

        BookmarkStart bookmark = res.SingleOrDefault();
        if (bookmark != null)
        {
            OpenXmlElement parent = bookmark.Parent; // bookmark's parent element

            var paragraph1 = new Paragraph {RsidParagraphAddition = "00023529", RsidRunAdditionDefault = "003E1BA6"};

            var run1 = new Run();
            var text1 = new Text("Text : "){ Space = SpaceProcessingModeValues.Preserve };

            run1.Append(text1);
            var bookmarkStart1 = new BookmarkStart {Name = "_GoBack", Id = 0};
            var bookmarkEnd1 = new BookmarkEnd {Id = 0};

            var run2 = new Run();
            var text2 = new Text();
            text2.Text = strGivenText;

            run2.Append(text2);

            paragraph1.Append(run1);
            paragraph1.Append(bookmarkStart1);
            paragraph1.Append(bookmarkEnd1);
            paragraph1.Append(run2);

            parent.InsertBeforeSelf(paragraph1);
        }

        // close saves all parts and closes the document
        mainPart.Document.Save();
        document.Close();
    }

您需要添加引用 DocumentFormat.OpenXml 命名空间