From 408ec5aaa9cff0e359c1e837074b161de18ed609 Mon Sep 17 00:00:00 2001 From: Tony Qu Date: Sun, 6 Mar 2022 10:25:54 +0800 Subject: [PATCH] parse AlternativeContent under CT_Run fix #769 --- OpenXmlFormats/Drawing/SpreadsheetDrawing.cs | 7 ++-- OpenXmlFormats/Vml/CT_AlternateContent.cs | 43 ++++++++++++++++++++ OpenXmlFormats/Vml/SpreadsheetDrawing.cs | 36 ---------------- OpenXmlFormats/Wordprocessing/Run.cs | 24 +++++++++++ 4 files changed, 71 insertions(+), 39 deletions(-) create mode 100644 OpenXmlFormats/Vml/CT_AlternateContent.cs diff --git a/OpenXmlFormats/Drawing/SpreadsheetDrawing.cs b/OpenXmlFormats/Drawing/SpreadsheetDrawing.cs index b9f6cc908..a897a8ea9 100644 --- a/OpenXmlFormats/Drawing/SpreadsheetDrawing.cs +++ b/OpenXmlFormats/Drawing/SpreadsheetDrawing.cs @@ -7,6 +7,7 @@ using System.Collections.Generic; using System.ComponentModel; using System.Text; using NPOI.OpenXml4Net.Util; +using NPOI.OpenXmlFormats.Vml; namespace NPOI.OpenXmlFormats.Dml.Spreadsheet { @@ -1448,8 +1449,8 @@ namespace NPOI.OpenXmlFormats.Dml.Spreadsheet get { return fromField; } set { fromField = value; } } - Vml.Spreadsheet.CT_AlternateContent alternateContentField = null; - public Vml.Spreadsheet.CT_AlternateContent alternateContent + CT_AlternateContent alternateContentField = null; + public CT_AlternateContent alternateContent { get { @@ -1562,7 +1563,7 @@ namespace NPOI.OpenXmlFormats.Dml.Spreadsheet } else if (childNode.LocalName == "AlternateContent") { - oneCellAnchor.alternateContent = Vml.Spreadsheet.CT_AlternateContent.Parse(childNode, namespaceManager); + oneCellAnchor.alternateContent = CT_AlternateContent.Parse(childNode, namespaceManager); } else if (childNode.LocalName == "clientData") { diff --git a/OpenXmlFormats/Vml/CT_AlternateContent.cs b/OpenXmlFormats/Vml/CT_AlternateContent.cs new file mode 100644 index 000000000..ebe447748 --- /dev/null +++ b/OpenXmlFormats/Vml/CT_AlternateContent.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; +using System.Xml; + +namespace NPOI.OpenXmlFormats.Vml +{ + public class CT_AlternateContent + { + public string InnerXml { get; set; } + public CT_AlternateContent() + { + } + public static CT_AlternateContent Parse(XmlNode node, XmlNamespaceManager namespaceManager) + { + if (node == null) + return null; + + var ac = new CT_AlternateContent(); + if (string.IsNullOrEmpty(node.InnerXml)) + { + return ac; + } + ac.InnerXml = node.InnerXml; + return ac; + } + internal void Write(StreamWriter sw, string nodeName) + { + sw.Write(string.Format("", nodeName)); + } + else + { + sw.Write(">"); + sw.Write(this.InnerXml); + sw.Write(string.Format("", nodeName)); + } + } + } +} diff --git a/OpenXmlFormats/Vml/SpreadsheetDrawing.cs b/OpenXmlFormats/Vml/SpreadsheetDrawing.cs index 2170c06e6..01710a493 100644 --- a/OpenXmlFormats/Vml/SpreadsheetDrawing.cs +++ b/OpenXmlFormats/Vml/SpreadsheetDrawing.cs @@ -9,42 +9,6 @@ using NPOI.OpenXml4Net.Util; namespace NPOI.OpenXmlFormats.Vml.Spreadsheet { - public class CT_AlternateContent - { - public string innerXml { get; set; } - public CT_AlternateContent() - { - } - public static CT_AlternateContent Parse(XmlNode node, XmlNamespaceManager namespaceManager) - { - if (node == null) - return null; - - var ac = new CT_AlternateContent(); - if (string.IsNullOrEmpty(node.InnerXml)) - { - return ac; - } - ac.innerXml = node.InnerXml; - return ac; - } - internal void Write(StreamWriter sw, string nodeName) - { - sw.Write(string.Format("", nodeName)); - } - else - { - sw.Write(">"); - sw.Write(this.innerXml); - sw.Write(string.Format("", nodeName)); - } - - } - - } [System.ComponentModel.DesignerCategory("code")] public class CT_ClientData { diff --git a/OpenXmlFormats/Wordprocessing/Run.cs b/OpenXmlFormats/Wordprocessing/Run.cs index c84c6b54f..62107c544 100644 --- a/OpenXmlFormats/Wordprocessing/Run.cs +++ b/OpenXmlFormats/Wordprocessing/Run.cs @@ -30,6 +30,8 @@ namespace NPOI.OpenXmlFormats.Wordprocessing private byte[] rsidDelField; private byte[] rsidRField; + + Vml.CT_AlternateContent alternateContentField = null; public CT_R() { @@ -50,6 +52,17 @@ namespace NPOI.OpenXmlFormats.Wordprocessing this.rPrField = value; } } + public Vml.CT_AlternateContent alternateContent + { + get + { + return alternateContentField; + } + set + { + this.alternateContentField = value; + } + } [XmlElement("annotationRef", typeof(CT_Empty), Order = 1)] [XmlElement("br", typeof(CT_Br), Order = 1)] @@ -373,6 +386,7 @@ namespace NPOI.OpenXmlFormats.Wordprocessing ctObj.rsidRPr = XmlHelper.ReadBytes(node.Attributes["w:rsidRPr"]); ctObj.rsidDel = XmlHelper.ReadBytes(node.Attributes["w:rsidDel"]); ctObj.rsidR = XmlHelper.ReadBytes(node.Attributes["w:rsidR"]); + foreach (XmlNode childNode in node.ChildNodes) { if (childNode.LocalName == "rPr") @@ -442,6 +456,10 @@ namespace NPOI.OpenXmlFormats.Wordprocessing ctObj.Items.Add(CT_Drawing.Parse(childNode, namespaceManager)); ctObj.ItemsElementName.Add(RunItemsChoiceType.drawing); } + else if (childNode.LocalName == "AlternateContent") + { + ctObj.alternateContent = Vml.CT_AlternateContent.Parse(childNode, namespaceManager); + } else if (childNode.LocalName == "endnoteRef") { ctObj.Items.Add(new CT_Empty()); @@ -551,6 +569,8 @@ namespace NPOI.OpenXmlFormats.Wordprocessing if (this.rPr != null) this.rPr.Write(sw, "rPr"); int i = 0; + + foreach (object o in this.Items) { if ((o is CT_Text) && this.ItemsElementName[i] == RunItemsChoiceType.instrText) @@ -619,6 +639,10 @@ namespace NPOI.OpenXmlFormats.Wordprocessing sw.Write(""); i++; } + if (this.alternateContent != null) + { + this.alternateContent.Write(sw, "AlternateContent"); + } sw.Write(string.Format("", nodeName)); }