1
votes

Hey guys I am trying to manipulate a word .docx file using the openXML sdk and C#.

I can open the file fine and insert paragraphs but I need to insert a paragraph at a specific location in my document(after a certain paragraph in my body).

I have not been able to find anything useful online about how to accomplish this.

Can anyone point me in the right direction?

1
honestly...nothing. I have no idea where to begin and have not found any samples of what I need to do. My thoughts were to find the paragraph just BEFORE where I need to insert and then do an InserAfter<ThatParagraph>.... - stephen776
What is the key you are using to find that paragraph to insert before? A piece of text or an index? - amurra
that sounds right, try it and if you have trouble ask a more specific question here. - Samuel Neff
retagged to remove "asp.net" and replace openxml with "openxml-sdk" - bitxwise

1 Answers

2
votes

The solution that I have settled on(though i know there are other ways) was to add a bookmark to the document, find the bookmark using the SDK, and replaceing it with my list. Works great.

IDictionary<String, BookmarkStart> bookMarkMap = new Dictionary<String, BookmarkStart>();
            foreach (BookmarkStart bookMarkStart in wordDoc.MainDocumentPart.RootElement.Descendants<BookmarkStart>())
            {
                bookMarkMap[bookMarkStart.Name] = bookMarkStart;
            }

            foreach (BookmarkStart bookMarkStart in bookMarkMap.Values)
            {
                if (bookMarkStart.Name == "MyBookmarkName")
                {
                    //do insert here   
                    var parent = bookMarkStart.Parent;

                    //create paragraph to insert
                    parent.InsertAfter(MyNewParagraph);         
                }
            }