Blog - Title

Move/Insert/Delete Paragraphs in Word Processing Documents using the Open XML SDK

Move/Insert/Delete Paragraphs in Word Processing Documents using the Open XML SDK

  • Comments 38

We can potentially make extensive modifications to Open XML word processing documents.  Many scenarios would benefit from the ability to move paragraphs around (both inter and intra-document), insert paragraphs, and delete paragraphs.  In practice, the process can be quite daunting because paragraphs often contain markup that refers to something outside of the paragraph.  For example, a paragraph might contain markup indicating that a comment begins inside that paragraph.  But because the comment ends in a later paragraph, another paragraph contains the markup indicating the end of the comment.  And the comment markup contains a reference to another one of the Open XML parts (the Comments part, of course), with a zero-based index.  There are a fair number of cases of markup in a paragraph that have relations to markup outside the paragraph, including comments, bookmarks, images, etc.  When you insert, delete, and move paragraphs, you must keep this markup in sync.  This isn’t easy.

This blog is inactive.
New blog: EricWhite.com/blog

Blog TOC
(Update March 27, 2009 - updated the Headers and Footers section, and added the Charts section.) 

This blog post details much of what you must do to keep such inter-related markup in sync.  It also presents some sample code (from the PowerTools for Open XML open source project), that shows one approach to solving this problem.  It is useful to have example code like this.  It can serve to educate about the Open XML specification, and perhaps inspire other innovative approaches to solving this problem.

Note from Eric White:  This is a guest post written by Bob McClellan, who is one of the open source developers on the PowerTools for Open XML project on CodePlex.  This code is the basis of some new cmdlets that enable composability of documents – take any number of source documents, specify some subset of each document in each source document, and compose a new, valid Open XML document.  The code takes care of a myriad of details to make sure that the new document is valid.

This is one of the most important Open XML posts that I’ve put on my blog.  The details handed by the code presented in this post enable true document composability.

The gist of the approach presented here is that we don’t actually modify any existing documents.  Instead, we start with any number of source documents, extract an arbitrary number of paragraphs (from any location in each document), and assemble a new, valid Open XML document.  If a paragraph contains a reference to an image, the image is moved to the new package, and the appropriate fix-ups are applied to the markup that refers to the image.  Ditto for comments, and many other cases.  We can use this basic function to do other interesting things – to delete a range of paragraphs in the middle of the document, we can specify the source document twice, first specifying a range up to the section that we want to delete, and then specifying a range of paragraphs after the deleted range.  After assembling the new document, we have a valid Open XML word processing document with the unwanted paragraphs removed.

In the next section, I will explain how to use the example code.  The final section explains generally how the code in the example works.

To get this example, you will need to download it from the CodePlex web site for the “PowerTools for OpenXML” project. (The PowerTools contain a number of PowerShell cmdlets that manipulate Open XML documents.) The link is www.CodePlex.com/PowerTools (then click on the “Releases” tab, and look for “DocumentBuilder.zip” under “Downloads and Files”).  The file that you will download is a compressed file containing the entire folder structure including the source code, sample documents and Visual Studio solution and project. You can open the solution file in any version of Visual Studio 2008 that supports C#, including the Visual C# 2008 Express.

In addition, this code uses the Open XML SDK Version 1 or Version 2.  In order to use this code, you need to download and install one of the versions of the SDK.  In addition, you may need to update the reference to the assembly in the project.

Running the Example

The example program creates a few new documents (named Test1.docx, Test2.docx, and Test3.docx) from existing documents (named Source1.docx and Source2.docx).  After running the example, Test1.docx contains a few paragraphs extracted from an existing document.  Test2.docx contains a set of paragraphs from the beginning and from the end of an existing document (omitting some paragraphs in the middle).  Test3.docx concatenates two existing documents into a new document.  This is a more generalized and effective approach for assembling documents than using altChunk.

Note: the source documents are located in the bin/debug directory under the project.  The created documents are placed in the same directory.  This allows you to simply open the project, fix up the reference to the Open XML SDK (if necessary), and run the program to see the newly created documents.

Each test calls the DocumentBuilder.BuildDocument method to create the new document.  The code for Test2 looks like this:

using (WordprocessingDocument part1 =
    WordprocessingDocument.Open("Source1.docx", false))
{
    List<Source> sources = new List<Source>();
    sources.Add(new Source(part1, 0, 12, true));
    sources.Add(new Source(part1, 49, true));
    DocumentBuilder.BuildDocument(sources, "Test2.docx");
}


The first line is calling the Open XML SDK to open the source document.  Then, the example creates a list of objects of type OpenXml.PowerTools.Source.  This collection is used to define which groups of paragraphs, or entire documents, will be used to create a new document.  In this case, two groups of paragraphs from the same document will be used to create the new document.  The first Source object is created with a “start” value of 0 and “count” of 12.  That means that twelve paragraphs will be extracted from the existing document starting with the first paragraph.  The starting paragraph is numbered 0, so if you are used to thinking of the first paragraph as paragraph 1, just subtract one when specifying the starting paragraph value.  The second Source object specifies only the “start” value, so it extracts all the remaining paragraphs starting at that value.  The call to BuildDocument creates the new document, named “Test2.docx” in this case.

NOTE: Although I have been describing the source parameters as referring to paragraphs, that is not strictly true. It actually refers to elements that are children of the “body” element. Although paragraphs are the most common, child elements of the body element may include tables, various range elements (start and end ranges often appear as siblings of paragraphs), content controls and others (e.g. Math paragraphs).  For more complex documents, you will probably need to view the XML directly in order to determine which values to use for start and count.  Of course, these values can be determined programmatically by scanning the XML.

How it Works

The source code for this example consists of three files.  Program.cs contains the Main function which makes calls to BuildDocument.  DocumentExtensions.cs contains extensions to the WordprocessingDocument class that handle reading and writing the document parts (using the GetXDocument and FlushParts extension methods).  The code in DocumentExtensions.cs will be covered in detail in a future blog post.  DocumentBuilder.cs contains the BuildDocument method, two versions of BuildOpenDocument, and supporting methods.  It also defines the Source class used to declare the desired sources for BuildDocument.  Each of these code sections are described below in general terms.  If you have more specific questions about the code, you are invited to ask questions in the comments on this post.

The first part of DocumentBuilder.cs defines the Source class.  The various constructors for the class all result in the same internal structure.  The member variables are a source document, the desired contents of that document (expressed as the starting paragraph number, and count of paragraphs in the constructor), and a Boolean value indicating if the final section divider should be retained.  Each constructor uses a LINQ-to-XML query to define the desired contents.  Note that these contents are not actually stored in the variable, but that the query itself is stored.  The elements of the query will only be retrieved as needed during processing.

The next part defines the XML namespace strings used in Wordprocessing documents.  These will be used throughout the code to refer to various element and attributes.  Next are the public functions, BuildDocument and BuildOpenDocument.  BuildDocument is used in the example and explained above.  There are two overloads of BuildOpenDocument.  The one that creates a file-based document is used by some of the PowerTools cmdlets. The other shows how to use a MemoryStream to create a document (which could be useful when using a SharePoint document library instead of the file system for document storage).  All of the public functions call a common internal function, DoBuildDocument.  Although the code may look fairly complex, it can be broken down into a few general steps.

1.       Create the new document.  A main document part must be created with “document” and “body” XML elements.

2.       Copy parts from the first source document.  A number of parts are copied from the first source document (e.g. styles) so that the new document will generally look like the first document in the list of the source documents - the first source document is essentially the “master” of the new document.  The parts that are copied are core, extended and custom file properties, settings, web settings, styles, font tables, and any theme.  The settings part often contains references to footnotes and endnotes that must be copied (e.g. separators).  The private functions that handle these operations are CopyStartingParts, CopyFootnotes and CopyEndnotes.

3.       Fix ranges.  There are a number of elements that come in pairs and “mark” everything within the two elements as part of that range.  Since these pairs can be “broken” by extracting only part of the range, this step fixes any broken ranges.  See below for more details.

4.       Copy references.  Many elements refer to other document parts (e.g. images) or elements within other parts (e.g. comments).  Each of these must be properly copied to the new document.  Many must be translated during that copy.  For example, a comment that is copied to a new document may not be able to use the same ID that it had in the original since it could be the same as the ID of another comment that was copied to the new document.  See below for more details.

5.       Append the source content.  The corrected source contents can now be appended to the new document.  The only special case here is the handling of the final section property element (sectPr). The sectPr element can appear either as a child of the body element or as a child of a paragraph (p) element.  The first case should only occur once as the last child element of the body.  It is essentially the default section for the document.  If there are any other section breaks in the document, those will appear as sectPr elements in the last paragraph before the section break.  When the code is putting together paragraphs from separate documents, it still has to be sure that these rules are followed.  There are two ways to handle multiple sections – either keep them or not.  If the sectPr at the end of a document is going to be kept and there are more paragraphs being concatenated after it, then the sectPr that used to end the original document must be moved into its last paragraph instead. If it is not going to be kept, then it must be left out of the new document.

The rest of the DocumentBuilder.cs file contains the code to support these steps.  Here is a detailed explanation of what is happening in each of the “fix” and “copy” operations.  The name or names in parenthesis are the names of the functions that handle each operation.

Fix Ranges (FixRanges, FixRange, DeleteUnmatchedRange)

Ranges can appear either inside or outside of paragraphs.  The function FixRanges makes all the calls necessary to fix ranges in the document.  The FixRange function is used to make sure that there is a matching start and end, based on an identifying attribute.  The names of the start and end elements are also passed to the function, of course.  The function iterates through all the start elements and uses a LINQ to XML query to try to find the matching end element.  If it is not found, then the matching end element is copied from the old document into the last paragraph of those being extracted.  The same process is then performed for each end element to see if it matches to a start element.  There is one special case for comment ranges.  A comment range must also contain a reference element to that same comment.  If the reference element was not included in the paragraphs being copied, then one is created.

The DeleteUnmatchedRange is used to delete “move from” ranges that don’t have a matching “move to” range and vice versa.  These ranges should come in pairs with the same “name” attribute.  If there isn’t a complete pair, then this function deletes the range.  These types of ranges are not necessary for a document, but they give additional information about how the document has been changed.  Leaving in an unmatched range will not make the document invalid, but it can be confusing when viewed in Microsoft Word.

Styles (CopyReferences, MergeStyles)

It is very complicated to determine what styles are referenced in a document, especially since styles can reference other styles.  It is also generally desirable to include all the styles from a document, even if they are not referenced.  However, it is not valid for two styles with the same name to appear in a document.  The MergeStyles function copies all styles that do not have a matching name in the new document.  That means that the first appearance of a style name in the source documents will be the one used in the new document.

Font Tables (CopyReferences, MergeFontTables)

Font tables are handled in the same way as styles.  The first appearance of a font table with a particular name is the one that is kept in the new document.

Footnotes and Endnotes (CopyFootnotes, CopyEndnotes)

There are, in general, two kinds of references – references to elements within a particular part or references to other parts.  Footnotes and Endnotes are the first type.  CopyFootnotes and CopyEndnotes iterate through all footnote or endnote references and then copy that footnote or endnote element from the source document to the new document.  Of course, the ID of the new element may need to be changed so that there is no overlap from the different source documents.  Rather than trying to change numbers only when there is a conflict, the new document renumbers all the elements starting at zero.  Of course, the reference to the elements in the main document must be changed to match.

Comments (CopyComments)

Comments work very much like footnotes and endnotes – all comments appear in a single part.  There is one difference and that is that they can be referenced from range elements as well as a “commentReference” element.  Since a “commentReference” element must always appear in the document, those are used to determine which comments to copy and then any range elements are changed to the same ID number, if they appear in the new document.

Images (CopyImages)

Images are the second type of reference - each image reference in the main document refers to its own separate part in the package.  In the case of images, these parts are binary data, rather than XML documents, so they must be copied using block read and write operations.  The OpenXML SDK will automatically generate a unique ID for the copied part.  That new ID must be changed in the reference within the new document.

Numbering (CopyNumbering)

This is the most complex copy because the numbering part contains both numbering and abstract numbering definitions.  The properties defining the numbering are in the abstract element, but some can be overridden in the numbering definition.  The main document will only refer to numbering elements.  The example code tries to reuse abstract numbering definitions that are the same (based on the “nsid” attribute) and it also tries to reuse numbering definitions that refer to the same abstract numbering definitions, as long as they don’t have any override elements.  (This approach is very similar to Microsoft Word’s method for copying numbering elements when you copy and paste numbered paragraphs from one document to another.)

Headers and Footers (CopyHeaders, CopyFooters, CopyHeaderShapes, CopyHeaderEmbeddedObjects, CopyHeaderImages, CopyFooterShapes, CopyFooterEmbeddedObjects, CopyFooterImages)

Like images, each header or footer is its own part.  To add to the complexity, these parts may also contain references to images, shapes and embedded objects.  The copy of those additional references is done just like those of the main document, except that the parts are created within the context of the header or footer.  In other words, the header and footer parts are essentially small documents of their own.  Since the header and footer parts are XML, they can be copied as XDocuments.

Diagrams (CopyDiagrams)

Diagram elements contain four attributes that refer to other parts in the document.  The “dm” attribute refers to a DiagramDataPart, the “lo” attribute refers to a DiagramLayoutDefinitionPart, the “qs” attribute refers to a DiagramStylePart and the “cs” attribute refers to a DiagramColorsPart.  All four of these parts are in XML and can be copied as XDocuments.

Shapes (CopyShapes)

Shapes are just another case of references to separate parts.  Those parts are copied as XDocuments.

Embedded Objects (CopyEmbeddedObjects)

Embedded objects are referenced very much like Images.  The referenced parts are binary data and must be copied using block read and write operations.

Custom Control Data (CopyCustomXml)

Custom controls have the ability to refer to XML data that appears in a separate “data store” part.  Multiple controls may refer to the same data store part, but there can also be more than one data store part.  Each data store part is associated with a GUID that is referenced from the custom control in the main document.  The CopyCustomXml function creates a list of all the GUID’s that are referenced and then copies each of them from the source to the new document.  The data store parts are made up of a CustomXmlPart and a CustomXmlPropertiesPart, so both must be copied along with the appropriate changes to ID’s.

Hyperlinks (CopyHyperlinks)

Hyperlinks actually refer to an “external relationship,” rather than referring to any part within the document.  These external relationships are copied from the source document for each hyperlink that is being copied to the new document.  Hyperlinks can be referenced from “hyperlink” or “imagedata” elements, so both of those cases are handled in the CopyHyperlinks function.

Charts(CopyCharts, CopyChartObjects)

Like headers and footers, each chart is its own part, which can be copied as an XDocument. In addition, each chart usually contains a reference to an embedded object with the data for the chart. These embedded objects are copied as binary data.

Caveats

There are some documents that the example cannot handle correctly.  These cases are rare and difficult to handle, so they were left out to simplify the example.  Here is a list of those special cases:

1.       Custom XML can be embedded in the body of a document, but this completely changes the hierarchy of elements in the resulting XML. That type of source document would require a different method for defining the paragraphs to extract.  Custom XML probably wouldn’t typically be used in this type of document assembly solution, so I decided it did not need to be handled for this example.

2.       The Glossary part is not copied. It is possible to create a header, for example, that refers to an entry in the glossary. If you use the code to copy from that type of document, the header text could be blank or partially blank as a result. However, it will not cause an error when opening the document in Microsoft Word.

3.       Copying of themes is not fully implemented.  If the theme contains references to images, then the resulting document will be invalid.  This problem can be avoided by removing the theme or choosing a document without images in the theme as the first document.  I plan to solve this problem in a future update.

4.       This example was tested with Microsoft Word 2007. I haven’t done any testing with Office 2003.

Conclusion

I hope this example shows how to handle some of the complexities of the Open XML standard for Word Processing documents.  Hopefully, by sharing the detailed code, you will be able to perform more of the types of document manipulations that you need to take advantage of the Open XML standard.

Leave a Comment
  • Please add 5 and 5 and type the answer here:
  • Post
  • Wow! Just what we were looking for..

  • Hi Kerry,

    When you're ready, kindly give feedback on this code / guidance!  I'm very interested to hear if it works for you.

    -Eric

  • Hi Eric,

    I've been using the SDK for a little bit, attempting to do some manipulation of the word documents read in, I've done something pretty simple to the Source instances created..

     foreach (var content in source.Contents)

        content.Value = "hello";

    and then I use these new 'Sources' in the 'BuildDocument' method. The problem I have is that I get a blank document when I open it in Word. Opening the docx and inspecting the document.xml file shows all the data is there, just it doesn't display.

    Have I done some wierd thing with the linking by changing the value of the XElement?

    Cheers for this and your post on the OpenXML stuff!

    Chris

  • Hi Chris,

    Changing the value of the XElements in the source.Contents using the Value property won't work.  Those elements need to be well-formed, valid paragraph:

       <w:p>
         <w:r>
           <w:t>Here is a detailed explanation of what is happening in each of the “fix” and “copy” operations. The name or names in parenthesis are the names of the functions that handle each operation.</w:t>
         </w:r>
       </w:p>

    Of course, all bets are off if this paragraph refers to something that isn't in the source document, such as an image or something.  But if you want to tweak those XElement objects, keeping them valid, it should work.  I haven't tried this, though.

    -Eric

  • This looks and works great except if one or more of the documents contains a chart - then the output file gets corrupt.  

    I tried to add some code that copied over the chartparts - this gave the chartparts in the new document new reference IDs but I cannot figure out how to change the references in the original Document body

    private static void CopyChartObjects(WordprocessingDocument oldDoc, WordprocessingDocument newDoc)

    {

       foreach (ChartPart cpItem in oldDoc.MainDocumentPart.ChartParts)

       {

           //Get original ID

           string relId = oldDoc.MainDocumentPart.GetIdOfPart(cpItem).ToString();

           ChartPart newPart = newDoc.MainDocumentPart.AddPart(cpItem);

           string newrelId = newDoc.MainDocumentPart.GetIdOfPart(newPart).ToString();

           //Substitute the newref for the old ref in the new document ...

           //Kinds stuck here

       }

    }

     

     

  • @teltest, thanks!  We'll fix this, and post the revised code.

    -Eric

     (Update: March 19, 2009 - the fixed code has been posted to CodePlex.  (www.codeplex.com/powertools).

  • Hi Eric,

    Fantastic post and code. I ran into a couple of object ref errors with some of my sample docs in CopyNumbering that were fixed by changing to check for existence of "num" elements before setting the number ~ ln 766:

    newNumbering = newDoc.MainDocumentPart.NumberingDefinitionsPart.GetXDocument();

    elements = newNumbering.Root.Elements(ns + "num");

    if (elements.Count() > 0)

    {

        number = elements.Max(f => ((int)f.Attribute(ns + "numId"))) + 1;

    }

    elements = newNumbering.Root.Elements(ns + "abstractNum");

    if (elements.Count() > 0)

    {

        abstractNumber = elements.Max(f => ((int)f.Attribute(ns + "abstractNumId"))) + 1;

    }

    I guess my first doc had a number element, but no actual numbers so my 2nd doc bunked up. Anyway simple fix.

    An actual merging issue i ran in to was with different style headers. My first document had even and odd pages whereas my second doc only had default. The second doc was then using even/odd pages so the header "disappeared". The user story i'm using this in doesn't actually need to handle this situation, so its not an issue that i need to resolve right now, but it would be nice to understand (believe i tried!)

    Thanks for the great work, it not only functions but allowed me to understand a lot more of the inner workings word's openxml structure by tracing through the code.

  • Niall,

    Thanks for the feedback. I will post a fix for the numbering issue soon.

    There is an option to "keep sections" when merging documents that should change the behavior you saw with headers and footers. Headers and footers are stored in the section properties, so a new section break must be created in order to store the headers/footers from documents other than the first.

    -Bob

  • I have multiple documents each with a header and each of those headers might have a different image. DocProc will merge the documents and their headers, but the images have not been merged into the new document. This is wonderful open source project. Thank you so much for posting.

    -RW

  • Hi RW,

    You might have a slightly older version of DocProc.  There was a bug in the first version that didn't merge the images in headers/footers into the new document.  Would you please try the most recent version available on codeplex.com/PowerTools, and let me know if it works for you?

    We are putting the final touches on a new version that gives finer control over headers/footers.  In addition, the new one will use the same image if included more than once in the merged document.

    I'm really happy that you like the project!  It's been fun!

    -Eric

  • Hi Eric.

    thank you for your code!

    I allways get an exeption in :

    public static XDocument GetXDocument(this OpenXmlPart part)

    {

       XDocument xdoc = part.Annotation<XDocument>();

       if (xdoc != null)

           return xdoc;

       try

       {

           using (StreamReader sr = new StreamReader(part.GetStream()))

           using (XmlReader xr = XmlReader.Create(sr))

           {

               xdoc = XDocument.Load(xr);

    this throws : root element is missing !

    Futhermore, I have a complex matrix of imbricated document that I have to be append at a certain place (replacing tokens by others files : could be wordart, images, rtf or docx) : so I have to combine your code and altchunks. It could be

    What about making a public method "InsertAt(source)"

    Ernest

  • Hi Ernest,

    I suspect you are trying to open an invalid document.  Have you tried with a known good doc?

    -Eric

  • Hi Eric.

    What a brilliant post. Unfortunatelly (at least for me) I read it too late.

    I had a lot to do with merging documents, copying headers/footers/styles/numberings... the list seems endless. It opens to me a whole new approach and i am eager to try it out real soon.

    Keep posting such great work.

    -Jan

  • Hi Eric, great work!

    I'm a bit confused regarding bookmark ids, and I could use your help.

    I'm assembling documents from sources that contain bookmarks, and I noticed using the reflector that the bookmark ids in the output document are not unique.

    For example, if I assemble a document from 3 sources that contain 1 bookmark each, the output document contains 3 bookmarks with id="0".

    Word 2007 does not seem to have any problems with these output files.  The compatibility checker did not find any problems, and Word 2003 with the compatibility pack was able to read and inspect the bookmarks correctly.

    I could add code to the class that re-sequences all bookmark ids, but the documentation on MSDN does not specify that they need to be unique, just that there must be a bookmarkEnd that is 'subsequent to this [bookmarkStart] element in document order with a matching id attribute value'.

    So, should I worry about this?  My first instinct is to assume that duplicate ids are ok as long as the bookmark contents to not overlap.

     - Bill

  • Hi Bill,

    I've taken a look at the spec, and the description for an id is "Specifies a unique identifier for an annotation within a WordprocessingML document.

    So I believe that they should be unique, and that this is a bug in DocumentBuilder.

    It will be a bit before I can get to this due to some upcoming deadlines.  Up to you whether you resequence after, fix DocumentBuilder (if you do, please let me know the fix :), or not worry about it, because Word isn't complaining.

    -Eric

Page 1 of 3 (38 items) 123