Patric Patric - 7 days ago 6
C# Question

OpenXML - Bullet Points get transformed to numbered list when merging documents

I have two documents that I try to merge partially.

I take some parts from document A that are inside a RTF content control and copy all child elements of the

sdtContent
of the content control to another
sdtContent
in document B using AppendChild and CloneNode(true) (deep cloning):

foreach (var srcChildElement in sourceDocumentSdtContentBlockNode.ChildElements)
{
targetSdtContentBlock.AppendChild(srcChildElement.CloneNode(true);
}


The problem is, that if the content (list items) in my source document A is formatted with bullet points the result in document B will be numbered list items.

Why isn't the resulting style like in the source when I clone it? I thought it would just keep its style as I don't manipulate it. In document B there seems also no to be a formatting issue. I can manually insert bullets or a numbered list without a problem.

I even created two completly new word documents and the same thing happend, so it is surely not some issue with the existing files.

Update:
I found out that there is a separate numbering definition in a document (https://msdn.microsoft.com/en-us/library/office/ee922775(v=office.14).aspx) which is not part of the node I copy. When I clone the node this information is not included at it is in a separate numbering definition. Is there a way to copy a node to a new document and tell it to retain the numbering styles from the numbering definition? The other option would be to manually check if the cloned node contains a
numPr
element and if yes to extract also the definition (including changing and reassigning the IDs) - would prefer a less complex way to copy an element and hope there is one :-)


Any ideas?

Answer

Well, I did manage to solve this. As already mentioned the Numbering information is stored in a separate part (or file inside the document zip): https://msdn.microsoft.com/en-us/library/office/ee922775(v=office.14).aspx

From the NumberingDefinitions (in NumberingPart) of the source I copied the necessary (where there is a numPr inside a paragraph). To the target file by lookup up the numId of the numPr.

I cloned the num element and also the corresponding abstractNum element to the target. Doing this I also replaced the IDs (incremented all by 1 using the existing element IDs in the target) like numId, abstractNumId and nsid (hex value) to make sure there won't be any conflicts.

Was a bit work but very doable.

Comments