Getting a repair error when copy slide from one presentation to another using open xml sdk

Swapnil Sharma 6 Reputation points
2021-09-03T14:31:18.6+00:00

Hi i am using this particular code to copy slide from one presentation to another but I am getting an error that ppt goes in repair mode.

static uint _uniqueId;

    static uint GetMaxIdFromChild(SlideMasterIdList slideMasterIdList)  
    {  
        // Slide master identifiers have a minimum value of greater than  
        // or equal to 2147483648.   
        uint max = 2147483648;  

        if (slideMasterIdList != null)  
            // Get the maximum id value from the current set of children.  
            foreach (SlideMasterId child in  
              slideMasterIdList.Elements<SlideMasterId>())  
            {  
                uint id = child.Id;  

                if (id > max)  
                    max = id;  
            }  

        return max;  
    }  

    static uint GetMaxIdFromChild(SlideIdList slideMasterIdList)  
    {  
        // Slide master identifiers have a minimum value of greater than  
        // or equal to 2147483648.   
        uint max = 2147483648;  

        if (slideMasterIdList != null)  
            // Get the maximum id value from the current set of children.  
            foreach (SlideId child in  
              slideMasterIdList.Elements<SlideId>())  
            {  
                uint id = child.Id;  

                if (id > max)  
                    max = id;  
            }  

        return max;  
    }  

    public static void Copy(string sourcePresentationStream, uint copiedSlidePosition, string destPresentationStream)  
    {  
        using (var destDoc = PresentationDocument.Open(destPresentationStream, true))  
        {  
            var sourceDoc = PresentationDocument.Open(sourcePresentationStream, false);  
            var destPresentationPart = destDoc.PresentationPart;  
            var destPresentation = destPresentationPart.Presentation;  

            //_uniqueId = 4;  
            //uint maxId = 4;  

            _uniqueId = GetMaxIdFromChild(destPresentation.SlideMasterIdList);  
            uint maxId = GetMaxIdFromChild(destPresentation.SlideIdList);  

            var sourcePresentationPart = sourceDoc.PresentationPart;  
            var sourcePresentation = sourcePresentationPart.Presentation;  

            int copiedSlideIndex = (int)--copiedSlidePosition;  

            int countSlidesInSourcePresentation = sourcePresentation.SlideIdList.Count();  
            if (copiedSlideIndex < 0 || copiedSlideIndex >= countSlidesInSourcePresentation)  
                throw new ArgumentOutOfRangeException(nameof(copiedSlidePosition));  

            SlideId copiedSlideId = sourcePresentationPart.Presentation.SlideIdList.ChildElements[copiedSlideIndex] as SlideId;  
            SlidePart copiedSlidePart = sourcePresentationPart.GetPartById(copiedSlideId.RelationshipId) as SlidePart;  

            SlidePart addedSlidePart = destPresentationPart.AddPart<SlidePart>(copiedSlidePart);  

            SlideMasterPart addedSlideMasterPart = destPresentationPart.AddPart(addedSlidePart.SlideLayoutPart.SlideMasterPart);  

            // Create new slide ID  
            maxId++;  
            SlideId slideId = new SlideId  
            {  
                Id = maxId,  
                RelationshipId = destDoc.PresentationPart.GetIdOfPart(addedSlidePart)  
            };  
            destPresentation.SlideIdList.Append(slideId);  

            // Create new master slide ID  
            _uniqueId++;  
            SlideMasterId slideMaterId = new SlideMasterId  
            {  
                Id = _uniqueId,  
                RelationshipId = destDoc.PresentationPart.GetIdOfPart(addedSlideMasterPart)  
            };  
            destDoc.PresentationPart.Presentation.SlideMasterIdList.Append(slideMaterId);  

            // change slide layout ID  
            FixSlideLayoutIds(destDoc.PresentationPart);  

            destDoc.PresentationPart.Presentation.Save();  
            destDoc.Close();  
            sourceDoc.Close();  
        }  
        //sourcePresentationStream.Close();  
        // destPresentationStream.Close();  
    }  

    static void FixSlideLayoutIds(PresentationPart presPart)  
    {  
        // Make sure that all slide layouts have unique ids.  
        foreach (SlideMasterPart slideMasterPart in presPart.SlideMasterParts)  
        {  
            foreach (SlideLayoutId slideLayoutId in slideMasterPart.SlideMaster.SlideLayoutIdList)  
            {  
                _uniqueId++;  
                slideLayoutId.Id = (uint)_uniqueId;  
            }  

            slideMasterPart.SlideMaster.Save();  
        }  
    }  

As soon as I copy I get this error

129000-image.png

Please can anyone help me in resolving this.

.NET CLI
.NET CLI
A cross-platform toolchain for developing, building, running, and publishing .NET applications.
337 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,995 questions
PowerPoint Management
PowerPoint Management
PowerPoint: A family of Microsoft presentation graphics products that offer tools for creating presentations and adding graphic effects like multimedia objects and special effects with text.Management: The act or process of organizing, handling, directing or controlling something.
230 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Swapnil Sharma 6 Reputation points
    2021-09-27T06:16:34.403+00:00

    private static uint CreateId(SlideIdList slideIdList)
    {
    uint currentId = 0;
    foreach (SlideId slideId in slideIdList)
    {
    if (slideId.Id > currentId)
    {
    currentId = slideId.Id;
    }
    }

            return ++currentId;
        }
    
        private static uint CreateId(SlideMasterIdList slideMasterIdList)
        {
            uint currentId = 0;
            foreach (SlideMasterId masterId in slideMasterIdList)
            {
                if (masterId.Id > currentId)
                {
                    currentId = masterId.Id;
                }
            }
    
            return ++currentId;
        }
    
        public static void Copy(string sourcePresentationStream, uint copiedSlidePosition, string destPresentationStream)
        {
            using (var destDoc = PresentationDocument.Open(destPresentationStream, true))
            {
                var sourceDoc = PresentationDocument.Open(sourcePresentationStream, false);
                var destPresentationPart = destDoc.PresentationPart;
                var destPresentation = destPresentationPart.Presentation;
    
                var sourcePresentationPart = sourceDoc.PresentationPart;
                var sourcePresentation = sourcePresentationPart.Presentation;
    
                int copiedSlideIndex = (int)--copiedSlidePosition;
    
                int countSlidesInSourcePresentation = sourcePresentation.SlideIdList.Count();
                if (copiedSlideIndex < 0 || copiedSlideIndex >= countSlidesInSourcePresentation)
                    throw new ArgumentOutOfRangeException(nameof(copiedSlidePosition));
    
                SlideId copiedSlideId = sourcePresentationPart.Presentation.SlideIdList.ChildElements[copiedSlideIndex] as SlideId;
                SlidePart copiedSlidePart = sourcePresentationPart.GetPartById(copiedSlideId.RelationshipId) as SlidePart;
    
                SlidePart addedSlidePart = destPresentationPart.AddPart<SlidePart>(copiedSlidePart);
    
                NotesSlidePart noticePart = addedSlidePart.GetPartsOfType<NotesSlidePart>().FirstOrDefault();
                if (noticePart != null)
                {
                    addedSlidePart.DeletePart(noticePart);
                }
    
                SlideMasterPart addedSlideMasterPart = destPresentationPart.AddPart(addedSlidePart.SlideLayoutPart.SlideMasterPart);
    
                // Create new slide ID
                SlideId slideId = new SlideId
                {
                    Id = CreateId(destPresentation.SlideIdList),
                    RelationshipId = destDoc.PresentationPart.GetIdOfPart(addedSlidePart)
                };
                destPresentation.SlideIdList.Append(slideId);
    
                // Create new master slide ID
                uint masterId = CreateId(destPresentation.SlideMasterIdList);
                SlideMasterId slideMaterId = new SlideMasterId
                {
                    Id = masterId,
                    RelationshipId = destDoc.PresentationPart.GetIdOfPart(addedSlideMasterPart)
                };
                destDoc.PresentationPart.Presentation.SlideMasterIdList.Append(slideMaterId);
    
                destDoc.PresentationPart.Presentation.Save();
    
                // Make sure that all slide layouts have unique ids.
                foreach (SlideMasterPart slideMasterPart in destDoc.PresentationPart.SlideMasterParts)
                {
                    foreach (SlideLayoutId slideLayoutId in slideMasterPart.SlideMaster.SlideLayoutIdList)
                    {
                        masterId++;
                        slideLayoutId.Id = masterId;
                    }
    
                    slideMasterPart.SlideMaster.Save();
                }
    
                destDoc.PresentationPart.Presentation.Save();
                destDoc.Close();
                sourceDoc.Close();
            }
            //sourcePresentationStream.Close();
            // destPresentationStream.Close();
        }
    
    1 person found this answer helpful.
    0 comments No comments

  2. Jack J Jun 24,516 Reputation points Microsoft Vendor
    2021-09-06T03:03:51.947+00:00

    @Swapnil Sharma , based on my test, I reproduced the same error with you. And based on my further research, I find it is hard for me to solve the issue.

    However, I find an alternative simple way to use Microsoft.Office.Interop.PowerPoint to copy a slide from one presentation to another.

    First, Please try to install nuget-package Microsoft.Office.Interop.PowerPoint.

    Second, Please try the following code to do it.

    using PowerPoint = Microsoft.Office.Interop.PowerPoint;  
      
    namespace ConsoleApp1  
    {  
        class Program  
        {  
            static void Main(string[] args)  
            {  
                PowerPoint.Application app = new PowerPoint.Application();  
                PowerPoint.Presentation p1= app.Presentations.Open("D:\\1.pptx",WithWindow:MsoTriState.msoFalse);  
                PowerPoint.Presentation p2 = app.Presentations.Open("D:\\3.pptx", WithWindow: MsoTriState.msoFalse);  
                PowerPoint.Slide s = p1.Slides[2];  
                s.Copy();  
                p2.Slides.Paste();  
                p2.Save();  
                p1.Close();  
                p2.Close();  
      
      
            }  
        }  
    }  
    

    Best Regards,

    Jack


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.