Search code examples
c#openxmlwordprocessingml

Merge multiple Word Document styles into one Open Xml


I am currently working on a project where I merge 3 documents into one new one. To add them in, I am creating an AltChunk method to contain the documents.

My question is there is a conflict on the styling between the three. By that, I mean I save one with a table that has red text underneath. However, once the three merge together into the new one, that document's styling is reset to plain black text. Is there a way to merge the styling of all three into this new document?

Below is my code of how I am merging in the documents (I don't show the top portion because all of it is alright so far).

using (WordprocessingDocument package = WordprocessingDocument.Create(fileName, DocumentFormat.OpenXml.WordprocessingDocumentType.Document))
{
    ...
    #region Append Non-Standard Section Template
    var nssAltChunkId = "AltChunkIdNSS" + this.AopPlanId.Value.ToString();
    var nssChunk = package.MainDocumentPart.AddAlternativeFormatImportPart(
                     AlternativeFormatImportPartType.WordprocessingML, nssAltChunkId);
    using (var fileStream = new MemoryStream(nssBuffer))
    {
        nssChunk.FeedData(fileStream);
    }

    var nssAltChunk = new DocumentFormat.OpenXml.Wordprocessing.AltChunk();
    nssAltChunk.Id = nssAltChunkId;
    package.MainDocumentPart.Document.Body.InsertAfter(nssAltChunk, package.MainDocumentPart.Document.Body.Elements<Paragraph>().Last());
    #endregion

    ... //Next 2 documents are the same way

    package.MainDocumentPart.Document.Save();
}

Any help would be appreciated. Thanks.

Edit: I changed to using DocumentBuilder from PowerTools, however, that still does not solve the issue of merging the styles. Any suggestions would be appreciated.


Solution

  • What Word does with formatting ("styling") depends on the origin of the formatting and algorithms inherent in Word on how to handle formatting conflicts. Based on the information you provide it's difficult to know exactly what the situation is with these documents, but here are some rules of thumb:

    1. Word will retain direct formatting (such as clicking Bold or Italics)

    2. When in-coming documents have styles of the same name as styles already present in the target document, the in-coming styles will take on the definitions of the styles in the target document. This is by design as Word was conceived as a document production tool, not an archiving tool.

    I'm guessing that (2) is the situation with which you're confronted. The only way to retain the style definitions is to first give the styles different names / define a different set of styles and apply those to the text in place of the existing styles. For example, if the Normal style of two documents is defined differently you'd need to copy the style definition to a new style (Normal1, for example) then replace the id used for normal in the various Parts that make up the document with the id used for Normal1.

    Something I've never tried would be to rename the id and name for Normal so that you wouldn't need the last step. But you might have to create a Normal style using the "old" id and name as Word expects that to be in the document. (But you could try without, since Word might create it automatically without thinking the document is invalid).