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

Media References are removed

Open lklein53 opened this issue 4 years ago • 9 comments

Hi I encountered the following issue while using the OpenXML SDK:

Description I opened the attached file with the OpenXML SDK and saved it using a different name. If I try to open the saved file Power Point reports that the file is broken and that it can try to repair it. After repairing the document does not contain the video which was originally in the presentation.

Information

  • .NET Target: .NET Core
  • DocumentFormat.OpenXml Version: 2.13.0-beta2

Repro

Use the following code with the attached file and try to open the saved pptx file.

    var ppt = PresentationDocument.Open("source.pptx", false);
    ppt.SaveAs("saved.pptx");

source.pptx

Observed

The saved file is broken and has to be repaired. The repaired file does not contain the video anymore. Somehow the OpenXML SDK removes all the media references contained in the pptx file.

Expected

A valid pptx file that is identical to the original document.

I would also be willing to help working on a fix if someone could provide me a starting point why/where the references are removed.

Thanks and kind regards, Lars

lklein53 avatar May 12 '21 16:05 lklein53

@lklein53 Can you try the latest beta? 2.7.1 is quite old at this point.

twsouthwick avatar May 12 '21 20:05 twsouthwick

@twsouthwick thanks for your quick reply. Sorry messed up the bug report. I already tried using version v2.13.0-beta2

lklein53 avatar May 12 '21 21:05 lklein53

Thanks! So to be clear, it removes them in both? Trying to understand if it is a regression from an earlier version that may have been working as expected.

twsouthwick avatar May 12 '21 22:05 twsouthwick

Yes, both versions produce a broken pptx without the video in it.

lklein53 avatar May 13 '21 06:05 lklein53

Thanks! At least it's not a regression. Not sure off the top of my head why it's being removed, but we'd gladly accept a PR!

twsouthwick avatar May 14 '21 01:05 twsouthwick

Hi, I had some time to look into this today. In line 1731 of the OpenXmlPartContainer is the following code:

var newDataPartReference = DataPartReferenceRelationship.Create(this, newDataPart, dataPartReferenceRelationship.RelationshipType, dataPartReferenceRelationship.Id);
ReferenceRelationshipList.AddLast(newDataPartReference);

I think this has to be replaced by

var newDataPartReference = DataPartReferenceRelationship.Create(child, newDataPart, dataPartReferenceRelationship.RelationshipType, dataPartReferenceRelationship.Id);
child.AddDataPartReferenceRelationship(newDataPartReference);

At least my example works with this change. If you agree I will create a PR for it. Including a test case

lklein53 avatar May 14 '21 09:05 lklein53

Awesome! Go ahead and make a PR and we can explore it further

twsouthwick avatar May 14 '21 16:05 twsouthwick

https://github.com/OfficeDev/Open-XML-SDK/pull/934 contains the change and a simple regression test

lklein53 avatar May 14 '21 16:05 lklein53

@tomjebo Can you take a look to verify this change makes sense?

twsouthwick avatar Jun 24 '21 17:06 twsouthwick

@twsouthwick can this issue be closed? Somehow my changes have been merged in June last year already (https://github.com/OfficeDev/Open-XML-SDK/pull/934)

lklein53 avatar Dec 22 '22 11:12 lklein53