1

我的数据库中有一个文本字段,它有一个多行的文本。使用 OpenXML 和书签生成 MS Word 文档时,文本变为单行。

我注意到在每一行中,书签值都显示字符“\r\n”。寻找解决方案,我找到了一些对我有帮助的答案,但我仍然遇到问题。

我已经使用了 run.Append(new Break()); 解决方案,但替换的文本也显示书签的名称。

例如:书签测试 =“第一段中的大文本\r\n第二段”。

它显示在 MS Word 文档中,如:

在第一段中测试大文本

第二段

任何人都可以帮我消除书签名称吗?

这是我的代码:

public void UpdateBookmarksVistoria(string originalPath, string copyPath, string fileType)
    {
        string wordmlNamespace = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";

        // Make a copy of the template file.
        File.Copy(originalPath, copyPath, true);

        //Open the document as an Open XML package and extract the main document part.
        using (WordprocessingDocument wordPackage = WordprocessingDocument.Open(copyPath, true))
        {
            MainDocumentPart part = wordPackage.MainDocumentPart;
            //Setup the namespace manager so you can perform XPath queries 
            //to search for bookmarks in the part.
            NameTable nt = new NameTable();
            XmlNamespaceManager nsManager = new XmlNamespaceManager(nt);
            nsManager.AddNamespace("w", wordmlNamespace);
            //Load the part's XML into an XmlDocument instance.
            XmlDocument xmlDoc = new XmlDocument(nt);
            xmlDoc.Load(part.GetStream());

            //pega a url para exibir as fotos
            string url = HttpContext.Current.Request.Url.ToString();
            string enderecoURL;
            if (url.Contains("localhost"))
                enderecoURL = url.Substring(0, 26);
            else if (url.Contains("www."))
                enderecoURL = url.Substring(0, 24);
            else
                enderecoURL = url.Substring(0, 20);

            //Iterate through the bookmarks.
            int cont = 56;
            foreach (KeyValuePair<string, string> bookmark in bookmarks)
            {
                var res = from bm in part.Document.Body.Descendants<BookmarkStart>()
                          where bm.Name == bookmark.Key
                          select bm;
                var bk = res.SingleOrDefault();
                if (bk != null)
                {
                        Run bookmarkText = bk.NextSibling<Run>();
                        if (bookmarkText != null) // if the bookmark has text replace it
                        {
                            var texts = bookmark.Value.Split(new[] { Environment.NewLine }, StringSplitOptions.None);

                            for (int i = 0; i < texts.Length; i++)
                            {
                                if (i > 0)
                                    bookmarkText.Append(new Break());

                                Text text = new Text();
                                text.Text = texts[i];
                                bookmarkText.Append(text); //HERE IS MY PROBLEM
                            }
                        }
                        else // otherwise append new text immediately after it
                        {
                            var parent = bk.Parent; // bookmark's parent element
                            Text text = new Text(bookmark.Value);
                            Run run = new Run(new RunProperties());
                            run.Append(text);
                            // insert after bookmark parent
                            parent.Append(run);
                        }

                        bk.Remove(); // we don't want the bookmark anymore
                }
            }

            //Write the changes back to the document part.
            xmlDoc.Save(wordPackage.MainDocumentPart.GetStream(FileMode.Create));
            wordPackage.Close();
        }}
4

0 回答 0