ทำงานกับคุณสมบัติของเอกสาร
คุณสมบัติของเอกสาร คุณสมบัติเหล่านี้สามารถแบ่งออกเป็นสองกลุ่ม:
- ระบบหรือในตัวที่ประกอบด้วยค่าเช่นชื่อเอกสารชื่อผู้เขียนเอกสารสถิติและอื่นๆ.
- ผู้ใช้กำหนดหรือกำหนดเองให้เป็นคู่ชื่อ-ค่าที่ผู้ใช้สามารถกำหนดทั้งชื่อและค่า.
มันเป็นประโยชน์ที่จะรู้ว่าข้อมูลเกี่ยวกับAPIและหมายเลขรุ่นจะถูกเขียนโดยตรงไปยังเอกสา ตัวอย่างเช่นเมื่อแปลงเอกสารเป็นPDFAspose.Wordsกรอกข้อมูลในช่อง"แอพลิเคชัน"ด้วย"Aspose.Words"และช่อง"PDFโปรดิวเซอร์"ด้วย"Aspose.WordsสำหรับJavaYYม.เอ็น"ที่YY.M.Nเป็นรุ่นของAspose.Wordsใช้สำหรับการแปลง สำหรับรายละเอียดเพิ่มเติมโปรดดูที่ เครื่องกำเนิดไฟฟ้าหรือชื่อผู้ผลิตที่รวมอยู่ในเอกสารที่ส่งออก.
คุณสมบัติการเข้าถึงเอกสาร
ในการเข้าถึงคุณสมบัติของเอกสารในAspose.Wordsใช้:
-
BuiltInDocumentPropertiesเพื่อรับคุณสมบัติในตัว.
-
CustomDocumentPropertiesเพื่อรับคุณสมบัติที่กำหนดเอง.
BuiltInDocumentProperties
BuiltInDocumentProperties
คลาสDocumentPropertyช่วยให้คุณสามารถรับชื่อค่าและชนิดของคุณสมบัติเอกสาร [ค่า]https://reference.aspose.com/words/java/com.aspose.words/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-Java | |
String fileName = dataDir + "Properties.doc"; | |
Document doc = new Document(fileName); | |
System.out.println("1. Document name: " + fileName); | |
System.out.println("2. Built-in Properties"); | |
for (DocumentProperty prop : doc.getBuiltInDocumentProperties()) | |
System.out.println(prop.getName() + " : " + prop.getValue()); | |
System.out.println("3. Custom Properties"); | |
for (DocumentProperty prop : doc.getCustomDocumentProperties()) | |
System.out.println(prop.getName() + " : " + prop.getValue()); |
ใน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-Java | |
Document doc = new Document(dataDir + "Properties.doc"); | |
CustomDocumentProperties props = doc.getCustomDocumentProperties(); | |
if (props.get("Authorized") == null) { | |
props.add("Authorized", true); | |
props.add("Authorized By", "John Smith"); | |
props.add("Authorized Date", new Date()); | |
props.add("Authorized Revision", doc.getBuiltInDocumentProperties().getRevisionNumber()); | |
props.add("Authorized Amount", 123.45); | |
} |
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการเอาคุณสมบัติเอกสารที่กำหนดเอง:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(dataDir + "Properties.doc"); | |
doc.getCustomDocumentProperties().remove("Authorized Date"); |
ปรับปรุงคุณสมบัติของเอกสารในตัว
Aspose.Wordsจะไม่อัปเดตคุณสมบัติของเอกสารโดยอัตโนมัติเนื่องจากMicrosoft Wordใช้กับคุณสมบัติบางอย่างแต่มีวิธี เรียกวิธีการUpdateWordCountเพื่อคำนวณใหม่และอัพเดตคุณสมบัติต่อไปนี้:
สร้างคุณสมบัติที่กำหนดเองใหม่ที่เชื่อมโยงกับเนื้อหา
Aspose.WordsจัดเตรียมวิธีการAddLinkToContentเพื่อสร้างคุณสมบัติเอกสารที่กำหนดเองใหม่ที่เชื่อมโยงกับเนื้อหา คุณสมบัตินี้ส่งคืนอ็อบเจ็กต์คุณสมบัติที่สร้างขึ้นใหม่หรือโมฆะถ้าLinkSourceไม่ถูกต้อง.
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการกำหนดค่าการเชื่อมโยงไปยังคุณสมบัติที่กำหนดเอง:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(dataDir + "test.docx"); | |
// Retrieve a list of all custom document properties from the file. | |
CustomDocumentProperties customProperties = doc.getCustomDocumentProperties(); | |
// 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.get("PropertyName"); | |
// Check whether the property is linked to content. | |
boolean isLinkedToContent = customProperty.isLinkToContent(); | |
// Get the source of the property. | |
String source = customProperty.getLinkSource(); | |
// Get the value of the property. | |
String value = customProperty.getValue().toString(); |
รับตัวแปรเอกสาร
คุณสามารถรับคอลเลกชันของตัวแปรเอกสารโดยใช้คุณสมบัติVariables ชื่อตัวแปรและค่าเป็นสตริง.
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการระบุตัวแปรเอกสาร:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(dataDir + "Document.doc"); | |
for (java.util.Map.Entry entry : doc.getVariables()) { | |
String name = entry.getKey().toString(); | |
String value = entry.getValue().toString(); | |
// Do something useful. | |
System.out.println("Name: " + name + ", Value: " + value); | |
} |
ลบข้อมูลส่วนบุคคลออกจากเอกสาร
ถ้าคุณต้องการแบ่งปันเอกสารคำกับผู้อื่นคุณอาจต้องการลบข้อมูลส่วนบุคคลเช่นชื่อผู้เขียน เมื่อต้องการทำเช่นนี้ให้ใช้คุณสมบัติRemovePersonalInformationเพื่อตั้งค่าแฟล็กที่ระบุว่าMicrosoft Wordจะลบข้อมูลผู้ใช้ทั้งหมดออกจา.