none
Merging Documents still not solved RRS feed

  • Question

  • Hi,

    I need to merge OpenXML-Documents to one single Document. I have a couple of single docx-Documents. Each document contains it's own header, footer and body.

    I need to merge the documents including their individual headers, footers, styles and everything that is needed.

     

    Has anybody been solving this (big) issue, so far and can provide some sample code of how to solve this?

    Thursday, November 4, 2010 1:51 PM

All replies

  • Hi,

    There are only 2 way to do the merging

    1. Use altChunk

    2. Write your own app

    There are a lots of articles available to explain about AltChunk.

    Regarding the second one, have a look at :http://blogs.msdn.com/b/ericwhite/archive/2009/03/19/announcing-the-release-of-powertools-for-open-xml-v1-1.aspx. Download and look for the sample which is written for the merging (Merge-OpenXmlDocument Cmdlet)

    Thanks
    Ankush


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. Regards, Ankush Microsoft Online Community Support
    • Proposed as answer by Ankush_Bhatia Thursday, November 4, 2010 2:07 PM
    • Unproposed as answer by SPDeveloperXP Thursday, November 4, 2010 2:16 PM
    Thursday, November 4, 2010 2:05 PM
  • Hi Ankush,

    what I could find so far is adding an image or text (for instance) into AltChunks. But I could not find anything about how to add the entire document including the header and footer. I don't want to add text into a predefined template. I have a lot of single template documents (*.docx) and each of these documents can have it's own header and footer. In the final document the layout should be retained, e.g. when I merge Doc1.docx and Doc2.docx (with 1 page each), the first page should have the exact layout (including header and footer) of Doc1.docx and the second page should have the layout of (including header and footer) of Doc2.docx.

    I don't know if this is possible with AltChunks?

    First of all, how can I add AltChunks with Word 2003 (compatibility pack installed) so that they can be filled with my individaul docx-Documents including their individual layout?

    Thursday, November 4, 2010 2:15 PM
  • Hi Ankush,

    I started experimenting with the following example to merge two Documents (3.docx into 1.docx). These documents have been merged successfully. Unfortunately there is one thing, I am confused about: 3.docx has a Header and Footer that already contains text. 1.docx not. When I open the merged document (1.docx), I can't see the text in the header and footer of page 2 (which is from 3.docx).

    Why is this? What's wrong with my little example sourcecode that prevents this header/footer from beeing displayed??

    Here is the sample code:

    private void MergeDocuments()
    {
      string targetDocument = @"C:\1.docx";
      string embeddedDocument = @"C:\3.docx";
    
      using (WordprocessingDocument doc = WordprocessingDocument.Open(targetDocument, true))
      {
        // Get the last paragraph in the document
        Paragraph paragraph = doc.MainDocumentPart.Document.Descendants<Paragraph>().Last();
            
            
    
        // Create a new Alternative Format part
        AlternativeFormatImportPart inDocPart = doc.MainDocumentPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.WordprocessingML);
    
        // Open file to be inserted and stream its contents into the part
        using (FileStream stream = new FileStream(embeddedDocument, FileMode.Open))
        {
          inDocPart.FeedData(stream);
        }
    
        // Create a AltChunk - <w:altChunk> and give the reltionship if linking it to the
        // alternative format part we added above
        AltChunk altChunk = new AltChunk();
        altChunk.Id = doc.MainDocumentPart.GetIdOfPart(inDocPart);
    
        // Insert the altChunk tag after the paragraph
        paragraph.InsertAfterSelf(altChunk);
    
        // Save the document
        doc.MainDocumentPart.Document.Save();
      }
    }
    

    Thursday, November 4, 2010 3:42 PM
  • Hi,

    altChunk can insert an entire document at a specified point. Please have a look at :http://blogs.msdn.com/b/ericwhite/archive/2009/04/19/comparison-of-altchunk-to-the-documentbuilder-class.aspx. Point to be noted that you can merge these legacy binary files without converting them to Open XML. Take a look at the following article: http://blogs.msdn.com/brian_jones/archive/2008/12/08/the-easy-way-to-assemble-multiple-word-documents.aspx

    The example uses docx as the altchunk, but you can change the code to point to binary documents. All you have to do is change the content type of the altchunk. I believe the binary content type is: application/msword.

    As I mentioned, there are 2 options to do it:

    1. Use altChunks – Check out http://blogs.msdn.com/brian_jones/archive/2008/12/08/the-easy-way-to-assemble-multiple-word-documents.aspx
    2. Manually merge the documents together using Open XML Power Tools – Check out http://blogs.msdn.com/ericwhite/archive/2009/03/19/announcing-the-release-of-powertools-for-open-xml-v1-1.aspx

    Both of these options have the ability to either match destination formatting or keep source formatting. If you want to work with different header/footers, have a look at the second approch

    Thanks
    Ankush

     


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. Regards, Ankush Microsoft Online Community Support
    Thursday, November 4, 2010 3:44 PM
  • Hi Ankush,

    thanks for your help and the links. The first two articles are good to understand the differences and get more Background Information on that topic.

    So, if I understand it correctly the target document is repsonsible the layout of the altChunk. In my case 1.docx would determine the layout for the document.

    What I need to have is the following (by merging 3.docx into 1.docx using altChunks):

    ------------------------

    header 1.docx
    content 1.docx
    footer 1.docx

    page break

    header 3.docx
    content 3.dox
    footer 3.docx

    ------------------------

     

    What I get right now is the following

    ------------------------

    header 1.docx
    content 1.docx
    footer 1.docx

    page break

    header 1.docx
    content 3.dox
    footer 1.docx

    ------------------------

     

    Unfortunately, I don't know what you mean by changing the altChunk to Binary Data. What option do I have to change in the example above to turn the altChunk to a Binary altChunk?

     

    I also looked on http://blogs.msdn.com/brian_jones/archive/2008/12/08/the-easy-way-to-assemble-multiple-word-documents.aspx but was wondering how I can insert the mentioned "Content Controls". I am using the Word 2003 (with Compatibility Pack). I do not have a clue how to get them into my documents.

    Thursday, November 4, 2010 5:11 PM
  • Hey,

    can anybody help me on that problem, or is there no solution out there??

    Monday, November 8, 2010 8:29 AM
  • Hi,

    Content Controls are meant to be work with Word 2007 or higher version only. They don’t work with Compatibility Pack.

    I think you need to write your own merger utility to achieve the results you are looking for.

    Thanks,
    Ankush

     


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. Regards, Ankush Microsoft Online Community Support
    Monday, November 8, 2010 12:23 PM
  • Hi,

    do you mean AltChunks? Or would this be a possibility?

    What do you mean by own Merger-Utility? I looked at your link to manually merge documents together, but I am not sure if I can use these cmdlets somehow in C# to merge the documents. If "manually merge documents" together you means copying one part from a source XML to my target-document, the question is, how do I get the corresponding formats?

    Using AltChunks was partly successful with my source code fragment above. If I knew how to get the right format for my second page, I would be satisfied.

    By manually copying my documents together (via Word), I can get whant I want. Therefore I navigate to the end of my target document and add a "Section Break". Then I copied everything from my source document and added it right after the section break (2nd page).

    I wonder how this could be realized with the OpenXML SDK.

     

    The sample source above is similar to that, as it is looking for the last paragraph in the target document und adding a AltChunkt right after it. Inserting the source document works good with it. Only formatting is the problem. Why doesn't Word take the format of the source document as it is done when I manually add a section break in Word and copy the entire content of my source document?

    Monday, November 8, 2010 4:40 PM
  • A bit late perhaps.

     

    Create an AltChunkProperty and append it to the AltChunk. In the altChunkProperty you can use the MatchSource class

     

    Code example:

    AltChunk altChunk = new AltChunk();
    AltChunkProperties acp = new AltChunkProperties() { MatchSource = new MatchSource() { Val = OnOffValue.FromBoolean(true) } };
    altChunk.Append(acp);
    

    http://msdn.microsoft.com/en-us/library/documentformat.openxml.wordprocessing.altchunk.aspx

    http://msdn.microsoft.com/en-us/library/documentformat.openxml.wordprocessing.altchunkproperties.aspx

    http://msdn.microsoft.com/en-us/library/documentformat.openxml.wordprocessing.matchsource.aspx <-- the most important link with explanation of how to use it.

    Monday, August 29, 2011 7:28 PM
  • for merging document, have a try to use Spire.Doc, merge document so easy, use following code ,

       string fileName = OpenFile();
        string fileMerge = OpenFile();
        if ((!string.IsNullOrEmpty(fileName)) && (!string.IsNullOrEmpty(fileMerge)))
        {
            //Create word document
            Document document = new Document();
            document.LoadFromFile(fileName,FileFormat.Doc);
    
            Document documentMerge = new Document();
            documentMerge.LoadFromFile(fileMerge, FileFormat.Doc);
    
            foreach( Section sec in documentMerge.Sections)
            {
                document.Sections.Add(sec.Clone());
            }
    
            //Save doc file.
            document.SaveToFile("Sample.doc", FileFormat.Doc);
    
            //Launching the MS Word file.
            WordDocViewer("Sample.doc");
        }
    
    

    Friday, September 2, 2011 2:37 AM