简体   繁体   English

如何从 PresentationDocument object 中获取字节数组或 Stream

[英]How to Obtain a Byte Array or Stream from a PresentationDocument object

I have Open Office XML code that slices and dices PowerPoint files.我有 Open Office XML 代码,可以对 PowerPoint 文件进行切片和切块。

Due to the nature of the Open XML SDK, much of this code reads to and writes from files on the file system.由于 Open XML SDK 的性质,此代码的大部分读取和写入文件系统上的文件。 The goal is to eliminate these temporary files completely, and replace them with byte arrays and/or in-memory streams.目标是完全消除这些临时文件,并用字节 arrays 和/或内存流替换它们。

Some of this code merges together PowerPoints from different slide sources.其中一些代码将来自不同幻灯片源的 PowerPoint 合并在一起。 An example of this code follows.此代码的示例如下。 Imagine a database containing PowerPoint presentations.想象一个包含 PowerPoint 演示文稿的数据库。 PmlDocument is a class in Open XML Power Tools: PmlDocument在Open XML Power Tools中是一个class:

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.其中byte[] data代表一个新的 PowerPoint PPTX,然后我可以将其保存回数据库。

This code works perfectly fine, because I'm already working with byte arrays in this example.这段代码工作得很好,因为我已经在这个例子中使用了字节 arrays。 The problem occurs when I have to deal with PresentationDocument objects.当我必须处理PresentationDocument对象时会出现问题。 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 .不幸的是,似乎没有更好的方法从PresentationDocument中提取字节。 Does anyone know how to do this in memory, perhaps with a MemoryStream object?有谁知道如何在 memory 中执行此操作,也许使用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.注意:我已阅读OfficeTalk: Working with In-Memory Open XML Documents ,但它似乎假定您已经拥有正确文档格式的字节数组。

Looking at the source here , PresentationDocument inherits from OpenXmlPackage Which implements the SaveAs method, and when followed eventually calls:这里的源码, PresentationDocument继承自OpenXmlPackage ,它实现了SaveAs方法,当遵循最终调用时:

/// <summary>
/// Creates a clone of this OpenXml package, opened on the given stream.
/// The cloned OpenXml package is opened with the same settings, i.e.,
/// FileOpenAccess and OpenSettings, as this OpenXml package.
/// </summary>
/// <param name="stream">The IO stream on which to open the OpenXml package.</param>
/// <returns>The cloned OpenXml package.</returns>
public OpenXmlPackage Clone(Stream stream)`

In the end this just copies the OpenXmlPart to the stream最后,这只是将OpenXmlPart复制到stream

using (OpenXmlPackage clone = CreateClone(stream))
{
     foreach (var part in Parts)
     {
         clone.AddPart(part.OpenXmlPart, part.RelationshipId);
     }
}

When you know it's there, you can eventually find it in the documentation !当您知道它在那里时,您最终可以在文档中找到它!

All-in-all, this gives you the ability to save directly to the bytes without going to file first.总而言之,这使您能够直接保存到字节,而无需先归档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM