|
| 1 | +// Patches relationship files for PowerPoint parts (presentation, slides, |
| 2 | +// slide layouts, slide masters, notes, themes, etc.). Handles any .rels file |
| 3 | +// under ppt/ — e.g. ppt/_rels/presentation.xml.rels, |
| 4 | +// ppt/slides/_rels/slide1.xml.rels, ppt/slideMasters/_rels/slideMaster1.xml.rels. |
| 5 | +// |
| 6 | +// These .rels files contain non-deterministic relationship IDs generated by |
| 7 | +// OpenXml SDK methods such as AddPart. This patcher renumbers them to |
| 8 | +// deterministic "DeterministicId{n}" values and stores the old→new mapping |
| 9 | +// so the corresponding content patcher (PptxContentPatcher) can remap r:id |
| 10 | +// references in the matching XML content file. |
| 11 | +class PptxRelationshipPatcher : IPatcher |
| 12 | +{ |
| 13 | + // Keyed by content file name (e.g. "ppt/slides/slide1.xml") → old-to-new ID mapping |
| 14 | + internal Dictionary<string, Dictionary<string, string>> IdMappings { get; } = new(StringComparer.OrdinalIgnoreCase); |
| 15 | + |
| 16 | + public bool IsMatch(Entry entry) => |
| 17 | + entry.FullName.StartsWith("ppt/") && |
| 18 | + entry.FullName.Contains("/_rels/") && |
| 19 | + entry.FullName.EndsWith(".xml.rels"); |
| 20 | + |
| 21 | + public void PatchXml(XDocument xml, string entryName) |
| 22 | + { |
| 23 | + var mapping = RelationshipRenumber.RenumberAndSort(xml, entryName); |
| 24 | + |
| 25 | + // Derive content file path from .rels path: |
| 26 | + // "ppt/slides/_rels/slide1.xml.rels" → "ppt/slides/slide1.xml" |
| 27 | + // "ppt/_rels/presentation.xml.rels" → "ppt/presentation.xml" |
| 28 | + var contentName = entryName |
| 29 | + .Replace("/_rels/", "/") |
| 30 | + .Replace(".xml.rels", ".xml"); |
| 31 | + IdMappings[contentName] = mapping; |
| 32 | + } |
| 33 | +} |
0 commit comments