使用OpenXML在Word中获取一个复选框 [英] Get a CheckBox in Word using OpenXML

查看:422
本文介绍了使用OpenXML在Word中获取一个复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用OpenXML获取嵌入在Word文档中的CheckBox控件的句柄?

How does one get a handle to a CheckBox control that's embedded in a Word document using OpenXML?

您会认为Paragraph.ControlPropertiesPart或Paragraph.Descendents()会实现某些目标,但是在每种情况下,我都会返回一个空类型.

You would think that either Paragraph.ControlPropertiesPart or Paragraph.Descendents() would achieve something but in every single case I get a null type returned.

我可以使用实际的XML结构遍历文档树,但这似乎很麻烦.

I can traverse down the document tree using the actual XML structure, but this seems cumbersome.

建议.

推荐答案

以下代码显示了如何枚举word文档中的所有复选框: 在文档的主体上使用Decendants<CheckBox>()方法.

The code below shows how to enumerate all checkboxes in a word document by using the Decendants<CheckBox>() method on the docuement's body.

using (WordprocessingDocument doc = WordprocessingDocument.Open("c:\\temp\\checkbox.docx", true))
{
  foreach (CheckBox cb in doc.MainDocumentPart.Document.Body.Descendants<CheckBox>())
  {
    Console.Out.WriteLine(cb.LocalName);

    FormFieldName cbName = cb.Parent.ChildElements.First<FormFieldName>();
    Console.Out.WriteLine(cbName.Val);

    DefaultCheckBoxFormFieldState defaultState = cb.GetFirstChild<DefaultCheckBoxFormFieldState>();
    Checked state = cb.GetFirstChild<Checked>();

    Console.Out.WriteLine(defaultState.Val.ToString());

    if (state.Val == null) // In case checkbox is checked the val attribute is null
    {
      Console.Out.WriteLine("CHECKED");
    }
    else
    {
      Console.Out.WriteLine(state.Val.ToString());
    }
  }
}

要确定给定复选框输入元素的名称,您必须访问 CheckBox实例的Parent属性,然后搜索FormFieldName元素(要为复选框分配名称,请使用Microsoft Word中的属性"对话框).

To determine the name of a given checkbox input element you have to access the Parent property of the CheckBox instance and then search for the FormFieldName element (to assign a name to a checkbox use the Properties Dialog in Microsoft Word).

DefaultCheckBoxFormFieldState Val属性保留复选框的默认状态. 此外,Checked元素的Val属性保留实际的检查状态. CheckBox实例的名称.请注意,对于Microsoft Word 2007,如果 该复选框处于选中状态.

The DefaultCheckBoxFormFieldState Val property holds the default state for the checkbox. Furthermore the Val property of the Checked element holds the actual checked state of the CheckBox instance. Note, for Microsoft Word 2007 the Val property is null if the checkbox is checked.

开始编辑

我想扩展我的答案.实际上,MS Word开发人员选项卡上有两种复选框控件-传统复选框和ActiveX控件复选框.上面显示的代码可用于枚举word文档中的旧复选框(请参阅此

I'd like to extend my answer. In fact, there are two kinds of checkbox controls on the MS Word developer tab - a legacy checkbox and an ActiveX control checkbox. The code shown above can be used to enumerte legacy checkboxes in a word document (see this article on how to create a legacy checkbox).

据我所知,您不能使用OpenXML SDK来获取/设置ActiveX复选框的值. 但是,您可以使用以下代码枚举ActiveX控件:

As far as I know, you can't use the OpenXML SDK to get/set values for an ActiveX checkbox. However you can enumerate ActiveX controls using the following code:

foreach (Control ctrl in doc.MainDocumentPart.Document.Body.Descendants<Control>())
{
   Console.Out.WriteLine(ctrl.Id);
   Console.Out.WriteLine(ctrl.Name);
   Console.Out.WriteLine(ctrl.ShapeId);
}

要确定给定的Control是否为复选框,您必须选中Control的类ID.复选框的类ID为{8BD21D40-EC42-11CE-9E0D-00AA006002F3}. 这是获取类ID的代码示例(我不知道是否有更简单的方法...):

To determine whether or not a given Control is a checkbox you have to ckeck the class ID of the Control. The class ID of a checkbox is {8BD21D40-EC42-11CE-9E0D-00AA006002F3}. Here is a code sample to get the class ID (I don't know if there is an easier way...):

OpenXmlPart part = doc.MainDocumentPart.GetPartById(ctrl.Id);
OpenXmlReader re = OpenXmlReader.Create(part.GetStream());
re.Read();
OpenXmlElement el = re.LoadCurrentElement();          
if(el.GetAttribute("classid", el.NamespaceUri).Value == "{8BD21D40-EC42-11CE-9E0D-00AA006002F3}")
{
  Console.WriteLine("Checkbox found...");
}
re.Close();

END EDIT

编辑2

我没有意识到Word 2010中有一个新的复选框控件(感谢Dennis 帕尔默).

I didn't realize that there is a new checkbox control in Word 2010 (Thanks to Dennis Palmer).

要枚举这些新的复选框控件,可以使用以下代码:

To enumerate those new checkbox controls you can use the following code:

using (WordprocessingDocument doc = WordprocessingDocument.Open(filename, true))
{
   MainDocumentPart mp = doc.MainDocumentPart;

   foreach(SdtContentCheckBox cb in mp.Document.Body.Descendants<SdtContentCheckBox>())
   {         
     if(cb.Checked.Val == "1");
     {
       Console.Out.WriteLine("CHECKED");  
     }           
   }
}

END EDIT 2

希望,这会有所帮助.

这篇关于使用OpenXML在Word中获取一个复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆