After searching the Internet and sniffing the document for a bit, I found that ActiveX control data can be found in the part of the document indicated by the identifier of the control. Determining the type of control is a bit complicated because I did not find any documentation about it. Obviously, you should get the "classid" attribute from the control and try to match it with the classes you know. Below is the code to define the values ββfor the three types of controls. Other identifiers are marked as unknown, and you can intuitively compare them with those that you added to the document.
using System; using System.Collections.Generic; using System.Xml.Linq; using System.Xml; using System.IO; using System.Text; using DocumentFormat.OpenXml; using DocumentFormat.OpenXml.Wordprocessing; using DocumentFormat.OpenXml.Packaging; namespace OpenXMLTest { class Program { const string textBoxId = "{8BD21D10-EC42-11CE-9E0D-00AA006002F3}"; const string radioButtonId = "{8BD21D50-EC42-11CE-9E0D-00AA006002F3}"; const string checkBoxId = "{8BD21D40-EC42-11CE-9E0D-00AA006002F3}"; static void Main(string[] args) { string fileName = @"C:\Users\Andy\Desktop\test_l1demo.docx"; using (WordprocessingDocument doc = WordprocessingDocument.Open(fileName, false)) { foreach (Control control in doc.MainDocumentPart.Document.Body.Descendants()) { Console.WriteLine(); Console.WriteLine("Control {0}:", control.Name); Console.WriteLine("Id: {0}", control.Id); displayControlDetails(doc, control.Id); } } Console.Read(); } private static void displayControlDetails(WordprocessingDocument doc, StringValue controlId) { string classId, type, value; OpenXmlPart part = doc.MainDocumentPart.GetPartById(controlId); OpenXmlReader reader = OpenXmlReader.Create(part.GetStream()); reader.Read(); OpenXmlElement controlDetails = reader.LoadCurrentElement(); classId = controlDetails.GetAttribute("classid", controlDetails.NamespaceUri).Value; switch (classId) { case textBoxId: type = "TextBox"; break; case radioButtonId: type = "Radio Button"; break; case checkBoxId: type = "CheckBox"; break; default: type = "Not known"; break; } value = "No value attribute";
Andrei M
source share