将 docx 文件合并在一起,包括页眉、页脚和图片
我必须将多个 DOCX 文件(使用 DocX Package 创建)合并到一个大文件中。我已经成功合并多个文件,但无法合并页眉和页脚(它们在文件、页面之间有所不同)。我已经尝试过 DocX 包 和 MS Office COM Interop,它只是不想合并标头/页脚。它们被跳过。
有谁可以提供可行的解决方案吗?可以是任何东西(包括使用 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);
}
}
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看看这个 http://devpinoy.org/blogs/keithrull/archive/2007/05/23/how-to-merge-multiple-microsoft-word-documents-in-c.aspx
更改此行:
到此
从第二个文档获取标题和页脚。
添加整个方法以及在源发生变化时如何调用它。
班级:
和电话:
Check this out http://devpinoy.org/blogs/keithrull/archive/2007/05/23/how-to-merge-multiple-microsoft-word-documents-in-c.aspx
Change this line:
to this
To get the heading and footer from the second document.
Adding in the entire method and how to call it in case the source changes.
The Class:
And the Call:
由于 最新版本的 DocX 合并工作正常。因此,现在可以直接使用 DocX,而不是使用 Interop。唯一需要知道的
页眉/页脚
取自第一个文档(模板),因为页眉/页脚是按文档而不是按页面设置的。Since newest version of DocX merging works correctly. So instead of using Interop it's now possible to use DocX directly. The only thing to know
headers/footers
are taken from first document (a template) as headers/footers are set per document not per page.从 http://powertools.codeplex.com/ C# 代码开始,稍微修改一下,使其执行以下操作你想要页眉和页脚。
Eric White 的博客解释包含有关代码如何工作的评论。
Start with the http://powertools.codeplex.com/ C# code, and modify it slightly so it does what you want with headers and footers.
Eric White's blog explains contains commentary on how the code works.
您是否尝试过在 xml 级别进行合并? Docx 是内含 Xml 文件的 zip 文件。
您可以使用 XDocument 合并 XML。
Have you tried to merge at xml level ? Docx are zip files with Xml files inside.
You may be able to merge the XML using XDocument.