在文件屬性中工作
文件屬性可以儲存您的文書中一些有用的資訊。 這些特性可以分成兩組。
*包含值如文檔名稱、作者名稱、文檔統計資料等之系統或內建。 “*用戶定義或自訂,以名稱值對的形式提供,用戶可以定義名稱和值。”
了解 API 和版本號直接寫入輸出文件的資訊很有用。 例如,當將文件轉換為 PDF 時,Aspose.Words 會將應用程式欄位填寫為Aspose.Words,並將PDF 製造商欄位填寫為Aspose.Words for .NET YY.M.N,其中 YY.M.N 是用於轉化的 Aspose.Words 版本。 詳細資料請參閱 Generator or Producer Name Included in Output Documents。
檢視檔案屬性
若要存取 Aspose.Words 中文件的屬性,請使用:
-
BuiltInDocumentProperties 來取得內建屬性。
-
CustomDocumentProperties 以取得自訂屬性。
BuiltInDocumentProperties 和 CustomDocumentProperties 是 DocumentProperty 物件的集合。 這些物件可透過索引屬性名稱或索引取得。
BuiltInDocumentProperties 透過輸入的屬性集來提供對文書屬性的存取。CustomDocumentProperties 讓您可以在文書中添加或移除文書屬性。
DocumentProperty類別讓您取得文件屬性的名稱、值和類型。 Value會傳回一個物件,但有一組方法可讓您取得屬性值並將其轉換為特定類型。 當你了解該屬性的類型後,您可以使用 DocumentProperty.ToXXX 方法中的某一個,例如 DocumentProperty.ToString 和 DocumentProperty.ToInt 來取得相應的值。
接下來的程式碼範例顯示如何列舉資料夾中的所有預設屬性和自訂屬性。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
string fileName = dataDir + "Properties.doc"; | |
Document doc = new Document(fileName); | |
Console.WriteLine("1. Document name: {0}", fileName); | |
Console.WriteLine("2. Built-in Properties"); | |
foreach (DocumentProperty prop in doc.BuiltInDocumentProperties) | |
Console.WriteLine("{0} : {1}", prop.Name, prop.Value); | |
Console.WriteLine("3. Custom Properties"); | |
foreach (DocumentProperty prop in doc.CustomDocumentProperties) | |
Console.WriteLine("{0} : {1}", prop.Name, prop.Value); |
在 Microsoft Word 中,您可以使用檔案→屬性選單來存取檔案屬性。

新增或刪除文件屬性
您無法透過 Aspose.Words 來新增或刪除內建文件屬性。 你只能改變或更新他們的值。
若要以 Aspose.Words 加入自訂的文件屬性,請使用 Add 方法,傳入新屬性的名稱和相應類型之值。 此方法會返回剛剛建立的 DocumentProperty 物件。
若要移除自訂屬性,請使用 Remove 方法並傳遞您想要移除的屬性名稱,或使用 RemoveAt 方法透過索引來移除該屬性。 您也可以透過 Clear 方法移除所有屬性。
以下範例程式碼會檢查文件中是否存在具有指定名稱的自訂屬性,並新增幾個額外的自訂文件屬性:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(dataDir + "Properties.doc"); | |
CustomDocumentProperties props = doc.CustomDocumentProperties; | |
if (props["Authorized"] == null) | |
{ | |
props.Add("Authorized", true); | |
props.Add("Authorized By", "John Smith"); | |
props.Add("Authorized Date", DateTime.Today); | |
props.Add("Authorized Revision", doc.BuiltInDocumentProperties.RevisionNumber); | |
props.Add("Authorized Amount", 123.45); | |
} |
接下來的程式碼範例說明了如何移除自訂的文件屬性:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(dataDir + "Properties.doc"); | |
doc.CustomDocumentProperties.Remove("Authorized Date"); |
更新內建文件屬性
Aspose.Words 不會自動更新文件屬性,就像 Microsoft Word 用一些屬性的方式一樣,但提供一種方法來更新某些內置文件的統計屬性。 呼叫 UpdateWordCount 方法重新計算並更新以下屬性:
建立新的與內容連結的自訂屬性
Aspose.Words 提供 AddLinkToContent 方法來建立新的自訂物件属性,該屬性與內容連結。 此屬性會傳回剛建立的屬性物件或無效的 LinkSource 時返回 null。
接下來的程式碼範例說明如何設定連結到自訂屬性:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(dataDir + "test.docx"); | |
// Retrieve a list of all custom document properties from the file. | |
CustomDocumentProperties customProperties = doc.CustomDocumentProperties; | |
// Add linked to content property. | |
DocumentProperty customProperty = customProperties.AddLinkToContent("PropertyName", "BookmarkName"); | |
// Also, accessing the custom document property can be performed by using the property name. | |
customProperty = customProperties["PropertyName"]; | |
// Check whether the property is linked to content. | |
bool isLinkedToContent = customProperty.IsLinkToContent; | |
// Get the source of the property. | |
string source = customProperty.LinkSource; | |
// Get the value of the property. | |
string value = customProperty.Value.ToString(); |
取得文書變數
您可以透過 Variables 屬性取得文件變數的集合。 變數名稱和值是字串。
以下範例顯示如何枚舉文檔變數。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_WorkingWithDocument(); | |
// Load the template document. | |
Document doc = new Document(dataDir + "TestFile.doc"); | |
string variables = ""; | |
foreach (KeyValuePair<string, string> entry in doc.Variables) | |
{ | |
string name = entry.Key.ToString(); | |
string value = entry.Value.ToString(); | |
if (variables == "") | |
{ | |
// Do something useful. | |
variables = "Name: " + name + "," + "Value: {1}" + value; | |
} | |
else | |
{ | |
variables = variables + "Name: " + name + "," + "Value: {1}" + value; | |
} | |
} |
從文件中移除個人資料
如果你想與其他人分享 Word 文檔,你可能想把個人資料(如作者名稱和公司)移除。 若要如此操作,請使用 RemovePersonalInformation 屬性設定旗標,指示在儲存文件時 Microsoft Word 會從評論、修訂和文件屬性中移除所有使用者資訊。
以下範例顯示如何移除個人資料:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(dataDir + "Properties.doc"); | |
doc.RemovePersonalInformation = true; | |
dataDir = dataDir + "RemovePersonalInformation_out.docx"; | |
doc.Save(dataDir); |