我必须将多个DOCX文件(使用DocX Package创建)合并为一个大文件。我已经设法合并了多个文件,但是我没有合并页眉和页脚(它们在文件,页面之间有所不同)。我试过DocX package和MS Office COM Interop,它根本不想合并页眉/页脚。他们被跳过。将docx文件合并在一起,包括页眉,页脚和图片
有没有人可以提供可行的解决方案?可以是任何东西(包括使用PDFCreator COM转换为PDF,或者Interop或DocX)。
我也试过C#中的PDFCreator示例,它很好地将一个文档转换为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);
}
}
不幸的是,这并没有合并我记得的脚注和标题,因为我记得之前尝试过。 – MadBoy 2010-10-14 07:37:12
好的,我会对这个解决方案进行修改,看看它是否可以修改。 – darkstar3d 2010-10-14 11:50:05
对不起,迟到了,工作赶上了我。如果您更改了我链接的解决方案类中的分页行,它将为您解决。将其更改为以下内容:object pageBreak = Word.WdBreakType.wdSectionBreakNextPage; – darkstar3d 2010-10-14 16:47:13