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;
}