我有一个word文件和内容控制复选框。如何使用sprire.doc查找这些复选框?
发布于 2022-05-05 03:04:35
下面的演示说明了如何查找复选框内容控件并使用Spire.Doc更新其复选状态。
using System;
using System.Windows.Forms;
using Spire.Doc;
using Spire.Doc.Documents;
using System.Collections.Generic;
namespace UpdateCheckBox
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Document document = new Document();
document.LoadFromFile("CheckBoxContentControl.docx");
//Get the content controls in the document
StructureTags structureTags = GetAllTags(document);
List<StructureDocumentTagInline> tagInlines = structureTags.tagInlines;
//Loop through the controls
for (int i = 0; i < tagInlines.Count; i++)
{
//Get the control type
string type = tagInlines[i].SDTProperties.SDTType.ToString();
//Update the check state of check box
if (type == "CheckBox")
{
SdtCheckBox scb = tagInlines[i].SDTProperties.ControlProperties as SdtCheckBox;
if (scb.Checked)
{
scb.Checked = false;
}
else
{
scb.Checked = true;
}
}
}
//Save the document
document.SaveToFile("Output.docx", FileFormat.Docx);
//Open the document
WordDocViewer("Output.docx");
}
static StructureTags GetAllTags(Document document)
{
StructureTags structureTags = new StructureTags();
foreach (Section section in document.Sections)
{
foreach (DocumentObject obj in section.Body.ChildObjects)
{
if (obj.DocumentObjectType == DocumentObjectType.Paragraph)
{
foreach (DocumentObject pobj in (obj as Paragraph).ChildObjects)
{
if (pobj.DocumentObjectType == DocumentObjectType.StructureDocumentTagInline)
{
structureTags.tagInlines.Add(pobj as StructureDocumentTagInline);
}
}
}
}
}
return structureTags;
}
public class StructureTags
{
List<StructureDocumentTagInline> m_tagInlines;
public List<StructureDocumentTagInline> tagInlines
{
get
{
if (m_tagInlines == null)
m_tagInlines = new List<StructureDocumentTagInline>();
return m_tagInlines;
}
set
{
m_tagInlines = value;
}
}
}
private void WordDocViewer(string fileName)
{
try
{
System.Diagnostics.Process.Start(fileName);
}
catch { }
}
}
}https://stackoverflow.com/questions/72071081
复制相似问题