none
Open Xml combining multiple word document into one RRS feed

  • Question

  • Hi

    I have written below code the combine multiple word DOCX to single one.

    But the merged document has lot of space and if i merge 2 - 24 page document into one it is not produced as 48 pages.

    because of extra spaces it appends, it becomes 82 pages somehow.

    Please advice and help on this. Thanks, Vikas

    private static void MergeWordDocs()
    {
        string sourceFolder = @"C:\Test\MergeDocs\";
        string targetFolder = @"C:\Test\";
    
        string altChunkIdBase = "acID";
        int altChunkCounter = 1;
        string altChunkId = altChunkIdBase + altChunkCounter.ToString();
    
        MainDocumentPart wdDocTargetMainPart = null;
        Document docTarget = null;
        AlternativeFormatImportPartType afType;
        AlternativeFormatImportPart chunk = null;
        AltChunk ac = null;
    
        using (WordprocessingDocument wdPkgTarget = WordprocessingDocument.Create(targetFolder + "finalDoc.docx", DocumentFormat.OpenXml.WordprocessingDocumentType.Document, true))
        {
            wdDocTargetMainPart = wdPkgTarget.MainDocumentPart;
            if (wdDocTargetMainPart == null)
            {
                wdDocTargetMainPart = wdPkgTarget.AddMainDocumentPart();
                Document wdDoc = new Document(
                    new Body(
                        new Paragraph(                    
                            ),                    
                        new SectionProperties(
                        new SectionType() { Val = SectionMarkValues.NextPage },
                        new PageNumberType { Start = 1 },
                        new SpacingBetweenLines() { After = "0"}
                            )
                            )
                        );
                wdDocTargetMainPart.Document = wdDoc;
            }
            docTarget = wdDocTargetMainPart.Document;
            SectionProperties secPropLast = docTarget.Body.Descendants<SectionProperties>().Last();
            SectionProperties secPropNew = (SectionProperties)secPropLast.CloneNode(true);
    
            Paragraph lastParaTarget = (Paragraph)docTarget.Body.Descendants<Paragraph>().Last();
            ParagraphProperties paraPropTarget = lastParaTarget.ParagraphProperties;
            if (paraPropTarget == null)
            {
                paraPropTarget = new ParagraphProperties();
            }
            paraPropTarget.Append(secPropNew);
    
            lastParaTarget.InsertAt(paraPropTarget, 0);
            SpacingBetweenLines spacing = new SpacingBetweenLines() { After = "0" };
            ParagraphProperties paraProps = null;
            System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(sourceFolder);
            IEnumerable<System.IO.FileInfo> docFiles = di.EnumerateFiles();
            foreach (System.IO.FileInfo fi in docFiles)
            {
                using (WordprocessingDocument pkgSourceDoc = WordprocessingDocument.Open(fi.FullName, true))
                {
                    IEnumerable<HeaderPart> partsHeader = pkgSourceDoc.MainDocumentPart.GetPartsOfType<HeaderPart>();
                    IEnumerable<FooterPart> partsFooter = pkgSourceDoc.MainDocumentPart.GetPartsOfType<FooterPart>();
    
                    if (partsHeader.Count() > 0 || partsFooter.Count() > 0)
                    {
                        Body sourceBody = pkgSourceDoc.MainDocumentPart.Document.Body;
                        SectionProperties docSectionBreak = sourceBody.Descendants<SectionProperties>().Last();
                        SectionProperties copySectionBreak = (SectionProperties)docSectionBreak.CloneNode(true);
                        Paragraph lastpara = sourceBody.Descendants<Paragraph>().Last();
                        paraProps = lastpara.ParagraphProperties;
                        if (paraProps == null)
                        {
                            paraProps = new ParagraphProperties();
                               
                            lastpara.Append(paraProps);
                        }
                        paraProps.Append(copySectionBreak);
                    }
                    pkgSourceDoc.MainDocumentPart.Document.Save();
                }
    
                afType = AlternativeFormatImportPartType.WordprocessingML;
                chunk = wdDocTargetMainPart.AddAlternativeFormatImportPart(afType, altChunkId);
                System.IO.FileStream fsSourceDocument = new System.IO.FileStream(fi.FullName, System.IO.FileMode.Open);
                chunk.FeedData(fsSourceDocument);
                ac = new AltChunk();
                ac.Id = altChunkId;
                    
                docTarget.Body.InsertAfter(ac, docTarget.Body.Descendants<Paragraph>().Last());
                docTarget.Save();
                altChunkCounter += 1;
                altChunkId = altChunkIdBase + altChunkCounter.ToString();
                chunk = null;
                ac = null;
            }
        }
    
    }

    • Moved by Fred Bao Tuesday, September 9, 2014 7:19 AM
    Monday, September 8, 2014 9:06 PM

All replies

  • Hello VKK,

    I move this thread to the OpenXML forum for getting better help.

    Thanks for your understanding.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, September 9, 2014 7:18 AM
  • Hi,

    I tested your method on my machine(Win8.1 X64 with Visual Studio 2013, Office 2013, OpenXML SDK 2.5), and it worked just fine.

    >>But the merged document has lot of space and if i merge 2 - 24 page document into one it is not produced as 48 pages.

    >>because of extra spaces it appends, it becomes 82 pages somehow.

    I used multiple word documents that contains tens of pages to test this method, the only thing I could see was that the first page was blank, that's because you insert a pagebreak at the top of the document in the code.  The total page count of the finalDoc.docx will be smaller than the total page count of all the original documents, that's because you don't insert pagebreak at the end of the merged document.

    I didn't see any space in the merged document. Probably you could firstly use some simple documents to test it, then use the big documents after. Check if there're any special parts in those big documents, e.g. spaces in the header or footer. You could also upload those documents in OneDrive so that I can reproduce this problem.

    These related links may give you some help:

    The Easy Way to Assemble Multiple Word Documents

    Merging Word documents using the Open XML SDK


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, September 11, 2014 8:17 AM
    Moderator
  • For me it is the .Last() method that is giving me trouble. It gives a "No Extension method 'Last'" for the IEnumerable<Paragraph>

    I am using OpenXML 2.5    what can I do to work around this?

     
    Thursday, October 10, 2019 2:03 PM