翻譯|使用教程|編輯:胡濤|2022-08-11 10:35:26.277|閱讀 213 次
概述:本文主要介紹新方法在 C# 中獲取 Word 文檔中內容控件的別名、標簽和 ID
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
內容控件為您提供了一種設計文檔的方法。當您向文檔添加內容控件時,該控件由邊框、標題和臨時文本標識,這些文本可以向用戶提供說明,并且可以防止用戶編輯或刪除文檔的受保護部分。
將文檔或模板的部分內容綁定到數據。您可以將內容控件綁定到數據庫字段、.NET Framework 中的托管對象、存儲在文檔中的 XML 元素以及其他數據源。
本文說明了如何通過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; } } } } }
歡迎下載|體驗更多E-iceblue產品
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn