Word控件Spire.Doc 【文本】教程(12) ;新方法在 C# 中获取 Word 文档中内容控件的别名、标签和 ID
内容控件为您提供了一种设计文档的方法。当您向文档添加内容控件时,该控件由边框、标题和临时文本标识,这些文本可以向用户提供说明,并且可以防止用户编辑或删除文档的受保护部分。
将文档或模板的部分内容绑定到数据。您可以将内容控件绑定到数据库字段、.NET Framework 中的托管对象、存储在文档中的 XML 元素以及其他数据源。
Spire.Doc for.NET 最新下载https://www.evget.com/product/3368/download
本文说明了如何通过Spire.Doc使用新方法获取所有控件及其属性,包括别名、id 和标签,并将 Barcode 替换为另一个图像。
参考这篇文章检查旧方法: Get alias, tag and id of content controls in a Word document in C#
以下是测试文件new.docx。
以下是步骤:
第 1 步:创建一个新的 Word 文档并加载测试文件。
Document doc = new Document(@"new.docx");
第 2 步:创建一个列表 StructureDocument 来存储标签。在这里,每个内容控件都将由标签标识。
public class StructureTags { List m_structureDocumnt; public List StructureDocument { get { if (m_structureDocumnt == null) m_structureDocumnt = new List();return m_structureDocumnt; } }}
第 3 步:使用foreach语句获取Word文档中的所有标签。
foreach (Section section in doc.Sections) { foreach (Body body in section.ChildObjects) { ModifyBody(body); } }
第 4 步:显示所有控件的属性。
List tagInlines = structureTags.StructureDocument; Console.WriteLine("Part1"); for (int i = 0; i < tagInlines.Count; i++) { string alias = tagInlines[i].SDTProperties.Alias; // Can be null or empty decimal id = tagInlines[i].SDTProperties.Id; string tag = tagInlines[i].SDTProperties.Tag; string STDType = tagInlines[i].SDTProperties.SDTType.ToString();Console.WriteLine("{0,20},{1,15},{2, 10} - {3}", alias, id, STDType, tag); Console.ReadKey(); }
第 5 步:替换图片内容控件内的图像。
doc.SaveToFile("replace1.docx", FileFormat.Docx2013); System.Diagnostics.Process.Start("replace1.docx");
结果截图:
完整代码:
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; using System; using System.Drawing; namespace GetAlias {class Program {static StructureTags structureTags = new StructureTags(); static void Main(string[] args) { Document doc = new Document(@"new.docx");foreach (Section section in doc.Sections) { foreach (Body body in section.ChildObjects) { ModifyBody(body); } }List tagInlines = structureTags.StructureDocument; Console.WriteLine("Part1"); for (int i = 0; i < tagInlines.Count; i++) { string alias = tagInlines[i].SDTProperties.Alias; decimal id = tagInlines[i].SDTProperties.Id; string tag = tagInlines[i].SDTProperties.Tag; string STDType = tagInlines[i].SDTProperties.SDTType.ToString();Console.WriteLine("{0,20},{1,15},{2, 10} - {3}", alias, id, STDType, tag); Console.ReadKey();if (tagInlines[i].SDTProperties.SDTType == SdtType.Picture) { DocPicture picture = tagInlines[i].ChildObjects.FirstItem as DocPicture; if (picture == null) { picture = new DocPicture(doc); picture.LoadImage(Image.FromFile(@"cat.jpg")); tagInlines[i].ChildObjects.Add(picture); } else { picture.LoadImage(Image.FromFile(@"cat.jpg")); } } }doc.SaveToFile("replace1.docx", FileFormat.Docx2013); System.Diagnostics.Process.Start("replace1.docx"); }static void ModifyBody(Body body) { if (body == null) return;foreach (DocumentObject docObj in body.ChildObjects) { if (docObj is StructureDocumentTag) { structureTags.StructureDocument.Add(docObj as StructureDocumentTag);} else if (docObj is Table) { ModifyTable(docObj as Table); } else if (docObj is Paragraph) { ModifyParagraph(docObj as Paragraph); } } }static void ModifyTable(Table table) { if (table == null) return;foreach (TableRow row in table.Rows) { foreach (TableCell cell in row.Cells) { if (cell is StructureDocumentTagCell) { structureTags.StructureDocument.Add(cell as StructureDocumentTagCell); } else { ModifyBody(cell); } } } }static void ModifyParagraph(Paragraph para) { if (para == null) return;foreach (DocumentObject docObj in para.ChildObjects) { if (docObj is StructureDocumentTagInline) {structureTags.StructureDocument.Add(docObj as StructureDocumentTagInline); } } }public class StructureTags { List m_structureDocumnt; public List StructureDocument { get { if (m_structureDocumnt == null) m_structureDocumnt = new List();return m_structureDocumnt; } } }} }
如有您有产品试用,授权等相关问题,欢迎私聊我获取~
Word控件Spire.Doc 【文本】教程(12) ;新方法在 C# 中获取 Word 文档中内容控件的别名、标签和 ID相关推荐
- Word控件Spire.Doc 转换教程(二十三):保留或禁用从 Word 到 PDF 转换的超链接
超链接为读者提供了更多的附加信息,它被广泛用于我们的 word 文档.Spire.Doc 具有强大的超链接文字元素操作功能.开发者可以在word文档中添加.修改和删除超链接.Spire.Doc for ...
- 微软文本检索_如何在Microsoft Word中引用其他文档中的文本
微软文本检索 You probably have some text that you type often in your Word documents, such as addresses. In ...
- word文档怎么给数字加千分符_Word中如何将文档中的金额数值设置为财务数字中的千分位格式...
Word中如何将文档中的金额数值设置为财务数字中的千分位格式 时间:2015-06-10 作者:snow 来源:互联网 如图6‑19所示为某公司给省商务厅上报的2013年农产品现代流通综合试点 ...
- Word控件Spire.Doc 转换教程(二十五):在 C#、VB.NET 中将 ODT 转换为 DOC
具有 .ODT 文件扩展名的文件是 OpenDocument 文本文档文件.这些文件通常由免费的 OpenOffice Writer 文字处理器程序创建.ODT 文件类似于与 Microsoft Wo ...
- Word控件Spire.Doc 转换教程(十二):如何将 Word 转换为 EPub、XPS、Emf
本文将介绍一种通过名为 Spire.Doc 的强大且独立的Word .NET 组件将 Word 转换为 EPub.XPS.Emf的简单方法,无需在机器上安装 Microsoft Word.它还支持将 ...
- Word控件Spire.Doc 转换教程(六):如何将 XML 转换为 Word
为什么使用 C#/VB.NET 将 Office OpenXML 转换为 Word? 作为一种出色的编程语言,Office OpenXML 在其自然形式下很难导航和访问.将文件从 Office Ope ...
- Word控件Spire.Doc 转换教程(九):在 Doc 转PDF 转换期间设置图像质量
您可能会好奇为什么我们在将Doc 转换为 PDF时设置图像质量.在我们转换Word文档后,尤其是文档中有很多图像,输出的PDF文档的大小明显大于原始的Doc文件.现在,Spire.Doc 新版本的新功 ...
- 使用pymupdf获取pdf文档中的文本下划线信息(全网唯一解决方案)
1,问题描述 最近,公司需要对一批pdf文档进行解析,获取其中文字,并再展示到前端页面上.如果单纯地提取文字,其实非常容易,但麻烦的在于保存原有文档中的文本格式,例如加粗.斜体.下划线,以及三者的各种 ...
- Word控件Spire.Doc 转换教程(七):如何将 Word 转换为 RTF
为什么要将 Word 转换为 RTF? 自 1990 年代以来,Microsoft 开始为其一系列专有的文字处理格式使用 .doc 扩展名.Doc 格式通常与 Microsoft Office 应用程 ...
- Word控件Spire.Doc 转换教程(十八): 将 RTF 转换为图像并重置图像分辨率
Spire.Doc具有在 C# 和 VB.NET 中操作 RTF 文件格式的强大能力.通过使用 Spire.Doc,开发人员可以将 RTF 转换为 PDF.HTML和 .doc..docx 格式的 w ...
最新文章
- 2w字长文,让你瞬间拥有「调用链」开发经验
- 2020 互联网应届硕士的薪资情况
- Java多线程相关的常用接口
- HDU - 4825 Xor Sum(字典树)
- DotNetAnywhere:可供选择的 .NET 运行时
- puppeteer执行js_使用Node.js和Puppeteer与表单和网页进行交互– 1
- 使用PlantUML和VS Code轻松实现UML
- PBRNet:Progressive Boundary Refinement Network for Temporal Action Detection (AAAI 2020)
- 80386的内存分页机制
- 初级程序员面试不靠谱指南(二)
- 一个字等于多少字节?
- C++定义点和矩形求矩形面积周长
- NLPIR+Hadoop
- Java创建SpringBoot服务时yml文件显示粉色不显示绿色小叶子
- 2020年区块链行业十大趋势
- 优化了的过关键点的光滑曲线拟合算法
- 重磅自动驾驶数据集来了!全球首个多传感器虚拟标注数据集免费开放
- 无人驾驶技术——无损卡尔曼滤波(UKF)
- python魔方方法超详细介绍
- NOIP 2011 聪明的质检员
热门文章
- android百度地图画圆,Android应用开发之android 百度地图自定义圆,更改默认图标等常用方法...
- 一个正在学习php菜鸟的自述
- 建筑业营业税改征增值税调整的通知(一)〔2018〕392号
- python 日期和时间处理(time,datetime模块讲解)
- 华北科技学院计算机专业录取分数线,华北科技学院2018年在河北省各专业录取分数线...
- 压缩包密码应该如何删除?
- python浮点数怎么运算_python中实现精确的浮点数运算详解
- AI 建模师 素养手冊(1)
- 苹果手机系统更新导致无服务器,IOS更新频繁出问题,IOS系统到底怎么了?看完就明白!...
- CNN全连接层怎么转化成一维向量?