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