write to powerpoint pptx from c# using openxml

Viewed 1391

I am writing a data in to a pptx last slide using openxml in csharp and below is code to save the ppt

public static void CreateTableInLastSlide(PresentationDocument presentationDocument)
    {
        // Get the presentation Part of the presentation document
        PresentationPart presentationPart = presentationDocument.PresentationPart;

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

        // Get the last Slide Part of the presentation document
        var tableSlidePart = (SlidePart)list.Last();

        // Declare and instantiate the graphic Frame of the new slide

        P.GraphicFrame graphicFrame = tableSlidePart.Slide.CommonSlideData.ShapeTree.AppendChild(new P.GraphicFrame());

        ApplicationNonVisualDrawingPropertiesExtension applicationNonVisualDrawingPropertiesExtension = new ApplicationNonVisualDrawingPropertiesExtension();
        P14.ModificationId modificationId1 = new P14.ModificationId() { Val = 3229994563U };
        modificationId1.AddNamespaceDeclaration("p14", "http://schemas.microsoft.com/office/powerpoint/2010/main");
        applicationNonVisualDrawingPropertiesExtension.Append(modificationId1);
        graphicFrame.NonVisualGraphicFrameProperties = new DocumentFormat.OpenXml.Presentation.NonVisualGraphicFrameProperties
        (new A.NonVisualDrawingProperties() { Id = 5, Name = "table 1" },
        new A.NonVisualGraphicFrameDrawingProperties(new A.GraphicFrameLocks() { NoGrouping = true }),
        new ApplicationNonVisualDrawingProperties(new ApplicationNonVisualDrawingPropertiesExtensionList(applicationNonVisualDrawingPropertiesExtension)));

        graphicFrame.Transform = new Transform(new Offset() { X = 10, Y = 10 });
        graphicFrame.Graphic = new A.Graphic(new A.GraphicData(GenerateTable()) { Uri = "http://schemas.openxmlformats.org/drawingml/2006/table" });
        presentationPart.Presentation.Save();

    }

the file is saved with required data but While opening the powerpoint I get the following error:

Powerpoint found a problem with content in "Sample.pptx". Power point can attempt to repair the presentation.

Once repaired, the content is shown ok. but since the file is corrupted when i run the code it is not updating with the latest data nexttime. Please help me to solve the issue

-tried to open in different version of office but no luck -created the template from different versions still the issue occurs

0 Answers
Related