none
Merging Word 2010 Document and keep formating RRS feed

  • Question

  • Hi everyone,

    Id'like to merge Word 2010 (docm) documents together but i have a problem with the layout. id'like to keed the formating (page, Marging, font, etc...) for each document.

    I have inserted a SectionBreak for each Altchunk but it doesn't work correctly.

    Thank you for your help.

    There is the code:

     

      public static ReportStream MergeDocuments3(IList<Stream> filesToMerge) 
        {
    
              using (Stream packagedStream = new MemoryStream())
              {
                CopyStream(filesToMerge[0], packagedStream);
    
                filesToMerge.RemoveAt(0);
    
                using (WordprocessingDocument doc = WordprocessingDocument.Open(packagedStream, true))
                {
                  doc.ChangeDocumentType(WordprocessingDocumentType.MacroEnabledDocument);
    
                  //doc.MainDocumentPart.Document.Body.RemoveAllChildren<Paragraph>();
                  //doc.MainDocumentPart.Document.Body.RemoveAllChildren<SectionProperties>();
    
                  int count = 1;
                  int nbrDoc = filesToMerge.Count;
    
                  foreach (Stream doctomerge in filesToMerge)
                  {
                    var pageBreak = doc.MainDocumentPart.Document.LastChild;
    
                    AlternativeFormatImportPart inDocPart = doc.MainDocumentPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.WordprocessingML);
    
                    inDocPart.FeedData(doctomerge);
    
                    AltChunk altChunk = new AltChunk();
                    altChunk.Id = doc.MainDocumentPart.GetIdOfPart(inDocPart);
                    //Insert the altChunk tag after the paragraph
                    pageBreak.InsertAfterSelf(altChunk);
    
                    if (count != nbrDoc)
                    {
                      //var lst = doc.MainDocumentPart.Document.Descendants<AltChunk>().Last();
    
                      var lst = doc.MainDocumentPart.Document.LastChild;
    
                      lst.InsertBeforeSelf(new Paragraph(
                                  new ParagraphProperties(
                                      new SectionProperties()
                                  )
                                )
                        );
                    }
    
                    count++;
                  }//fin for each
    
                }//fin using
    
                return new ReportStream(packagedStream);
              }
    
        }
    
    Friday, April 22, 2011 11:49 AM

All replies

  • Dear eGm,

    Please take a look at these article about Assembing Multiple word documents:

    http://blogs.msdn.com/b/brian_jones/archive/2008/12/08/the-easy-way-to-assemble-multiple-word-documents.aspx

    http://blogs.msdn.com/b/ericwhite/archive/2008/10/27/how-to-use-altchunk-for-document-assembly.aspx

    Hope the two articles are helpful to you.

    Regards,


    Be happy.
    Wednesday, April 27, 2011 3:53 AM
  • Thanks for the answer.

    Altchunk it's the easiest way to merge documents, but the layout of the inserted documents aren't applied to the new word document and header/footer aren't respected.

    The header/footer of the first document it's applied to the other inserted documents.

    The idea it's to insert a section break to respect the header/footer of each inserted documents (that's working), but the layout (marging isn't respected).

    So i'm trying to get the last sectionProperties of the inserted document and creation a new sectionBreak with the same properties as marging,....

    Regards.

    P.S. : Sorry for the english :p

     

    Wednesday, April 27, 2011 3:46 PM
  • Hi eGm,

    Add the matchSrc element as a child of the altchunk property if you want the altchunk to preserve its own formatting.

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

    Thanks
    Ankush
    http://ankushbhatia.wordpress.com

     


    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

    Saturday, April 30, 2011 11:22 AM
  • Thank's for this solution.

    I have made this code but it doesn't work correclty, Marging and header/footer doesn't appear in the merged document.

     

    public static ReportStream MergeDocuments99(IList<Stream> filesToMerge)
        {
              using (Stream packagedStream = new MemoryStream())
              {
                CopyStream(filesToMerge[0], packagedStream);
    
                filesToMerge[0].Seek(0, SeekOrigin.Begin);
    
                using (WordprocessingDocument doc = WordprocessingDocument.Open(packagedStream, true))
                {
                  doc.MainDocumentPart.Document.Body.RemoveAllChildren();
    
                  bool flag = false;
    
                  foreach (Stream doctomerge in filesToMerge)
                  {
                    AlternativeFormatImportPart inDocPart = doc.MainDocumentPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.WordprocessingML);
    
                    inDocPart.FeedData(doctomerge);
    
                    AltChunk altChunk = new AltChunk( new AltChunkProperties(
                              new MatchSource(){ Val = new OnOffValue(){ Value = true }}
                            ) 
                      
                    );
                    altChunk.Id = doc.MainDocumentPart.GetIdOfPart(inDocPart);
    
    
                    if (flag)
                    {
                      var elem = doc.MainDocumentPart.Document.Descendants<AltChunk>().Last();
                      elem.InsertAfterSelf(altChunk);
                    }
                    else
                    {
                      var elem2 = doc.MainDocumentPart.Document.Body;
                      elem2.InsertAfterSelf(altChunk);
                      flag = true;
                    }
                           
                  }//fin for each
    
                }//fin using
    
                return new ReportStream(packagedStream);
              }
    
    

     

    Tuesday, May 3, 2011 9:07 AM