简体   繁体   中英

Power point template gets corrupted while trying to get all slide parts from presentation document

Code follows,

 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>();

 }

The last line of code makes the power point template corrupted. my openxml version is 2.5. Any one please tell me where i'm going wrong.

  • EDIT

Here is how i load the memoryStreamObj,

byte[] reportByteArray = null;
        using (MemoryStream memoryStreamObj = new MemoryStream())
        {
            memoryStreamObj.Write(reportTemplateByteArray, 0, (int)reportTemplateByteArray.Length);
            using (PresentationDocument presentationDocumentObj = PresentationDocument.Open(memoryStreamObj,true))
         {                   
               //made changes to template

         }
            reportByteArray = memoryStreamObj.GetBuffer();
     }

The following line was the issue,

memoryStreamObj.GetBuffer();

Instead used the following,

memoryStreamObj.ToArray();

MSDN documentation follows,

Note that the buffer contains allocated bytes which might be unused. For example, if the string "test" is written into the MemoryStream object, the length of the buffer returned from GetBuffer is 256, not 4, with 252 bytes unused. To obtain only the data in the buffer, use the ToArray method; however, ToArray creates a copy of the data in memory.

See the below link for more details,

When is GetBuffer() on MemoryStream ever useful?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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