0

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();
     }
Harun
  • 5,109
  • 4
  • 38
  • 60
  • How are you loading the `memoryStreamObj`? One very easy fix if you only want to read from the document and not update it is to change the call to the `open` method to `PresentationDocument.Open(memoryStreamObj,false)` - the boolean denotes whether or not the document is editable. – petelids May 17 '17 at 19:33
  • @petelids, No, i need to add content to one of the slide. – Harun May 18 '17 at 07:11
  • @petelids,Please see the edit section in the question to see how I'm loading memoryStreamObj. – Harun May 18 '17 at 07:16

1 Answers1

2

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?

Community
  • 1
  • 1
Harun
  • 5,109
  • 4
  • 38
  • 60