Quantcast
Channel: XML, System.Xml, MSXML and XmlLite forum
Viewing all articles
Browse latest Browse all 935

Replace Bookmars looses formatting in Word 2010

$
0
0

Hello

i'm replacing bookmark in a word 2010 file but it loses formatting (like indentation, numbering, lists etc)

Maybe i'm doing wrong in my code.

This is the code of function i use to replace bookmark :

        public static void ReplaceBookmarkText(WordprocessingDocument doc, string bookmarkName, string replacementText)
        {
            XDocument xDoc = doc.MainDocumentPart.GetXDocument();

            XElement bookmark = xDoc.Descendants(W.bookmarkStart).FirstOrDefault(d => (string)d.Attribute(W.name) == bookmarkName);

            if (bookmark == null)
                throw new BookmarkReplacerException("Document doesn't contain bookmark.");

            if (bookmark.Parent.Name.Namespace == M.m)
                throw new BookmarkReplacerException("Replacing text in math formulas is not supported.");

            if (OpenXmlPowerTools.RevisionAccepter.HasTrackedRevisions(doc))
                throw new BookmarkReplacerException("Replacing bookmark text in documents that have tracked revisions is not supported.");

            if (xDoc.Descendants(W.sdt).Any())
                throw new BookmarkReplacerException("Replacing bookmark text in documents that have content controls is not supported.");

            XElement newRoot = (XElement)FlattenParagraphsTransform(xDoc.Root);

            XElement startBookmarkElement = newRoot.Descendants(W.bookmarkStart).Where(d => (string)d.Attribute(W.name) == bookmarkName).FirstOrDefault();

            int bookmarkId = (int)startBookmarkElement.Attribute(W.id);

            XElement endBookmarkElement = newRoot.Descendants(W.bookmarkEnd).Where(d => (int)d.Attribute(W.id) == bookmarkId).FirstOrDefault();

            if (startBookmarkElement.Ancestors(W.hyperlink).Any() || endBookmarkElement.Ancestors(W.hyperlink).Any())
                throw new BookmarkReplacerException("Bookmark is within a hyperlink.  Can't replace text.");

            if (startBookmarkElement.Ancestors(W.fldSimple).Any() || endBookmarkElement.Ancestors(W.fldSimple).Any())
                throw new BookmarkReplacerException("Bookmark is within a simple field.  Can't replace text.");

            if (startBookmarkElement.Ancestors(W.smartTag).Any() || endBookmarkElement.Ancestors(W.smartTag).Any())
                throw new BookmarkReplacerException("Bookmark is within a smart tag.  Can't replace text.");

            if (startBookmarkElement.Parent != endBookmarkElement.Parent)
                throw new BookmarkReplacerException("Bookmark start and end not at same levels.  Can't replace text.");

            XElement parentElement = startBookmarkElement.Parent;

            var elementsBetweenBookmarks = startBookmarkElement.ElementsAfterSelf().TakeWhile(e => e != endBookmarkElement);

            var newElements = parentElement.Elements().TakeWhile(e => e != startBookmarkElement)
                .Concat(new[]
                {
                    startBookmarkElement,
                    new XElement(BookmarkReplacerCustomNamespace + "Insert",
                        elementsBetweenBookmarks
                            .Where(e => e.Name == W.r)
                            .Take(1)
                            .Elements(W.rPr)
                            .FirstOrDefault()),
                })
                .Concat(elementsBetweenBookmarks.Where(e => e.Name != W.p &&
                    e.Name != W.r && e.Name != W.tbl))
                .Concat(new[]
                {
                    endBookmarkElement
                })
                .Concat(endBookmarkElement.ElementsAfterSelf());

            parentElement.ReplaceNodes(newElements);

            newRoot = (XElement)UnflattenParagraphsTransform(newRoot);
            newRoot = (XElement)ReplaceInsertElement(newRoot, replacementText);
            newRoot = (XElement)DemoteRunChildrenOfBodyTransform(newRoot);

            xDoc.Elements().First().ReplaceWith(newRoot);

            doc.MainDocumentPart.PutXDocument();
        }


Viewing all articles
Browse latest Browse all 935

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>