I have Open Office XML code that slices and dices PowerPoint files.
Due to the nature of the Open XML SDK, much of this code reads to and writes from files on the file system. The goal is to eliminate these temporary files completely, and replace them with byte arrays and/or in-memory streams.
Some of this code merges together PowerPoints from different slide sources. An example of this code follows. Imagine a database containing PowerPoint presentations. PmlDocument
is a class in Open XML Power Tools:
foreach (var item in database)
{
var openXmlDocument = new OpenXmlPowerToolsDocument(item.data);
var document = new PmlDocument(openXmlDocument);
sources.Add(new SlideSource(document, false));
}
PmlDocument newDocument = PresentationBuilder.BuildPresentation(sources);
byte[] data = newDocument.DocumentByteArray;
Where byte[] data
represents a new PowerPoint PPTX, which I can then save back to the database.
This code works perfectly fine, because I'm already working with byte arrays in this example. The problem occurs when I have to deal with PresentationDocument
objects. A great deal of processing already occurs in our program with these objects, and the current way to obtain the byte array from such objects is to do this:
presentationDocument.SaveAs(fileName);
byte[] array = File.ReadAllBytes(fileName);
Which is not what I want.
Unfortunately, there doesn't appear to be a better way to extract the bytes from a PresentationDocument
. Does anyone know how to do this in memory, perhaps with a MemoryStream
object?
Note: I've read OfficeTalk: Working with In-Memory Open XML Documents, but it appears to assume that you already have a byte array in the proper document format.