views:

187

answers:

2

I've to merge multiple DOCX files (created with DocX Package) into one big file. I've managed to merge multiple files already but I fail to merge Headers and Footers (they differ between files, pages). I've tried both DocX package and MS Office COM Interop and it simply doesn't want to merge the headers / footers. They are skipped.

Does anyone can provide solution that would work? Can be anything really (including conversion to PDF using PDFCreator COM, or Interop or DocX).

I've also tried PDFCreator Sample in C# and it converts nicely one document to PDF but I don't know how to provide multiple documents to it so it would create one big PDF. So if anyone can provide a good (free) way it will be appreciated.

This is what i use:

internal static class DocumentsMerging {
    private static object missing = Type.Missing;
    private static ApplicationClass wordApplication { get; set; }
    private static void addDocument(object path, Document doc, bool firstDocument) {
        object subDocPath = path;
        var subDoc = wordApplication.Documents.Open(ref subDocPath, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
        try {
            if (!firstDocument) {
                insertPageBreak(doc);
            }
            object docStart = doc.Content.End - 1;
            object docEnd = doc.Content.End;
            object start = subDoc.Content.Start;
            object end = subDoc.Content.End;
            Range rng = doc.Range(ref docStart, ref docEnd);
            rng.FormattedText = subDoc.Range(ref start, ref end);
            //if (!lastDocument) {
            //}
        } finally {
            subDoc.Close(ref missing, ref missing, ref missing);
        }
    }
    public static bool deleteFile(string fileName) {
        if (File.Exists(fileName)) {
            try {
                File.Delete(fileName);
                if (File.Exists(fileName)) {
                    return false;
                }
                return true;
            } catch (IOException) {
                DialogResult result = MessageBox.Show(new Form {TopMost = true}, "Plik " + fileName + " jest w użyciu lub nie masz uprawnień do zapisania raportu w tym miejscu. Czy chcesz spróbować ponownie?", "Błąd zapisu (000002)", MessageBoxButtons.YesNo, MessageBoxIcon.Stop);
                if (result == DialogResult.No) {
                    return false;
                }
                if (deleteFile(fileName)) {
                    return true;
                }
            } catch (Exception e) {
                MessageBox.Show(new Form {TopMost = true}, "Plik " + fileName + " nie może zostać skasowany. Błąd " + Environment.NewLine + Environment.NewLine + e, "Błąd zapisu (000003)", MessageBoxButtons.YesNo, MessageBoxIcon.Stop);
                return false;
            }
        } else {
            return true;
        }
        return false;
    }
    public static void documentsMerge(object fileName, List<string> arrayList) {
        // object fileName = Path.Combine(Environment.CurrentDirectory, @"NewDocument.doc");
        bool varTest = deleteFile(fileName.ToString());
        if (varTest) {
            try {
                wordApplication = new ApplicationClass();
                var doc = wordApplication.Documents.Add(ref missing, ref missing, ref missing, ref missing);
                try {
                    doc.Activate();
                    int count = 0;
                    foreach (var alItem in arrayList) {
                        addDocument(alItem, doc, count == 0);
                        count++;
                    }
                    // addDocument(@"D:\Projects\WordTests\ConsoleApplication1\Documents\Doc1.doc", doc ) ; //, false);
                    // addDocument(@"D:\Projects\WordTests\ConsoleApplication1\Documents\Doc2.doc", doc ) ; //, true);
                    doc.SaveAs(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
                } finally {
                    doc.Close(ref missing, ref missing, ref missing);
                }
            } finally {
                wordApplication.Quit(ref missing, ref missing, ref missing);
            }
        } else {
            return;
        }
    }
    private static void insertPageBreak(Document doc) {
        object docStart = doc.Content.End - 1;
        object docEnd = doc.Content.End;
        Range rng = doc.Range(ref docStart, ref docEnd);
       // object pageBreak = WdBreakType.wdPageBreak;
        object pageBreak = WdBreakType.wdSectionBreakNextPage;

        rng.InsertBreak(ref pageBreak);
    }
}
+2  A: 

Check this out http://devpinoy.org/blogs/keithrull/archive/2007/05/23/how-to-merge-multiple-microsoft-word-documents-in-c.aspx

Change this line:

object pageBreak = Word.WdBreakType.wdPageBreak;

to this

object pageBreak = Word.WdBreakType.wdSectionBreakNextPage;

To get the heading and footer from the second document.

Adding in the entire method and how to call it in case the source changes.

The Class:

using System;
using Word = Microsoft.Office.Interop.Word;
using System.Configuration;

namespace KeithRull.Utilities.OfficeInterop
{
  public class MsWord
  {
    /// <summary>
    /// This is the default Word Document Template file. I suggest that you point this to the location
    /// of your Ms Office Normal.dot file which is usually located in your Ms Office Templates folder.
    /// If it does not exist, what you could do is create an empty word document and save it as Normal.dot.
    /// </summary>
    private static string defaultWordDocumentTemplate = @"Normal.dot";

    /// <summary>
    /// A function that merges Microsoft Word Documents that uses the default template
    /// </summary>
    /// <param name="filesToMerge">An array of files that we want to merge</param>
    /// <param name="outputFilename">The filename of the merged document</param>
    /// <param name="insertPageBreaks">Set to true if you want to have page breaks inserted after each document</param>
    public static void Merge(string[] filesToMerge, string outputFilename, bool insertPageBreaks)
    {
        Merge(filesToMerge, outputFilename, insertPageBreaks, defaultWordDocumentTemplate);
    }

    /// <summary>
    /// A function that merges Microsoft Word Documents that uses a template specified by the user
    /// </summary>
    /// <param name="filesToMerge">An array of files that we want to merge</param>
    /// <param name="outputFilename">The filename of the merged document</param>
    /// <param name="insertPageBreaks">Set to true if you want to have page breaks inserted after each document</param>
    /// <param name="documentTemplate">The word document you want to use to serve as the template</param>
    public static void Merge(string[] filesToMerge, string outputFilename, bool insertPageBreaks, string documentTemplate)
    {
        object defaultTemplate = documentTemplate;
        object missing = System.Type.Missing;
        object pageBreak = Word.WdBreakType.wdSectionBreakNextPage;
        object outputFile = outputFilename;

        // Create  a new Word application
        Word._Application wordApplication = new Word.Application( );

        try
        {
            // Create a new file based on our template
            Word.Document wordDocument = wordApplication.Documents.Add(
                                          ref missing
                                        , ref missing
                                        , ref missing
                                        , ref missing);

            // Make a Word selection object.
            Word.Selection selection = wordApplication.Selection;

            //Count the number of documents to insert;
            int documentCount = filesToMerge.Length;

            //A counter that signals that we shoudn't insert a page break at the end of document.
            int breakStop = 0;

            // Loop thru each of the Word documents
            foreach (string file in filesToMerge)
            {
                breakStop++;
                // Insert the files to our template
                selection.InsertFile(
                                            file
                                        , ref missing
                                        , ref missing
                                        , ref missing
                                        , ref missing);

                //Do we want page breaks added after each documents?
                if (insertPageBreaks && breakStop != documentCount)
                {
                    selection.InsertBreak(ref pageBreak);
                }
            }

            // Save the document to it's output file.
            wordDocument.SaveAs(
                            ref outputFile
                        , ref missing
                        , ref missing
                        , ref missing
                        , ref missing
                        , ref missing
                        , ref missing
                        , ref missing
                        , ref missing
                        , ref missing
                        , ref missing
                        , ref missing
                        , ref missing
                        , ref missing
                        , ref missing
                        , ref missing);

            // Clean up!
            wordDocument = null;
        }
        catch (Exception ex)
        {
            //I didn't include a default error handler so i'm just throwing the error
            throw ex;
        }
        finally
        {
            // Finally, Close our Word application
            wordApplication.Quit(ref missing, ref missing, ref missing);
        }
    }
}
}

And the Call:

using System;
using KeithRull.Utilities.OfficeInterop;

namespace WordDocMerge2
{
  class Program
  {
    static void Main(string[] args)
    {
        try
        {
            string document1 = @"D:\Visual Studio Projects\31.docx";
            string document2 = @"D:\Visual Studio Projects\33.docx";
            string document3 = @"D:\Visual Studio Projects\32.docx";

            string[] documentsToMerge = { document1, document2, document3 };

            string outputFileName = String.Format("D:\\Visual Studio Projects\\{0}.docx", Guid.NewGuid( ));

            MsWord.Merge(documentsToMerge, outputFileName, true);

        }
        catch (Exception ex)
        {
            //messageLabel.Text = ex.Message;
        }
    }
}
}
darkstar3d
Unfortunately this doesn't merge the footers and headers from what I remember since I remember trying it some time ago.
MadBoy
Ok, I will tinker around with that solution to see if it can be modified to do so.
darkstar3d
Sorry for the delay, work caught up with me. If you change the pagebreak line in the class of the solution I linked too, that will fix it for you. Change it to the following: object pageBreak = Word.WdBreakType.wdSectionBreakNextPage;
darkstar3d
I've attached to my post code I'm using. I've changed pageBreak to what you described and it seems to not change anything. I have picutres as headers/footers. Maybe that's the problem?
MadBoy
The code I linked to with the modification does the picture header as I tested that yesterday. I didn't with the footer. I won't have time to exam the posted code until later today.
darkstar3d
I've tried your code and if i put doc template with header and attach documents to it it works (in the way that the header and footer are used from template). However if i remove header and footer from template and attach documents with headers and footers those are ignored. More is that each of my document may or may not have different header (picture in header) which means the document needs to have possibility for different header and footer for each document/page.
MadBoy
Thats odd. I used the normal template which is a blank document on my system and adding two docx, each with different header and footers. I will need to create a template in order to recreate your scenario.
darkstar3d
I also adding picture header and footer and it worked as well. Which version of Word are you using?
darkstar3d
I also just realize while creating the solution at home that I posted the earlier link for the class, not the latest. I was using the latest at work. http://devpinoy.org/blogs/keithrull/archive/2007/06/09/updated-how-to-merge-multiple-microsoft-word-documents.aspx
darkstar3d
I am using office 2010 x64, but at work they use office 2007. I'll try your new code.
MadBoy
I've looked at the code and there's no much change to the original (besides assigning normal.dot file to webconfig. For WinForms apps not much changes i guess.
MadBoy
If needed i can send you 3 docx files that I try to merge
MadBoy
darkstar3d
Ok, send me the files and try the solution. stacyspear[at]gmail.com
darkstar3d
I've not had a chance to test it yet but you have put much effort into this. So here you go. Your points.
MadBoy
Let me know if it doesn't work for you, but it did for me with your files. Good luck!
darkstar3d
Thanks it seems to work :-) For the sake of StackOverflow maybe post the whole class in here. You never know how long the external sources will be available and it's always good to keep stuff like that :-)
MadBoy
Good idea, I'll do that later.
darkstar3d
A: 

Have you tried to merge at xml level ? Docx are zip files with Xml files inside.

You may be able to merge the XML using XDocument.

olifozzy
I have not. I don't really know how so would be really great if you can provide a way to do so ;)
MadBoy