I have a WordprocessingDocument
that I clone into a new object using the following function:
private static WordprocessingDocument CloneWordprocessingDocument(string docPath)
{
using (var fileStream = new FileStream(docPath, FileMode.Open, FileAccess.Read,
FileShare.ReadWrite))
{
using (var originalDoc = WordprocessingDocument.Open(fileStream, false))
{
var clonedDoc = originalDoc.Clone() as WordprocessingDocument;
originalDoc.Close();
return clonedDoc;
}
}
}
Afterwards, I process the cloned object using various methods. This procedure can be repeated for a large number of objects, if necessary, which are subsequently kept in a list.
These objects are then to be merged into a single .docx. Currently I write them on disk for this and pass the paths to DocBuilder
.
public static string MergeDocs(List<string> docPaths, string tmpPath)
{
List<Source> sources = docPaths.Select(docPath => new Source(new WmlDocument(docPath), false)).ToList();
var finalDoc = DocumentBuilder.BuildDocument(sources);
var finalPath = tmpPath + Guid.NewGuid() + ".docx";
finalDoc.SaveAs(finalPath);
return finalPath;
}
To save myself the overhead of storing them, I would like to convert them to a byte [] and pass them to DocBuilder that way.
I know that I can initialize a WmlDocument with a byte[]. But how can I convert a WordprocessingDocument to a byte[] without saving it?