Open-XML-SDK icon indicating copy to clipboard operation
Open-XML-SDK copied to clipboard

Attempt to open merged presentation file throws a "needs repair" error

Open vramanrsv opened this issue 2 years ago • 1 comments

I am having an issue when I am trying to merge PPTs, the code is working without throwing any error but when I open the merged presentation file it says that it needs to be repaired. When the repair is complete, the file opens and all of the data is there.

Screenshots 1 2

To Reproduce My code:

namespace Console_app
{
    using DocumentFormat.OpenXml.Packaging;
    using DocumentFormat.OpenXml.Presentation;
    using System.IO;
    class Program
    {
        static uint _uniqueId;
        static void Main(string[] args)
        {
            Stream stream1 = File.Open("sample1.pptx", FileMode.Open,FileAccess.Read);
            Stream stream2 = File.Open("sample2.pptx", FileMode.Open,FileAccess.Read);            
            Stream stream3 = File.Open("result.pptx", FileMode.Open,FileAccess.ReadWrite);         
            Stream[] arrStream = {stream1, stream2};
            PresentationDocument destDoc = PresentationDocument.Open(stream3, true);  
            Copy(arrStream,destDoc);    
            stream3.Close();
        }
    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(Stream[] arrStream, PresentationDocument destDoc)
    {
        var destPresentationPart = destDoc.PresentationPart;         
        var destPresentation = destPresentationPart.Presentation; 
        destPresentation.SlideIdList = new SlideIdList();             
        foreach (Stream sourcePresentationStream  in arrStream){    
            var sourceDoc = PresentationDocument.Open(sourcePresentationStream, false);  
            var sourcePresentationPart = sourceDoc.PresentationPart;                  
            SlideId copiedSlideId = sourcePresentationPart.Presentation.SlideIdList.ChildElements[0] as SlideId;  
            SlidePart copiedSlidePart = sourcePresentationPart.GetPartById(copiedSlideId.RelationshipId) as SlidePart; 
            SlidePart addedSlidePart = destPresentationPart.AddPart<SlidePart>(copiedSlidePart);   
            SlideId slideId = new SlideId
            {
                Id = CreateId(destPresentation.SlideMasterIdList),
                RelationshipId = destDoc.PresentationPart.GetIdOfPart(addedSlidePart)
            };
            destPresentation.SlideIdList.Append(slideId);       
            SlideMasterPart addedSlideMasterPart = destPresentationPart.AddPart(addedSlidePart.SlideLayoutPart.SlideMasterPart);
            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();
            sourcePresentationStream.Close();    
            sourceDoc.Close();
            }
            destDoc.Close();
        }
    }
}

Steps to reproduce the behavior:

  1. Save the project code
  2. Download files in the project folder sample1.pptx, sample2.pptx, result.pptx
  3. Run the code using dotnet restore, dotnet build and dotnet run
  4. Open the result.pptx file, the above mentioned error is shown.

Observed behavior The result.pptx file is throwing error as shown in the screenshots but after click on "repair" and "ok", the functionality worked and the PPTs got merged.

Expected behavior When opened should not show any error to the user and functionality should be worked.

vramanrsv avatar Aug 04 '22 04:08 vramanrsv

Hi @vramanrsv ,

Thanks for letting us know about this issue. I'll look into this and see if I can find the issue.

mikeebowen avatar Aug 04 '22 16:08 mikeebowen

This might be related to notes slides or custom xml parts that might exist on the presentations which are to be merged. Please have a look at the answer I just added to the following issue (not the Aspose one), which got it working for me, at least as far as I have tested: https://stackoverflow.com/questions/67410465/copy-slide-containing-notes-from-one-powerpoint-presentaition-to-another-with-op

danielwidegrenmeepo avatar Feb 01 '23 09:02 danielwidegrenmeepo

@vramanrsv,

There are actually many things that could be the problem here. If the slides you are copying are using different formatting, styling or other resources in the presentation, then there maybe broken relationships or references internally. As @danielwidegrenmeepo mentioned back in February this is not necessarily a problem with the SDK. The framework code doesn't make sure all relationships and resources used by a slide are also merged into the new presentation when adding the part. Unless you can point out what the SDK is doing or not doing that it should be, then I think this issue should be closed.

tomjebo avatar Dec 21 '23 16:12 tomjebo

@vramanrsv, I am also facing a similar error. Is there any workaround to solving this issue?

idiot-owl avatar Feb 09 '24 05:02 idiot-owl

@idiot-owl if you're facing the same problem, are you doing the same as the OP? If not, please open a new issue and provide sample code/sample files so this can be debugged further. @vramanrsv since you haven't commented further, we're closing this issue.

AlfredHellstern avatar Feb 20 '24 19:02 AlfredHellstern