C# 尝试从演示文档获取所有幻灯片部分时,Power point模板损坏
代码如下:C# 尝试从演示文档获取所有幻灯片部分时,Power point模板损坏,c#,openxml,powerpoint-2013,C#,Openxml,Powerpoint 2013,代码如下: using (PresentationDocument presentationDocumentObj = PresentationDocument.Open(memoryStreamObj,true)) { PresentationPart presentationPart = presentationDocument.PresentationPart; // Check for a null document object.
using (PresentationDocument presentationDocumentObj = PresentationDocument.Open(memoryStreamObj,true))
{
PresentationPart presentationPart = presentationDocument.PresentationPart;
// Check for a null document object.
if (presentationDocument == null)
{
throw new ArgumentNullException("presentationDocument");
}
// Get the Slide Id collection of the presentation document
var slideIdList = presentationPart.Presentation.SlideIdList;
if (slideIdList == null)
throw new NullReferenceException("The number of slide is empty, please select a ppt with a slide at least again");
// Get all Slide Part of the presentation document
var list = slideIdList.ChildElements.Cast<SlideId>().Select(x => presentationPart.GetPartById(x.RelationshipId)).Cast<SlidePart>();
}
下面这句话就是问题所在
memoryStreamObj.GetBuffer();
相反,我们使用了以下内容:
memoryStreamObj.ToArray();
MSDN文件如下:
请注意,缓冲区包含可能未使用的已分配字节。例如,如果将字符串测试写入MemoryStream对象,则从GetBuffer返回的缓冲区长度为256,而不是4,未使用252字节。要仅获取缓冲区中的数据,请使用ToArray方法;但是,ToArray会在内存中创建数据的副本
有关更多详细信息,请参阅下面的链接
如何加载memoryStreamObj?如果您只想读取文档而不想更新文档,一个非常简单的修复方法是将对open方法的调用更改为PresentationDocument。OpenmemoryStreamObj,false-布尔值表示文档是否可编辑。@petelids,否,我需要向其中一张幻灯片添加内容。@petelids,请参阅问题中的编辑部分,了解我是如何加载memoryStreamObj的。
memoryStreamObj.ToArray();