3

There is need where I need to merge Word documents saved in a Document Library. I was looking for out of the box solution but did not come across one.

Please provide custom C# solution if there exists one.

Has any one succeed in implemention.

4
  • Dear Mancy, Have you developed above solution. kindly provide complete solution.
    – Pravin
    Nov 26, 2015 at 6:18
  • I am sorry to say that this is a client solution so i have to keep it confidential. I have provided the core functions to you and they are working. Nov 27, 2015 at 7:02
  • its working. Thank you.
    – Pravin
    Dec 10, 2015 at 15:33
  • Any proper solution to merge two documents located on SPO document. Oct 26, 2018 at 11:02

1 Answer 1

2

I am working on this kind of a solution currently. You will need to create a custom feature in order to achieve this. You can use the OpenXML DLL for this purpose. This DLL will allow you to merge the documents.

The following steps shall help you to develop:

  1. download the word files locally
  2. create a new word file that will hold the merged files.
  3. upload the merged file to the document library

Use the following code for merging the files:

 //creates word file -- accepts complete file path with name as param
 void createWordFile(string fileName) 
    {
        using (WordprocessingDocument document = WordprocessingDocument.Create(fileName, WordprocessingDocumentType.Document))
        {                
            MainDocumentPart mainPart = document.AddMainDocumentPart();
            mainPart.Document = new Document(new Body());                
        }
    }

    //merges files with page break
    //params : source and destination file paths and unique value for AltChunkId
    public void MergeDocumentWithPagebreak(string sourceFile, string destinationFile, string AltChunkID)
    {
        try
        {
            using (WordprocessingDocument myDoc = WordprocessingDocument.Open(sourceFile, true))
            {
                string altChunkId = AltChunkID;
                MainDocumentPart mainPart = myDoc.MainDocumentPart;
                //Append page break
                Paragraph para = new Paragraph(new Run((new Break() { Type = BreakValues.Page })));
                mainPart.Document.Body.InsertAfter(para, mainPart.Document.Body.LastChild);
                //Append file
                AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart(
                AlternativeFormatImportPartType.WordprocessingML, altChunkId);
                using (FileStream fileStream = File.Open(destinationFile, FileMode.Open))
                    chunk.FeedData(fileStream);
                AltChunk altChunk = new AltChunk();
                altChunk.Id = altChunkId;
                mainPart.Document.Body.InsertAfter(altChunk, mainPart.Document.Body.Elements<Paragraph>().Last());
                mainPart.Document.Save();
            }
        }
        catch (Exception ex)
        {
            lblError.Text = ex.Message;
        }

    }

    //uploads word file to the folder in the document set
    public void UploadFile(string newFileName, string folderURL)
    {
        SPFolder ds = SPContext.Current.Web.GetFolder(folderURL);

        using (FileStream strm = new FileInfo(newFileName).Open(FileMode.Open))
        {
            byte[] data = new byte[strm.Length];
            strm.Read(data, 0, (int)strm.Length);

            String fileToBeUploaded = System.IO.Path.GetFileName(newFileName);
            ds.Files.Add(fileToBeUploaded, data, true);
            ds.Update();
        }
    }

These functions will help you solve the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.