Search code examples
openxmlopenxml-sdk

OpenXml ChangeDocumentType


I need to convert a powerpoint template from potx to pptx. As seen here: http://www.codeproject.com/Tips/366463/Create-PowerPoint-presentation-using-PowerPoint-te I have tried with the following code. However the resulting pptx document is invalid and can't be opened by Office Powerpoint. If I skip the line newDoc.ChangeDocumentType then the resulting document is valid, but not converted to pptx. templateContentBytes is a byte array containing the content of the potx document. And temppath points to its local version.

using (var stream = new MemoryStream())
                {
                    stream.Write(templateContentBytes, 0, templateContentBytes.Length);

                    using (var newdoc = PresentationDocument.Open(stream, true))
                    {
                        newdoc.ChangeDocumentType(PresentationDocumentType.Presentation);
                        PresentationPart presentationPart = newdoc.PresentationPart;

                        presentationPart.PresentationPropertiesPart.AddExternalRelationship(
                            "http://schemas.openxmlformats.org/officeDocument/2006/" + "relationships/attachedTemplate",
                            new Uri(tempPath, UriKind.Absolute));
                        presentationPart.Presentation.Save();


                        File.WriteAllBytes(tempPathResult, stream.ToArray());

Solution

  • I had the same problem, just move

    File.WriteAllBytes(tempPathResult, stream.ToArray());
    

    outside of the using