Copy Paragraph with Numbered list

Home Forums Open-Xml-Sdk Copy Paragraph with Numbered list

This topic contains 9 replies, has 3 voices, and was last updated by  Naveed Ahmad 11 months ago.

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
  • #2469


    Hello Eric,
    I have been trying to copy a paragraph with numbered list from one word document into a new word document using Document builder. The paragraph copies fine, but the numbering sequence is all messed up. I can’t wrap my head around on how to simply copy the Numbering part of the source to the destination document.
    For example:
    Source doc-
    1. City A
    * County A
    i. Street A
    2. City B
    * County B
    i. Street B

    Destination doc-
    1. City A
    2. County A
    3. Street A
    4. City B
    5. County B
    6. Street B

    I went through your WordprocessingML numbering and Heading numbering posts. But still, am not able to figure out, how to simply bring in the paragraph and numbering the way it is in the source document.

    • This topic was modified 2 years, 1 month ago by  Ashish. Reason: Clarity

    Eric White

    The problem here is that I believe that DocumentBuilder is pulling the definition of the numbered list from an earlier document in the list of sources. There are multiple ways that numbering can be set up in a document, and one of the ways is that numbering is applied to styles, i.e. Heading1, Heading2, Heading3 have numbering applied to them. Is this the case with your documents?

    Depending on your use case, there is an easy way to resolve this, which is to use FormattingAssembler to create a new document where numbering is ‘flattened’. The resulting document does not use numbering, and instead, there is a new run that contains the correct list item. You then use the ‘flattened’ document as your source(s) for DocumentBuilder.

    So to debug this further, I need to know if in fact you have multiple documents in your list of sources, and what kind of numbered lists you are using.

    I haven’t yet determined how to enable attaching files to forum posts using bbPress. Can you upload your source documents (simplified if possible) to OneDrive or DropBox, and then post the link?

    Cheers, Eric



    Thank you for the prompt response Eric.

    I didn’t properly explain the scenario to you, and I apologize for that.
    Both the source (Doc A) and destination (Doc B) live in SharePoint. The code is configured in a web part and is executed at a list item menu click.

    Doc B is created from a Template document which also lives in SharePoint in a doc library. The code creates Doc B from the Template, copies content of Doc A and inserts into Doc B (specific section -Row 2 of a table). Everything works fine, except for the numbering. It of course is not copied from the source.

    As a manual test, after Doc B (DocB.docx) is created I opened it in Visual studio using PowerTools for VS, opened numbering.xml and deleted everything inside w:abstractNum. Then I opened Doc A in VS and copied everything inside the w:abstractNum and pasted in the w:abstractNum section of Doc B. The numbering in Doc B looked the same as in the Doc A when opened in MS Word. It worked!

    So, I was wondering if the same could be achieved with code- check if Doc A has numberingpart, if Yes then copy its abstractNum content from numbering.xml, and replace it with the abstractNum content of Doc B.

    Banging my head on this for the better part of last week, still clueless and even more dazed than when I had begun!

    • This reply was modified 2 years, 1 month ago by  Ashish.

    Eric White

    Trust me, I fully understand how complicated numbering is. I wrote an MSDN article, Working with Numbered Lists in Open XML WordprocessingML. I then wrote the ListItemRetriever module which is a reference implementation of retrieving list items, and then eventually re-wrote it again. I suppose I have spent three full months working with numbering, so believe me, I know.

    Copying the numbering part from one document to another is not a good approach. It might work in this specific scenario, but has the potential to break the document far too easily.

    The problem you are facing is that the template document does not contain the definition for the numbering in Doc A – instead, it has some conflicting numbering defined for that particular numbering ID. (Numbering ids are the key identifier – abstract numbering is a level of indirection that enables reuse of the core numbering definition in multiple numIds.)

    Here is an easy way to solve your problem.

    When you set up your list of sources for DocumentBuilder, first include the numbered content at the beginning of the document. This should be the first source in your list of sources. Make a note of the number of paragraphs that you have inserted. This will pull in the numbering definitions from Doc A.

    Then set up the remainder of your sources exactly as you are currently doing, and fire off DocumentBuilder. This will create a document with the correct numbering, in row 2 of your table.

    Now, after DocumentBuilder is finished, open up the resulting document and delete those extra paragraphs that you have inserted at the beginning of the document. This is easy code to write, because you have saved away the number of paragraphs that you have inserted.

    Then you will have a document that looks like what you want.



    “When you set up your list of sources for DocumentBuilder, first include the numbered content at the beginning of the document. This should be the first source in your list of sources. Make a note of the number of paragraphs that you have inserted. This will pull in the numbering definitions from Doc A.”

    Please pardon my ignorance. IF possible, could you please show this in code example?


    Eric White

    I’ll see what I can do. I have some personal commitments over the next 3 days.

    My idea is to create a new Open-Xml-PowerTools example, which contains two documents with conflicting numbering styles, where we want to take the numbering definitions from the second document in the sources.

    Have you looked at all of the existing DocumentBuilder examples, and understand setting up sources for DocumentBuilder? It would be worthwhile to go into the four examples, play with them, changing the sources, changing the number of paragraphs in each source, and so on.



    It seems I might be able to use FormattingAssembler for my problem. However, my work computer has VS 2010 and I couldn’t find the OpenXMLPowerTools version that is compatible with VS 2010. Is there a way to make the existing version compatible to VS 2010? Or some other work around you suggest!



    Hey Eric,
    It’s me again! Well I was able to solve the problem using FormattingAssembler, as you had suggested. The flattened content resembles source content, with numbers and bullets, and the client is happy with that.
    Thank you for your help and keep doing what you’re doing!


    Eric White

    Awesome! I’m glad your issue is resolved.


    Naveed Ahmad

    Hey Ashish,

    I am facing the same issue can you please share the code of your solution.

Viewing 10 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic.