linkedinfacebooktwittermenuarrow-up

GemBox Support Forum

Combine .doc and .docx files

Hello all,

I am working on a task of merging different .doc and .docx files in one docx result file.

I faced some issues:

  • In result file, numbered items should be ordered in the result file, but it is not the case with items from .doc and .docx files.

  • As input I recieve heder and footer in one file, I want to make it repeated in each page in result file, what is the best way to do this, knowing that each section in output is one input file.

below is the code I used.

Thank you in advance.

        Dictionary<string, string> dict = new Dictionary<string, string>();
        Console.WriteLine("Get path of merge fields File");
        string chamsDeFusion = Console.ReadLine();           
        dict = ReadFieldMergeFile(chamsDeFusion);
        int j = 1;
        
        foreach (var file in fileArray)
        {
            var section = new Section(destination);
            var source = DocumentModel.Load(file);
            
            // Set list number
            int i = SetListNumber(source, j);
            j = i;
            ReplaceMergeFields(source, dict);              
            source.Sections[0].PageSetup.SectionStart = SectionStart.Continuous;
            destination.DefaultCharacterFormat = source.DefaultCharacterFormat.Clone();
            destination.DefaultParagraphFormat = source.DefaultParagraphFormat.Clone();
            destination.Content.End.InsertRange(source.Content);
            
        }            
        // Save joined documents into one file.
        destination.Save("Merged Files.docx");

    // ....
    // ...

    private static int SetListNumber(DocumentModel destination, int i)
    {
        
        foreach (Section sec in destination.Sections)
        {
            foreach (var par in sec.Blocks)
            {
                if (par.ElementType.ToString() == "Paragraph")
                {
                    Paragraph p = (Paragraph)par;
                    if (p.ListFormat.Style != null)
                    {
                        p.ListFormat.ListLevelFormat.StartAt = i;
                        i++;
                    }
                }
            }
        }
        return i;
    }

Hi,

Regarding the first issue, what exactly do you mean by “ordered”, do you want the numbers to continue, or are you referring to something else?
Whatever the case, I think it would help greatly if you can provide us with some input files and show us what your desired output should be.

Regarding the second issue, you’ll need to add the desired header and footer to every Section element, see the Headers and Footers example.

I hope this helps.

Regards,
Mario

Hello,

Thank you for your response.
For the first issue, I joined below a screen shot of the result file.

Is there an e-mail to send you the document exemples ?

Regards,

You can send us an email or submit a support ticket, see our Contact page.

The following is the solution for merging documents with “Heading 2” list paragraphs and keeping their numbering in order:

string[] fileArray = Directory.GetFiles(directoryName);
var destination = DocumentModel.Load(fileArray[0]);

foreach (var file in fileArray.Skip(1))
{
    var source = DocumentModel.Load(file);
    source.Sections[0].PageSetup.SectionStart = SectionStart.Continuous;

    var mapping = new ImportMapping(source, destination, false);
    if (destination.Styles.Contains("heading 2") && source.Styles.Contains("heading 2"))
        mapping.SetDestinationStyle(source.Styles["heading 2"], destination.Styles["heading 2"]);

    foreach (var sourceSection in source.Sections)
        destination.Sections.Add(
            destination.Import(sourceSection, true, mapping));
}

destination.Save("Merged Files.docx");

Also, here is how to set the headers/footers from a source document to a destination document:

var headersDocument = DocumentModel.Load("header.docx");
var headersMapping = new ImportMapping(headersDocument, destination, false);
var headersFooters = headersDocument.Sections[0].HeadersFooters;

foreach (var section in destination.Sections)
    foreach (var headerFooter in headersFooters)
        section.HeadersFooters.Add(
            destination.Import(headerFooter, true, headersMapping));

destination.Save("Merged Files.docx");