Merging word documents

Home Forums Open-Xml-PowerTools Merging word documents

This topic contains 1 reply, has 2 voices, and was last updated by  Eric White 7 years, 6 months ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #3842

    mx
    Participant

    I’m trying to merge two word documents and i want to keep their footers, headers and styles in the new one. So far i tried this piece of code.

    string source1 = “C:\\Users\\Administrator\\Desktop\\word.docx”;
    string source2 = “C:\\Users\\Administrator\\Desktop\\word2.docx”; //Document to insert
    List<Source> sources = null;
    sources = new List<Source>()
    {
    new Source(new WmlDocument(source1), true),
    new Source(new WmlDocument(source2), true)
    };
    DocumentBuilder.BuildDocument(sources, “C:\\Users\\Administrator\\Desktop\\word3.docx”);

    It worked and merged them. But i have some problems.
    -Font size is derived from the first document. Let’s say word.docx’s font size is 12 and word2.docx’s font size is 11 for the whole document. Then word3.docx is created with font size 12. If word2.docx is first 1 in list then new font size will be 11.
    -Spacing is also derived from the first document.

    Is there a way to keep both styles in the new document?
    I have one cover page and i want to add this page to many duments. So far couldn’t do it properly.

    Thanks in advance.

    • This topic was modified 7 years, 6 months ago by  mx.
    #3878

    Eric White
    Keymaster

    Hi,

    One constraint about DocumentBuilder is that it merges styles, and takes the first style in the list of source documents. If you want to keep both styles in both documents, then you will need to rename the style in one of them. You can write a small program to do this – not hard. Need to change the style name in the document.xml part, and also in the styles.xml part.

    Be aware that if you have other styles based on the style you are renaming, then the reference from the derived style also needs to be updated. But most probably you are not facing this situation.

    At the time I designed DocumentBuilder, I made this decision – it more or less meets peoples needs, but occasionally, we will find an issue such as this. Renaming styles before merging should solve your issue.

    If you want to see what needs to be done to rename styles, use the approach shown in screen-cast #13 in the following series:

    Introduction to Open XML

    Cheers, Eric

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

You must be logged in to reply to this topic.