DocumentFormat.OpenXML
考虑到 MS Word“跟踪更改”的模式,有什么方法可以使用 library 合并 2 个 word-documents 。或者也许可以使用另一个库?
问问题
2398 次
1 回答
0
You can use standart api of MS Word
Microsoft.Office.Interop.Word.Document worddoc;
Microsoft.Office.Interop.Word.Application wordApp;
Microsoft.Office.Interop.Word.Range _range;
object file = "1.doc";
wordApp = new Microsoft.Office.Interop.Word.Application();
worddoc = wordApp.Documents.Open(ref file);
_range = worddoc.Content;
Microsoft.Office.Interop.Word.Range rng = _range;
rng.Start = rng.End;
rng.InsertFile("2.doc");
EDIT
And read this Merging-Word-Documents
EDIT2
so try this
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
....
public byte[] OpenAndCombine(List<string> FileNames)
{
List<byte[]> documents = new List<byte[]>();
foreach (string _FileName in FileNames)
documents.Add(File.ReadAllBytes(_FileName));
MemoryStream mainStream = new MemoryStream();
mainStream.Write(documents[0], 0, documents[0].Length);
mainStream.Position = 0;
int pointer = 1;
byte[] ret;
using (WordprocessingDocument mainDocument = WordprocessingDocument.Open(mainStream, true))
{
XElement newBody = XElement.Parse(mainDocument.MainDocumentPart.Document.Body.OuterXml);
for (pointer = 1; pointer < documents.Count; pointer++)
{
WordprocessingDocument tempDocument = WordprocessingDocument.Open(new MemoryStream(documents[pointer]), true);
XElement tempBody = XElement.Parse(tempDocument.MainDocumentPart.Document.Body.OuterXml);
newBody.Add(tempBody);
mainDocument.MainDocumentPart.Document.Body = new Body(newBody.ToString());
mainDocument.MainDocumentPart.Document.Save();
mainDocument.Package.Flush();
}
}
ret = mainStream.ToArray();
mainStream.Close();
mainStream.Dispose();
return (ret);
}
于 2012-04-27T06:12:29.767 回答