Skip to content

Commit 089e54b

Browse files
NikiforovAllsergey-tihonCopilot
authored
fix: copy 3D model source links and web extension parts when extracting slides (#566)
* fix: copy 3D model source links and web extension parts when extracting slides A slide with am3d:attrSrcUrl or we:webextensionref keeps a relationship id that points to nothing, because the hyperlink relationship and the web extension part (with its snapshot image) are not copied. * Change validation method for presentation Update validation to use Office 2013 file format version. Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * fix: copy linked web extension snapshots --------- Co-authored-by: Sergey Tihon <sergey.tihon@gmail.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent e199127 commit 089e54b

4 files changed

Lines changed: 189 additions & 1 deletion

File tree

‎Clippit.Tests/PowerPoint/PresentationBuilderTests.cs‎

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,155 @@ public async Task PB010_EmbeddedFontWithEot10Header()
242242
await Assert.That(fonts["New Font"]).IsEquivalentTo(eot22);
243243
}
244244

245+
[Test]
246+
public async Task PB011_SlideWith3DModelSourceUrl()
247+
{
248+
var sourceUrl = new Uri("https://example.com/models/cube");
249+
250+
var sourceDir = new DirectoryInfo("../../../../TestFiles/");
251+
using var sourceStream = new MemoryStream();
252+
sourceStream.Write(File.ReadAllBytes(Path.Combine(sourceDir.FullName, "PB008-3DModel.pptx")));
253+
using (var source = PresentationDocument.Open(sourceStream, true))
254+
{
255+
var sourceSlide = source.PresentationPart!.SlideParts.Single(s =>
256+
s.Model3DReferenceRelationshipParts.Any()
257+
);
258+
var hyperlink = sourceSlide.AddHyperlinkRelationship(sourceUrl, true);
259+
var raster = sourceSlide.GetXDocument().Descendants(AM3D.blip).Single().Parent!;
260+
raster.AddBeforeSelf(new XElement(AM3D.attrSrcUrl, new XAttribute(R.id, hyperlink.Id)));
261+
sourceSlide.PutXDocument();
262+
}
263+
264+
var sources = new List<SlideSource>
265+
{
266+
new(new PmlDocument("PB011-Input.pptx", sourceStream.ToArray()), 1, 1, true),
267+
};
268+
var outputPptx = new FileInfo(Path.Combine(TempDir, "PB011-Output.pptx"));
269+
PresentationBuilder.BuildPresentation(sources).SaveAs(outputPptx.FullName);
270+
271+
using var pptx = PresentationDocument.Open(outputPptx.FullName, false);
272+
await Validate(pptx);
273+
274+
var slidePart = pptx.PresentationPart!.SlideParts.Single();
275+
var relId = (string)slidePart.GetXDocument().Descendants(AM3D.attrSrcUrl).Single().Attribute(R.id);
276+
var copied = slidePart.HyperlinkRelationships.Single(h => h.Id == relId);
277+
await Assert.That(copied.Uri).IsEqualTo(sourceUrl);
278+
await Assert.That(copied.IsExternal).IsTrue();
279+
}
280+
281+
[Test]
282+
public async Task PB012_SlideWithWebExtension()
283+
{
284+
var snapshotUrl = new Uri("https://example.com/snapshot.png");
285+
var snapshot = Convert.FromBase64String(
286+
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg=="
287+
);
288+
var webExtension = new XElement(
289+
WE.webextension,
290+
new XAttribute(XNamespace.Xmlns + "we", WE.we),
291+
new XAttribute(XNamespace.Xmlns + "r", R.r),
292+
new XAttribute("id", "{6F1B2A5C-0B5E-4C0E-9A57-2D0C3E6B7A11}"),
293+
new XElement(
294+
WE.reference,
295+
new XAttribute("id", "wa000000000"),
296+
new XAttribute("version", "1.0.0.0"),
297+
new XAttribute("store", "en-US"),
298+
new XAttribute("storeType", "OMEX")
299+
),
300+
new XElement(WE.alternateReferences),
301+
new XElement(WE.properties),
302+
new XElement(WE.bindings),
303+
new XElement(WE.snapshot)
304+
);
305+
306+
var sourceDir = new DirectoryInfo("../../../../TestFiles/");
307+
using var sourceStream = new MemoryStream();
308+
sourceStream.Write(File.ReadAllBytes(Path.Combine(sourceDir.FullName, "PB001-Input1.pptx")));
309+
using (var source = PresentationDocument.Open(sourceStream, true))
310+
{
311+
var presentationPart = source.PresentationPart!;
312+
var firstSlideId = (string)
313+
presentationPart.GetXDocument().Root!.Element(P.sldIdLst)!.Elements(P.sldId).First().Attribute(R.id);
314+
var sourceSlide = (SlidePart)presentationPart.GetPartById(firstSlideId);
315+
316+
var webExtensionPart = sourceSlide.AddNewPart<WebExtensionPart>();
317+
var snapshotPart = webExtensionPart.AddImagePart(ImagePartType.Png);
318+
using (var snapshotStream = new MemoryStream(snapshot))
319+
snapshotPart.FeedData(snapshotStream);
320+
var snapshotLink = webExtensionPart.AddExternalRelationship(
321+
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
322+
snapshotUrl
323+
);
324+
webExtension
325+
.Element(WE.snapshot)!
326+
.Add(
327+
new XAttribute(R.embed, webExtensionPart.GetIdOfPart(snapshotPart)),
328+
new XAttribute(R.link, snapshotLink.Id)
329+
);
330+
webExtensionPart.PutXDocument(new XDocument(webExtension));
331+
332+
var spTree = sourceSlide.GetXDocument().Descendants(P.spTree).Single();
333+
spTree.Add(
334+
new XElement(
335+
P.graphicFrame,
336+
new XElement(
337+
P.nvGraphicFramePr,
338+
new XElement(P.cNvPr, new XAttribute("id", "1000"), new XAttribute("name", "Add-in")),
339+
new XElement(P.cNvGraphicFramePr),
340+
new XElement(P.nvPr)
341+
),
342+
new XElement(
343+
P.xfrm,
344+
new XElement(A.off, new XAttribute("x", "0"), new XAttribute("y", "0")),
345+
new XElement(A.ext, new XAttribute("cx", "914400"), new XAttribute("cy", "914400"))
346+
),
347+
new XElement(
348+
A.graphic,
349+
new XElement(
350+
A.graphicData,
351+
new XAttribute("uri", WE.we.NamespaceName),
352+
new XElement(
353+
WE.webextensionref,
354+
new XAttribute(XNamespace.Xmlns + "we", WE.we),
355+
new XAttribute(R.id, sourceSlide.GetIdOfPart(webExtensionPart))
356+
)
357+
)
358+
)
359+
)
360+
);
361+
sourceSlide.PutXDocument();
362+
}
363+
364+
var sources = new List<SlideSource>
365+
{
366+
new(new PmlDocument("PB012-Input.pptx", sourceStream.ToArray()), 0, 1, true),
367+
};
368+
var outputPptx = new FileInfo(Path.Combine(TempDir, "PB012-Output.pptx"));
369+
PresentationBuilder.BuildPresentation(sources).SaveAs(outputPptx.FullName);
370+
371+
using var pptx = PresentationDocument.Open(outputPptx.FullName, false);
372+
await Validate(pptx, DocumentFormat.OpenXml.FileFormatVersions.Office2013);
373+
374+
var slidePart = pptx.PresentationPart!.SlideParts.Single();
375+
var relId = (string)slidePart.GetXDocument().Descendants(WE.webextensionref).Single().Attribute(R.id);
376+
var copiedPart = slidePart.WebExtensionParts.Single();
377+
await Assert.That(slidePart.GetIdOfPart(copiedPart)).IsEqualTo(relId);
378+
379+
var copiedXml = copiedPart.GetXDocument().Root!;
380+
await Assert.That((string)copiedXml.Attribute("id")).IsEqualTo((string)webExtension.Attribute("id"));
381+
await Assert
382+
.That((string)copiedXml.Element(WE.reference)!.Attribute("id"))
383+
.IsEqualTo((string)webExtension.Element(WE.reference)!.Attribute("id"));
384+
385+
var copiedSnapshot = copiedXml.Element(WE.snapshot)!;
386+
var snapshotId = (string)copiedSnapshot.Attribute(R.embed);
387+
await Assert.That(ReadAllBytes(copiedPart.GetPartById(snapshotId))).IsEquivalentTo(snapshot);
388+
389+
var linkedSnapshotId = (string)copiedSnapshot.Attribute(R.link);
390+
var copiedSnapshotLink = copiedPart.ExternalRelationships.Single(r => r.Id == linkedSnapshotId);
391+
await Assert.That(copiedSnapshotLink.Uri).IsEqualTo(snapshotUrl);
392+
}
393+
245394
private static byte[] ReadAllBytes(OpenXmlPart part)
246395
{
247396
using var stream = part.GetStream(FileMode.Open, FileAccess.Read);

‎Clippit/PowerPoint/Fluent/FluentPresentationBuilder.Copy.cs‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,12 +453,15 @@ IEnumerable<XElement> newContent
453453
|| name == A.blip
454454
|| name == SVG.svgBlip
455455
|| name == AM3D.blip
456+
|| name == WE.snapshot
456457
)
457458
{
458459
CopyRelatedImage(oldContentPart, newContentPart, element, R.embed);
459460
CopyRelatedImage(oldContentPart, newContentPart, element, R.pict);
460461
CopyRelatedImage(oldContentPart, newContentPart, element, R.id);
461462
CopyRelatedImage(oldContentPart, newContentPart, element, O.relid);
463+
if (name == WE.snapshot)
464+
PBT.CopyRelatedMediaExternalRelationship(oldContentPart, newContentPart, element, R.link);
462465
}
463466
else if (name == A.videoFile || name == A.quickTimeFile)
464467
{
@@ -491,6 +494,10 @@ IEnumerable<XElement> newContent
491494
{
492495
PBT.CopyModel3DPart(oldContentPart, newContentPart, element, R.embed);
493496
}
497+
else if (name == WE.webextensionref)
498+
{
499+
CopyWebExtensionPart(oldContentPart, newContentPart, element, R.id);
500+
}
494501
else if (name == P.contentPart)
495502
{
496503
PBT.CopyInkPart(oldContentPart, newContentPart, element, R.id);
@@ -821,6 +828,30 @@ static void PruneCustDataReference(XElement element)
821828
}
822829
}
823830

831+
private void CopyWebExtensionPart(
832+
OpenXmlPart oldContentPart,
833+
OpenXmlPart newContentPart,
834+
XElement webExtensionReference,
835+
XName attributeName
836+
)
837+
{
838+
var relId = (string)webExtensionReference.Attribute(attributeName);
839+
if (newContentPart.HasRelationship(relId) || newContentPart is not SlidePart newSlidePart)
840+
return;
841+
842+
if (
843+
oldContentPart.Parts.FirstOrDefault(p => p.RelationshipId == relId).OpenXmlPart
844+
is not WebExtensionPart oldPart
845+
)
846+
return;
847+
848+
var newPart = newSlidePart.AddNewPart<WebExtensionPart>();
849+
newPart.GetXDocument().Add(oldPart.GetXDocument().Root);
850+
webExtensionReference.Attribute(attributeName).Set(newSlidePart.GetIdOfPart(newPart));
851+
PBT.AddRelationships(oldPart, newPart, [newPart.GetXDocument().Root]);
852+
CopyRelatedPartsForContentParts(oldPart, newPart, [newPart.GetXDocument().Root]);
853+
}
854+
824855
private void CopyRelatedImage(
825856
OpenXmlPart oldContentPart,
826857
OpenXmlPart newContentPart,
@@ -885,6 +916,7 @@ XName attributeName
885916
ThemeOverridePart part => part.AddImagePart(contentType),
886917
ThemePart part => part.AddImagePart(contentType),
887918
VmlDrawingPart part => part.AddImagePart(contentType),
919+
WebExtensionPart part => part.AddImagePart(contentType),
888920
WorksheetPart part => part.AddImagePart(contentType),
889921
_ => null,
890922
};

‎Clippit/PowerPoint/Fluent/PresentationBuilderTools.cs‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ public static List<string> GetSlideIdsInOrder(PresentationDocument srcDoc)
103103
{ A.hlinkClick, new[] { R.id } },
104104
{ A.hlinkMouseOver, new[] { R.id } },
105105
{ A.hlinkHover, new[] { R.id } },
106+
{ AM3D.attrSrcUrl, new[] { R.id } },
106107
{ A.relIds, new[] { R.cs, R.dm, R.lo, R.qs } },
107108
{ C.chart, new[] { R.id } },
108109
{ C.externalData, new[] { R.id } },
@@ -383,7 +384,12 @@ IEnumerable<XElement> newContent
383384
.ToList();
384385
foreach (var e in relevantElements)
385386
{
386-
if (e.Name == A.hlinkClick || e.Name == A.hlinkHover || e.Name == A.hlinkMouseOver)
387+
if (
388+
e.Name == A.hlinkClick
389+
|| e.Name == A.hlinkHover
390+
|| e.Name == A.hlinkMouseOver
391+
|| e.Name == AM3D.attrSrcUrl
392+
)
387393
{
388394
var relId = (string)e.Attribute(R.id);
389395
if (string.IsNullOrEmpty(relId))

‎Clippit/PtOpenXmlUtil.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4049,6 +4049,7 @@ public static class AM3D
40494049
public static readonly XNamespace am3d = "http://schemas.microsoft.com/office/drawing/2017/model3d";
40504050
public static readonly XName model3d = am3d + "model3d";
40514051
public static readonly XName blip = am3d + "blip";
4052+
public static readonly XName attrSrcUrl = am3d + "attrSrcUrl";
40524053
}
40534054

40544055
public static class A1611

0 commit comments

Comments
 (0)