Improved reading and writing large Office files without copying them into memory (#1243)

This commit is contained in:
Thorsten Sommer authored and GitHub committed 2026-11-28 23:03:30 +01:00
1 parent 918572c158
commit dbe609a9a7
37 files changed
+1161 -471

No files matched your search

@@ -139,7 +139,7 @@ internal static partial class PresentationWriter
var bounds = templateSlide.OnSlide(slot).Bounds;
replacement = slot.Role switch
{
PlaceholderRole.PICTURE when index < slide.Pictures.Count && content.Pictures.TryGetValue(slide.Pictures[index], out var picture) => PictureShape(NextShapeIdOf(shapeTree), placeholder, slidePart, picture, bounds, bounds.Area >= content.SlideArea * PresentationTemplate.BACKDROP_AREA_SHARE),
PlaceholderRole.PICTURE when index < slide.Pictures.Count && content.Pictures.TryGet(slide.Pictures[index], out var picture) => PictureShape(NextShapeIdOf(shapeTree), placeholder, slidePart, content.Pictures, picture, bounds, bounds.Area >= content.SlideArea * PresentationTemplate.BACKDROP_AREA_SHARE),
PlaceholderRole.TABLE when index == 0 && slide.Table is { Headers.Count: > 0 } table => TableShape(NextShapeIdOf(shapeTree), placeholder, table, bounds, content.TableStyleId),
_ => null,
@@ -246,8 +246,10 @@ internal static partial class PresentationWriter
/// <remarks>
/// A shape refers to parts of the file by relationship IDs: a picture to its image, a chart to
/// its data, a diagram to its drawing. A part which nothing else on the slide refers to leaves
/// the slide; the file drops it when no other slide refers to it either. An animation of the
/// shape would point into nothing afterwards, which PowerPoint repairs by dropping every
/// the slide; the file drops it when no other slide refers to it either. A video or a sound is
/// no part of the slide but of the file, which the slide only refers to; once no slide refers to
/// it anymore, the Open XML SDK drops it when it saves. The same goes for links. An animation of
/// the shape would point into nothing afterwards, which PowerPoint repairs by dropping every
/// animation of the slide, so that is done here right away.
/// </remarks>
private static void RemoveShape(SlidePart slidePart, OpenXmlElement shape)
@@ -262,12 +264,24 @@ internal static partial class PresentationWriter
{
if (slidePart.TryGetPartById(relationshipId, out var part))
slidePart.DeletePart(part);
else if (ReferenceRelationshipOf(slidePart, relationshipId) is { } reference)
slidePart.DeleteReferenceRelationship(reference);
}
if (slide?.Timing?.Descendants().Any(element => element.GetAttributes().Any(attribute => attribute.LocalName == "spid" && shapeIds.Contains(attribute.Value ?? string.Empty))) is true)
slide.Timing = null;
}
/// <summary>
/// The relationship of a slide with the given ID which leads to no part of the slide: to a video or a sound of the
/// file, or to a link.
/// </summary>
/// <returns>The relationship; null when the slide has no such relationship.</returns>
private static ReferenceRelationship? ReferenceRelationshipOf(SlidePart slidePart, string relationshipId) => slidePart.DataPartReferenceRelationships
.Concat<ReferenceRelationship>(slidePart.HyperlinkRelationships)
.Concat(slidePart.ExternalRelationships)
.FirstOrDefault(relationship => relationship.Id == relationshipId);
/// <summary>
/// The relationship IDs an element and everything within it refer to.
/// </summary>