獲取 Word 文檔中內(nèi)容控件的別名、標簽和 ID
內(nèi)容控件為您提供了一種設(shè)計文檔的方法。當您向文檔添加內(nèi)容控件時,該控件由邊框、標題和臨時文本標識,這些文本可以向用戶提供說明,并且可以防止用戶編輯或刪除文檔的受保護部分。
將文檔或模板的部分內(nèi)容綁定到數(shù)據(jù)。您可以將內(nèi)容控件綁定到數(shù)據(jù)庫字段、.NET Framework 中的托管對象、存儲在文檔中的 XML 元素以及其他數(shù)據(jù)源。
本文說明了如何通過Spire.Doc使用新方法獲取所有控件及其屬性,包括別名、id 和標簽,并將 Barcode 替換為另一個圖像。
參考這篇文章檢查舊方法: Get alias, tag and id of content controls in a Word document in C#
以下是測試文件new.docx。
  
 
	
以下是步驟:
第 1 步:創(chuàng)建一個新的 Word 文檔并加載測試文件。
Document doc = new Document(@"new.docx");
第 2 步:創(chuàng)建一個列表 StructureDocument 來存儲標簽。在這里,每個內(nèi)容控件都將由標簽標識。
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 步:替換圖片內(nèi)容控件內(nèi)的圖像。
doc.SaveToFile("replace1.docx", FileFormat.Docx2013);
System.Diagnostics.Process.Start("replace1.docx");
結(jié)果截圖:
  
 
	
  
 
完整代碼:
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;
}
}
}
}
}
                
            
 QQ交談
QQ交談 在線咨詢
在線咨詢 
                 
                
 渝公網(wǎng)安備
            50010702500608號
渝公網(wǎng)安備
            50010702500608號
             
            
 客服熱線
客服熱線