ทำงานกับคุณสมบัติของเอกสาร

คุณสมบัติของเอกสาร คุณสมบัติเหล่านี้สามารถแบ่งออกเป็นสองกลุ่ม:

  • ระบบหรือในตัวที่ประกอบด้วยค่าเช่นชื่อเอกสารชื่อผู้เขียนเอกสารสถิติและอื่นๆ.
  • ผู้ใช้กำหนดหรือกำหนดเองให้เป็นคู่ชื่อ-ค่าที่ผู้ใช้สามารถกำหนดทั้งชื่อและค่า.

มันเป็นประโยชน์ที่จะรู้ว่าข้อมูลเกี่ยวกับAPIและหมายเลขรุ่นจะถูกเขียนโดยตรงไปยังเอกสา ตัวอย่างเช่นเมื่อแปลงเอกสารเป็นPDFAspose.Wordsกรอกข้อมูลในฟิลด์"แอพลิเคชัน"ด้วย"Aspose.Words"และฟิลด์"PDFโปรดิวเซอร์"ด้Aspose.WordsสำหรับC++ YY.ม.เอ็น"ที่YY.M.Nเป็นรุ่นของAspose.Wordsใช้สำหรับการแปลง สำหรับรายละเอียดเพิ่มเติมโปรดดูที่ เครื่องกำเนิดไฟฟ้าหรือชื่อผู้ผลิตที่รวมอยู่ในเอกสารที่ส่งออก.

คุณสมบัติการเข้าถึงเอกสาร

ในการเข้าถึงคุณสมบัติของเอกสารในAspose.Wordsใช้:

BuiltInDocumentProperties

BuiltInDocumentProperties

คลาส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-C
System::String fileName = inputDataDir + u"Properties.doc";
System::SharedPtr<Document> doc = System::MakeObject<Document>(fileName);
std::cout << "1. Document name: " << fileName.ToUtf8String() << std::endl;
std::cout << "2. Built-in Properties" << std::endl;
for (System::SharedPtr<DocumentProperty> prop : System::IterateOver(doc->get_BuiltInDocumentProperties()))
{
std::cout << prop->get_Name().ToUtf8String() << " : " << prop->get_Value()->ToString().ToUtf8String() << std::endl;
}
std::cout << "3. Custom Properties" << std::endl;
for (System::SharedPtr<DocumentProperty> prop : System::IterateOver(doc->get_CustomDocumentProperties()))
{
std::cout << prop->get_Name().ToUtf8String() << " : " << prop->get_Value()->ToString().ToUtf8String() << std::endl;
}

ในMicrosoft Wordคุณสามารถเข้าถึงคุณสมบัติของเอกสารโดยใช้เมนู"คุณสมบัติซีดีไฟล์".

work-with-document-properties-aspose-words-cpp_1.png

เพิ่มหรือลบคุณสมบัติของเอกสาร

คุณไม่สามารถเพิ่มหรือลบคุณสมบัติเอกสารในตัวโดยใช้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-C
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"Properties.doc");
System::SharedPtr<CustomDocumentProperties> props = doc->get_CustomDocumentProperties();
if (props->idx_get(u"Authorized") == nullptr)
{
props->Add(u"Authorized", true);
props->Add(u"Authorized By", System::String(u"John Smith"));
props->Add(u"Authorized Date", System::DateTime::get_Today());
props->Add(u"Authorized Revision", doc->get_BuiltInDocumentProperties()->get_RevisionNumber());
props->Add(u"Authorized Amount", 123.45);
}

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการเอาคุณสมบัติเอกสารที่กำหนดเอง:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"Properties.doc");
doc->get_CustomDocumentProperties()->Remove(u"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-C
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"test.docx");
// Retrieve a list of all custom document properties from the file.
System::SharedPtr<CustomDocumentProperties> customProperties = doc->get_CustomDocumentProperties();
// Add linked to content property.
System::SharedPtr<DocumentProperty> customProperty = customProperties->AddLinkToContent(u"PropertyName", u"BookmarkName");
// Also, accessing the custom document property can be performed by using the property name.
customProperty = customProperties->idx_get(u"PropertyName");
// Check whether the property is linked to content.
bool isLinkedToContent = customProperty->get_IsLinkToContent();
// Get the source of the property.
System::String source = customProperty->get_LinkSource();
// Get the value of the property.
System::String value = customProperty->get_Value()->ToString();

รับตัวแปรเอกสาร

คุณสามารถรับคอลเลกชันของตัวแปรเอกสารโดยใช้คุณสมบัติVariables ชื่อตัวแปรและค่าเป็นสตริง.

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการระบุตัวแปรเอกสาร:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// The path to the documents directory.
System::String inputDataDir = GetInputDataDir_WorkingWithDocument();
// Load the template document.
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"TestFile.doc");
System::String variables = u"";
for (System::Collections::Generic::KeyValuePair<System::String, System::String> entry : System::IterateOver(doc->get_Variables()))
{
System::String name = entry.get_Key();
System::String value = entry.get_Value();
if (variables == u"")
{
// Do something useful.
variables = System::String(u"Name: ") + name + u"," + u"Value: " + value;
}
else
{
variables = variables + u"Name: " + name + u"," + u"Value: " + value;
}
}

ลบข้อมูลส่วนบุคคลออกจากเอกสาร

ถ้าคุณต้องการแบ่งปันเอกสารคำกับผู้อื่นคุณอาจต้องการลบข้อมูลส่วนบุคคลเช่นชื่อผู้เขียน เมื่อต้องการทำเช่นนี้ให้ใช้คุณสมบัติRemovePersonalInformationเพื่อตั้งค่าแฟล็กที่ระบุว่าMicrosoft Wordจะลบข้อมูลผู้ใช้ทั้งหมดออกจา.

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการลบข้อมูลส่วนบุคคล:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"Properties.doc");
doc->set_RemovePersonalInformation(true);
System::String outputPath = outputDataDir + u"DocProperties.RemovePersonalInformation.docx";
doc->Save(outputPath);