From 7ae136b9aa272d76327b254141eb635744b0b8e1 Mon Sep 17 00:00:00 2001 From: Mark Kidder <83427558+mark-sil@users.noreply.github.com> Date: Thu, 26 Sep 2024 16:48:10 -0400 Subject: [PATCH] Word Export LT-21942: Fix export failure (#169) Fixed the problem with some pictures causing the export to fail. Change-Id: Ic15ae717a686161f7a20ba080271e862801f72e4 --- Src/xWorks/LcmWordGenerator.cs | 86 +++++++++++++++++----------------- 1 file changed, 44 insertions(+), 42 deletions(-) diff --git a/Src/xWorks/LcmWordGenerator.cs b/Src/xWorks/LcmWordGenerator.cs index bb246eccf3..0e4c180721 100644 --- a/Src/xWorks/LcmWordGenerator.cs +++ b/Src/xWorks/LcmWordGenerator.cs @@ -403,7 +403,7 @@ public void Append(IFragment frag) if (elem.Descendants().Any()) { // then need to append image in such a way that the relID is maintained - this.DocBody.AppendChild(CloneImageRun(frag, elem)); + this.DocBody.AppendChild(CloneImageElement(frag, elem)); // wordWriter.WordFragment.AppendPhotoToParagraph(frag, elem, wordWriter.ForceNewParagraph); } @@ -417,19 +417,25 @@ public void Append(IFragment frag) /// /// Append a table to the doc fragment. /// - public void AppendTable(WP.Table table) + /// If the table contains pictures, then this is the fragment + /// where we copy the picture data from. + /// The table to append. + public void AppendTable(IFragment copyFromFrag, WP.Table table) { // Deep clone the run b/c of its tree of properties and to maintain styles. - this.DocBody.AppendChild(table.CloneNode(true)); + this.DocBody.AppendChild(CloneElement(copyFromFrag, table)); } /// /// Append a paragraph to the doc fragment. /// - public void AppendParagraph(WP.Paragraph para) + /// If the paragraph contains pictures, then this is the fragment + /// where we copy the picture data from. + /// The paragraph to append. + public void AppendParagraph(IFragment copyFromFrag, WP.Paragraph para) { // Deep clone the run b/c of its tree of properties and to maintain styles. - this.DocBody.AppendChild(para.CloneNode(true)); + this.DocBody.AppendChild(CloneElement(copyFromFrag, para)); } @@ -487,39 +493,48 @@ public void AppendToParagraph(IFragment fragToCopy, OpenXmlElement run, bool for lastPar.AppendChild(CloneElement(fragToCopy, run)); } - public OpenXmlElement CloneElement(IFragment fragToCopy, OpenXmlElement elem) + /// + /// Does a deep clone of the element. If there is picture data then that is cloned + /// from the copyFromFrag into 'this' frag. + /// + /// If the element contains pictures, then this is the fragment + /// where we copy the picture data from. + /// Element to clone. + /// The cloned element. + public OpenXmlElement CloneElement(IFragment copyFromFrag, OpenXmlElement elem) { if (elem.Descendants().Any()) { - return CloneImageRun(fragToCopy, elem); + return CloneImageElement(copyFromFrag, elem); } - return elem.CloneNode(true); - } /// - /// Clones and returns a run containing an image. + /// Clones and returns a element containing an image. /// - public OpenXmlElement CloneImageRun(IFragment fragToCopy, OpenXmlElement run) + /// The fragment where we copy the picture data from. + /// Element to clone. + /// The cloned element. + public OpenXmlElement CloneImageElement(IFragment copyFromFrag, OpenXmlElement elem) { - var clonedRun = run.CloneNode(true); - clonedRun.Descendants().ToList().ForEach( + var clonedElem = elem.CloneNode(true); + clonedElem.Descendants().ToList().ForEach( blip => { var newRelation = - CopyImage(DocFrag, blip.Embed, ((DocFragment)fragToCopy).DocFrag); + CopyImage(DocFrag, blip.Embed, ((DocFragment)copyFromFrag).DocFrag); // Update the relationship ID in the cloned blip element. blip.Embed = newRelation; }); - clonedRun.Descendants().ToList().ForEach( + clonedElem.Descendants().ToList().ForEach( imageData => { - var newRelation = CopyImage(DocFrag, imageData.RelationshipId, ((DocFragment)fragToCopy).DocFrag); + var newRelation = CopyImage(DocFrag, imageData.RelationshipId, ((DocFragment)copyFromFrag).DocFrag); // Update the relationship ID in the cloned image data element. imageData.RelationshipId = newRelation; }); - return clonedRun; + return clonedElem; } /// @@ -970,11 +985,7 @@ public IFragment AddSenseData(ConfigurableDictionaryNode config, IFragment sense if (inAPara) { - List elements = SeparateIntoFirstLevelElements(newPara, senseContent as DocFragment, config); - foreach (OpenXmlElement elem in elements) - { - senseData.DocBody.Append(elem); - } + SeparateIntoFirstLevelElements(senseData, newPara, senseContent as DocFragment, config); } else { @@ -1017,11 +1028,7 @@ config.DictionaryNodeOptions is IParaOption && if (newPara != null) { - List elements = SeparateIntoFirstLevelElements(newPara, content as DocFragment, config); - foreach (OpenXmlElement elem in elements) - { - collData.DocBody.Append(elem); - } + SeparateIntoFirstLevelElements(collData, newPara, content as DocFragment, config); } else { @@ -1429,14 +1436,14 @@ public void AddEntryData(IFragmentWriter writer, List - /// The first paragraph that will be returned. The content will be added + /// The fragment where the new elements will be added. + /// The first paragraph that will be added to 'copyToFrag'. Content from contentToAdd will be added /// to this paragraph until a un-nestable type is encountered. /// The content to add either to the paragraph or at the same level as the paragraph. - /// A list of elements that contain the original outputParagraph and the contentToAdd. The contentToAdd - /// might all be added to the outputParagraph, or it might also result in additional un-nestable types being returned - /// in the list. - public List SeparateIntoFirstLevelElements(WP.Paragraph outputParagraph, DocFragment contentToAdd, ConfigurableDictionaryNode node) + public void SeparateIntoFirstLevelElements(DocFragment copyToFrag, WP.Paragraph firstParagraph, DocFragment contentToAdd, ConfigurableDictionaryNode node) { - List retList = new List(); bool continuationParagraph = false; - var workingParagraph = outputParagraph; + var workingParagraph = firstParagraph; var elements = ((DocFragment)contentToAdd).DocBody.Elements(); foreach (OpenXmlElement elem in elements) { @@ -2272,11 +2276,11 @@ public List SeparateIntoFirstLevelElements(WP.Paragraph outputPa // End the current working paragraph and add it to the list. if (EndParagraph(workingParagraph, node, continuationParagraph)) { - retList.Add(workingParagraph); + copyToFrag.DocBody.AppendChild(workingParagraph); } // Add the un-nestable element. - retList.Add(elem.CloneNode(true)); + copyToFrag.DocBody.AppendChild(copyToFrag.CloneElement(contentToAdd, elem)); // Start a new working paragraph. continuationParagraph = true; @@ -2284,7 +2288,7 @@ public List SeparateIntoFirstLevelElements(WP.Paragraph outputPa } else { - workingParagraph.AppendChild(contentToAdd.CloneElement(contentToAdd, elem)); + workingParagraph.AppendChild(copyToFrag.CloneElement(contentToAdd, elem)); } } @@ -2292,10 +2296,8 @@ public List SeparateIntoFirstLevelElements(WP.Paragraph outputPa // it to the return list. if (EndParagraph(workingParagraph, node, continuationParagraph)) { - retList.Add(workingParagraph); + copyToFrag.DocBody.AppendChild(workingParagraph); } - - return retList; } ///