使用C#代碼向Word文檔添加文檔屬性的操作指南
文檔屬性(也稱為元數(shù)據(jù)) 是指描述文檔的一組信息。所有 Word 文檔都自帶一組內(nèi)置的文檔屬性,包括標(biāo)題、作者、主題、關(guān)鍵詞等。除了內(nèi)置文檔屬性之外,Microsoft Word 還允許用戶為 Word 文檔添加自定義文檔屬性。
在本文中,我們將介紹如何使用 Spire.Doc for .NET,通過 C# 和 VB.NET 向 Word 文檔添加這些文檔屬性。
安裝 Spire.Doc for .NET
首先,你需要將 Spire.Doc for .NET 包中包含的 DLL 文件添加為 .NET 項(xiàng)目的引用。這些 DLL 文件可以通過此鏈接下載,或通過 NuGet 進(jìn)行安裝。
PM> Install-Package Spire.Doc
在 C# 和 VB.NET 中向 Word 文檔添加內(nèi)置文檔屬性
內(nèi)置文檔屬性由名稱和值組成。由于其名稱是由 Microsoft Word 預(yù)先定義的,因此無法設(shè)置或更改內(nèi)置文檔屬性的名稱,但可以設(shè)置或修改其對應(yīng)的值。
示例代碼如下:
using Spire.Doc;
namespace BuiltinDocumentProperties
{
class Program
{
static void Main(string[] args)
{
//創(chuàng)建一個(gè) Document 實(shí)例
Document document = new Document();
//加載 Word 文檔
document.LoadFromFile("Sample.docx");
//向文檔添加內(nèi)置文檔屬性
BuiltinDocumentProperties standardProperties = document.BuiltinDocumentProperties;
standardProperties.Title = "添加文檔屬性";
standardProperties.Subject = "C# 示例";
standardProperties.Author = "張三";
standardProperties.Company = "Eiceblue";
standardProperties.Manager = "李四";
standardProperties.Category = "文檔操作";
standardProperties.Keywords = "C#, Word, 文檔屬性";
standardProperties.Comments = "本文演示了如何添加文檔屬性";
//保存生成的文檔
document.SaveToFile("StandardDocumentProperties.docx", FileFormat.Docx2013);
}
}
}在 C# 和 VB.NET 中向 Word 文檔添加自定義文檔屬性
自定義文檔屬性可以由文檔作者或用戶自行定義。每個(gè)自定義文檔屬性都應(yīng)包含名稱、值和數(shù)據(jù)類型。數(shù)據(jù)類型可以是以下四種之一:文本(Text)、日期(Date)、數(shù)字(Number) 和 是/否(Yes or No)。
示例代碼如下:
using Spire.Doc;
using System;
namespace CustomDocumentProperties
{
class Program
{
static void Main(string[] args)
{
//創(chuàng)建一個(gè) Document 實(shí)例
Document document = new Document();
//加載 Word 文檔
document.LoadFromFile("Sample.docx");
//向文檔添加自定義文檔屬性
CustomDocumentProperties customProperties = document.CustomDocumentProperties;
customProperties.Add("Document ID", 1);
customProperties.Add("Authorized", true);
customProperties.Add("Authorized By", "John Smith");
customProperties.Add("Authorized Date", DateTime.Today);
//保存生成的文檔
document.SaveToFile("CustomDocumentProperties.docx", FileFormat.Docx2013);
}
}
}到此這篇關(guān)于使用C#代碼向Word文檔添加文檔屬性的操作指南的文章就介紹到這了,更多相關(guān)C#向Word添加文檔屬性內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#高效實(shí)現(xiàn)Excel數(shù)據(jù)讀取入門教程
在?C#?開發(fā)中,Excel?數(shù)據(jù)處理是高頻場景—,免費(fèi)版?Free?Spire.XLS恰好能解決這些痛點(diǎn),下面小編就來詳細(xì)介紹如何用它實(shí)現(xiàn)Excel數(shù)據(jù)讀取吧2025-09-09
winform中的ListBox和ComboBox綁定數(shù)據(jù)用法實(shí)例
這篇文章主要介紹了winform中的ListBox和ComboBox綁定數(shù)據(jù)用法,實(shí)例分析了將集合數(shù)據(jù)綁定到ListBox和ComboBox控件的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2014-12-12
C#語言MVC框架Aspose.Cells控件導(dǎo)出Excel表數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了C#語言MVC框架Aspose.Cells控件導(dǎo)出Excel表數(shù)據(jù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
C#數(shù)據(jù)結(jié)構(gòu)之堆棧(Stack)實(shí)例詳解
這篇文章主要介紹了C#數(shù)據(jù)結(jié)構(gòu)之堆棧(Stack),結(jié)合實(shí)例形式較為詳細(xì)的分析了堆棧的原理與C#實(shí)現(xiàn)堆棧功能的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11

