ทำงานกับคุณสมบัติเอกสาร
คุณสมบัติเอกสารช่วยให้สามารถจัดเก็บข้อมูลที่เป็นประโยชน์เกี่ยวกับเอกสารของคุณได้ คุณสมบัติเหล่านี้สามารถแบ่งออกเป็นสองกลุ่ม:
- ระบบหรือบิวด์อินที่มีค่าต่างๆ เช่น ชื่อเอกสาร ชื่อผู้เขียน สถิติเอกสาร และอื่นๆ
- ผู้ใช้กำหนดหรือกำหนดเอง โดยระบุเป็นคู่ของชื่อ-ค่า ซึ่งผู้ใช้สามารถกำหนดทั้งชื่อและค่าได้
มีประโยชน์ที่จะทราบว่าข้อมูลเกี่ยวกับ API และหมายเลขเวอร์ชันถูกเขียนโดยตรงไปยังเอกสารเอาท์พุต ตัวอย่างเช่น เมื่อแปลงเอกสารเป็น PDF แล้ว Aspose.Words จะเติมในช่อง “Application” ด้วย “Aspose.Words” และช่อง “PDF Producer” ด้วย “Aspose.Words สำหรับ .NET YY.MN” โดยที่ YY.M.N คือเวอร์ชันของ Aspose.Words ที่ใช้สำหรับการแปลง. สำหรับรายละเอียดเพิ่มเติม โปรดดูที่ ชื่อตัวสร้างหรือผู้ผลิตรวมอยู่ในเอกสารเอาต์พุต
เข้าถึงคุณสมบัติเอกสาร
หากต้องการเข้าถึงคุณสมบัติของเอกสารใน Aspose.Words ให้ใช้:
-
built_in_document_properties เพื่อรับคุณสมบัติในตัว
-
custom_document_properties เพื่อรับคุณสมบัติที่กำหนดเอง
built_in_document_properties และ custom_document_properties เป็นคอลเลกชันของวัตถุ DocumentProperty วัตถุเหล่านี้สามารถรับได้ผ่านคุณสมบัติตัวสร้างดัชนีตามชื่อหรือตามดัชนี
built_in_document_properties ยังให้สิทธิ์เข้าถึงคุณสมบัติของเอกสารผ่านชุดคุณสมบัติที่ป้อนซึ่งส่งคืนค่าประเภทที่เหมาะสม custom_document_properties ช่วยให้คุณสามารถเพิ่มหรือลบคุณสมบัติของเอกสารออกจากเอกสารได้
คลาส DocumentProperty ช่วยให้คุณได้รับชื่อ ค่า และประเภทของคุณสมบัติเอกสาร value ส่งคืนออบเจ็กต์ แต่มีชุดของวิธีการที่ช่วยให้คุณได้รับค่าคุณสมบัติที่แปลงเป็นประเภทเฉพาะ หลังจากที่คุณทราบว่าคุณสมบัติเป็นประเภทใด คุณสามารถใช้วิธี DocumentProperty.to_XXX อย่างใดอย่างหนึ่ง เช่น DocumentProperty.__str__ และ DocumentProperty.to_int เพื่อรับค่าของประเภทที่เหมาะสมได้
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการระบุคุณสมบัติที่มีอยู่แล้วภายในและแบบกำหนดเองทั้งหมดในเอกสาร:
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
doc = aw.Document(docs_base.my_dir + "Properties.docx") | |
print("1. Document name: 0", doc.original_file_name) | |
print("2. Built-in Properties") | |
for prop in doc.built_in_document_properties : | |
print("0 : 1", prop.name, prop.value) | |
print("3. Custom Properties") | |
for prop in doc.custom_document_properties : | |
print("0 : 1", prop.name, prop.value) |
ใน Microsoft Word คุณสามารถเข้าถึงคุณสมบัติของเอกสารได้โดยใช้เมนู “ไฟล์ → คุณสมบัติ”

เพิ่มหรือลบคุณสมบัติเอกสาร
คุณไม่สามารถเพิ่มหรือลบคุณสมบัติเอกสารในตัวโดยใช้ Aspose.Words คุณสามารถเปลี่ยนหรืออัปเดตค่าได้เท่านั้น
หากต้องการเพิ่มคุณสมบัติเอกสารที่กำหนดเองด้วย Aspose.Words ให้ใช้วิธีการ add โดยส่งชื่อคุณสมบัติใหม่และค่าของประเภทที่เหมาะสม วิธีการส่งกลับวัตถุ DocumentProperty ที่สร้างขึ้นใหม่
หากต้องการลบคุณสมบัติที่กำหนดเอง ให้ใช้วิธี remove โดยส่งชื่อคุณสมบัติที่จะลบ หรือวิธี remove_at เพื่อลบคุณสมบัติตามดัชนี คุณยังสามารถลบคุณสมบัติทั้งหมดโดยใช้วิธี clear
ตัวอย่างโค้ดต่อไปนี้จะตรวจสอบว่ามีคุณสมบัติแบบกำหนดเองที่มีชื่อที่กำหนดอยู่ในเอกสารหรือไม่ และเพิ่มคุณสมบัติเอกสารแบบกำหนดเองอีกสองสามรายการ:
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
doc = aw.Document(docs_base.my_dir + "Properties.docx") | |
customDocumentProperties = doc.custom_document_properties | |
if (customDocumentProperties.get_by_name("Authorized") != None) : | |
return | |
customDocumentProperties.add("Authorized", True) | |
customDocumentProperties.add("Authorized By", "John Smith") | |
customDocumentProperties.add("Authorized Date", datetime.today()) | |
customDocumentProperties.add("Authorized Revision", doc.built_in_document_properties.revision_number) | |
customDocumentProperties.add("Authorized Amount", 123.45) |
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการลบคุณสมบัติเอกสารแบบกำหนดเอง:
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
doc = aw.Document(docs_base.my_dir + "Properties.docx") | |
doc.custom_document_properties.remove("Authorized Date") |
อัปเดตคุณสมบัติเอกสารในตัว
Aspose.Words จะไม่อัปเดตคุณสมบัติของเอกสารโดยอัตโนมัติ เช่นเดียวกับที่ Microsoft Word ทำกับคุณสมบัติบางอย่าง แต่มีวิธีการอัปเดตคุณสมบัติเอกสารในตัวทางสถิติบางอย่าง เรียกวิธีการ update_word_count เพื่อคำนวณใหม่ และปรับปรุงคุณสมบัติต่อไปนี้:
สร้างคุณสมบัติแบบกำหนดเองใหม่ที่เชื่อมโยงกับเนื้อหา
Aspose.Words มีเมธอด add_link_to_content เพื่อสร้างคุณสมบัติเอกสารที่กำหนดเองใหม่ที่เชื่อมโยงกับเนื้อหา คุณสมบัตินี้จะส่งคืนอ็อบเจ็กต์คุณสมบัติที่สร้างขึ้นใหม่หรือเป็นโมฆะหาก link_source ไม่ถูกต้อง
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีกำหนดค่าลิงก์ไปยังคุณสมบัติที่กำหนดเอง:
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
doc = aw.Document() | |
builder = aw.DocumentBuilder(doc) | |
builder.start_bookmark("MyBookmark") | |
builder.writeln("Text inside a bookmark.") | |
builder.end_bookmark("MyBookmark") | |
# Retrieve a list of all custom document properties from the file. | |
customProperties = doc.custom_document_properties | |
# Add linked to content property. | |
customProperty = customProperties.add_link_to_content("Bookmark", "MyBookmark") | |
customProperty = customProperties.get_by_name("Bookmark") | |
isLinkedToContent = customProperty.is_link_to_content | |
linkSource = customProperty.link_source | |
customPropertyValue = customProperty.value |
รับตัวแปรเอกสาร
คุณสามารถรับคอลเลกชันของตัวแปรเอกสารได้โดยใช้คุณสมบัติ variables ชื่อและค่าของตัวแปรเป็นสตริง
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการเพิ่มและเข้าถึงตัวแปรเอกสาร:
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
doc = aw.Document(docs_base.my_dir + "Document.docx") | |
doc.variables.add("test", "test") | |
variables = "" | |
for entry in doc.variables : | |
name = entry.key | |
value = entry.value | |
if (variables == "") : | |
variables = "Name: " + name + "," + "Value: " + value | |
else : | |
variables = variables + "Name: " + name + "," + "Value: " + value | |
ลบข้อมูลส่วนบุคคลออกจากเอกสาร
หากคุณต้องการแชร์เอกสาร Word กับบุคคลอื่น คุณอาจต้องการลบข้อมูลส่วนบุคคล เช่น ชื่อผู้เขียนและบริษัท ในการดำเนินการนี้ ให้ใช้คุณสมบัติ remove_personal_information เพื่อตั้งค่าสถานะที่ระบุว่า Microsoft Word จะลบข้อมูลผู้ใช้ทั้งหมดออกจากความคิดเห็น การแก้ไข และคุณสมบัติเอกสารเมื่อบันทึกเอกสาร
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการลบข้อมูลส่วนบุคคล:
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
doc = aw.Document(docs_base.my_dir + "Properties.docx") | |
doc.remove_personal_information = True | |
doc.save(docs_base.artifacts_dir + "DocumentPropertiesAndVariables.remove_personal_information.docx") |