问题描述
使用Microsoft Office Interop程序集(Office 2007)和ASP.NET 3.5将多个Word文档合并在一起时,我遇到了一些困难.我可以合并文档,但是缺少某些格式(即字体和图像).
I'm having some difficulty merging multiple word documents together using Microsoft Office Interop Assemblies (Office 2007) and ASP.NET 3.5. I'm able to merge the documents, but some of my formatting is missing (namely the fonts and images).
我当前的合并代码如下所示.
My current merge code is shown below.
private void CombineDocuments() {
object wdPageBreak = 7;
object wdStory = 6;
object oMissing = System.Reflection.Missing.Value;
object oFalse = false;
object oTrue = true;
string fileDirectory = @"C:\documents\";
Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document wDoc = WordApp.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
string[] wordFiles = Directory.GetFiles(fileDirectory, "*.doc");
for (int i = 0; i < wordFiles.Length; i++) {
string file = wordFiles[i];
wDoc.Application.Selection.Range.InsertFile(file, ref oMissing, ref oMissing, ref oMissing, ref oFalse);
wDoc.Application.Selection.Range.InsertBreak(ref wdPageBreak);
wDoc.Application.Selection.EndKey(ref wdStory, ref oMissing);
}
string combineDocName = Path.Combine(fileDirectory, "Merged Document.doc");
if (File.Exists(combineDocName))
File.Delete(combineDocName);
object combineDocNameObj = combineDocName;
wDoc.SaveAs(ref combineDocNameObj, ref m_WordDocumentType, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
}
我不一定在乎这是如何实现的.如果需要,它可以通过PDF输出.我只想保留格式.
I don't care necessarily how this is accomplished. It could output via PDF if it had to. I just want the formatting to carry over.
推荐答案
在添加文档时,您跳过了模板名称,这就是为什么ut缺少格式的原因.
While adding the documents you are skipping the Template name, that why ut's missing the format.
它应该看起来像
string defaultTemplate="your template name with full path";
OR
默认模板名称
string defaultTemplate="Normal.dot";
wordApplication.Documents.Add(ref defaultTemplate,............
Use this link as Ref: http://devpinoy.org/blogs/keithrull/archive/2007/05/23/how-to-merge-multiple-microsoft-word-documents-in-c.aspx
这篇关于合并Word文档(Office Interop& amp; .NET),保持格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!