我必须将多个 DOCX 文件(使用DocX Package创建)合并到一个大文件中。我已经设法合并多个文件,但我无法合并页眉和页脚(它们在文件、页面之间有所不同)。我已经尝试过DocX 包和 MS Office COM 互操作,它根本不想合并页眉/页脚。他们被跳过。
有没有人可以提供可行的解决方案?可以是任何东西(包括使用 PDFCreator COM、Interop 或DocX转换为 PDF )。
我还在 C# 中尝试过 PDFCreator Sample,它可以很好地将一个文档转换为 PDF,但我不知道如何向它提供多个文档,因此它会创建一个大 PDF。因此,如果有人可以提供一种好的(免费)方式,我们将不胜感激。
这就是我使用的:
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);
}
}