From 0cc5c078fc4bbd3a5dc027d889f3e94e6bf05f1f Mon Sep 17 00:00:00 2001 From: mysoftlab Date: Sun, 12 Jan 2025 17:52:11 +0800 Subject: [PATCH] Fixes #1472. Optimize CT_Row.Copy to reduce row cloning time under 1ms --- OpenXmlFormats/Wordprocessing/Table.cs | 19 +++++++------------ ooxml/XWPF/Usermodel/XWPFTableRow.cs | 10 ++++++++++ 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/OpenXmlFormats/Wordprocessing/Table.cs b/OpenXmlFormats/Wordprocessing/Table.cs index 3e1e68527..2e609e03f 100644 --- a/OpenXmlFormats/Wordprocessing/Table.cs +++ b/OpenXmlFormats/Wordprocessing/Table.cs @@ -5521,6 +5521,10 @@ public class CT_Row private byte[] paraIdField; private byte[] textIdField; + public XmlNode XmlNode { get; private set; } + + public XmlNamespaceManager NamespaceManager { get; private set; } + public CT_Row() { this.itemsElementNameField = new List(); @@ -5537,6 +5541,8 @@ public static CT_Row Parse(XmlNode node, XmlNamespaceManager namespaceManager, o if (node == null) return null; CT_Row ctObj = new CT_Row(); + ctObj.XmlNode = node; + ctObj.NamespaceManager = namespaceManager; if (parent != null) ctObj.parent = parent; if (node.Attributes["w14:paraId"] != null) @@ -5775,18 +5781,7 @@ internal void Write(StreamWriter sw, string nodeName) } public CT_Row Copy() { - CT_Row ctRow = new CT_Row(); - ctRow.paraIdField = this.paraIdField?.ToArray(); - ctRow.rsidRField = this.rsidRField?.ToArray(); - ctRow.rsidDelField = this.rsidDelField?.ToArray(); - ctRow.rsidRPrField = this.rsidRPrField?.ToArray(); - ctRow.rsidTrField = this.rsidTrField?.ToArray(); - ctRow.textIdField = this.textIdField?.ToArray(); - ctRow.trPrField = this.trPrField?.Copy(); - ctRow.tblPrExField = this.tblPrExField?.Copy(); - ctRow.itemsElementNameField = this.itemsElementNameField?.Copy(); - ctRow.itemsField = this.itemsField?.Copy(); - return ctRow; + return Parse(XmlNode, NamespaceManager, Parent); } public void RemoveTc(int pos) { diff --git a/ooxml/XWPF/Usermodel/XWPFTableRow.cs b/ooxml/XWPF/Usermodel/XWPFTableRow.cs index 4995e3ae2..c19b6d6e5 100644 --- a/ooxml/XWPF/Usermodel/XWPFTableRow.cs +++ b/ooxml/XWPF/Usermodel/XWPFTableRow.cs @@ -115,6 +115,16 @@ public XWPFTableRow CloneRow() return clonedRow; } + /** + * Clones a table row and inserts it at the specified position in the table + */ + public XWPFTableRow CloneRow(int pos) + { + XWPFTableRow clonedRow= new XWPFTableRow(ctRow.Copy(), this.table); + table.AddRow(clonedRow, pos); + return clonedRow; + } + /** * This element specifies the height of the current table row within the * current table. This height shall be used to determine the resulting