Bekerja dengan Properti Dokumen

Properti dokumen memungkinkan penyimpanan beberapa informasi berguna tentang dokumen Anda. Properti ini dapat dibagi menjadi dua kelompok:

  • Sistem atau built-in yang berisi nilai-nilai seperti judul dokumen, nama penulis, statistik dokumen, dan lain-lain.
  • Ditentukan pengguna atau khusus, disediakan sebagai pasangan nama-nilai di mana pengguna dapat menentukan nama dan nilai.

Berguna untuk mengetahui bahwa informasi tentang API dan Nomor Versi langsung ditulis ke dokumen keluaran. Misalnya, saat mengonversi dokumen menjadi PDF, Aspose.Words mengisi bidang “Aplikasi” dengan “Aspose.Words”, dan bidang “ProduserPDF” dengan “Aspose.Words untuk C++ YY.M. N”, di mana YY.M.N adalah versi Aspose.Words yang digunakan untuk konversi. Untuk lebih jelasnya, lihat Generator atau Nama Produsen yang Disertakan dalam Dokumen Keluaran.

Mengakses Properti Dokumen

Untuk mengakses properti dokumen dalam Aspose.Words gunakan:

BuiltInDocumentProperties

BuiltInDocumentProperties

Kelas DocumentProperty memungkinkan Anda mendapatkan nama, nilai, dan tipe properti dokumen. Value mengembalikan objek, tetapi ada serangkaian metode yang memungkinkan Anda mengonversi nilai properti ke tipe tertentu. Setelah Anda mengetahui jenis propertinya, Anda dapat menggunakan salah satu metode DocumentProperty.ToXXX, seperti DocumentProperty. ToString dan DocumentProperty. ToInt, untuk mendapatkan nilai dari tipe yang sesuai.

Contoh kode berikut menunjukkan cara menghitung semua properti bawaan dan properti khusus dalam dokumen:

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;
}

Di Microsoft Word, Anda dapat mengakses properti dokumen menggunakan menu “File → Properties”.

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

Menambah atau Menghapus Properti Dokumen

Anda tidak dapat menambah atau menghapus properti dokumen bawaan menggunakan Aspose.Words. Anda hanya dapat mengubah atau memperbarui nilainya.

Untuk menambahkan properti dokumen khusus dengan Aspose.Words, gunakan metode Add, berikan nama properti baru dan nilai dari tipe yang sesuai. Metode mengembalikan objek DocumentProperty yang baru dibuat.

Untuk menghapus properti khusus, gunakan metode Remove, berikan nama properti untuk dihapus, atau metode RemoveAt untuk menghapus properti berdasarkan indeks. Anda juga dapat menghapus semua properti menggunakan metode Clear.

Contoh kode berikut memeriksa apakah properti kustom dengan nama tertentu ada di dokumen dan menambahkan beberapa properti dokumen kustom lainnya:

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);
}

Contoh kode berikut menunjukkan cara menghapus properti dokumen khusus:

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");

Perbarui properti Dokumen bawaan

Aspose.Words tidak memperbarui properti dokumen secara otomatis, seperti yang dilakukan Microsoft Word dengan beberapa properti, tetapi menyediakan metode untuk memperbarui beberapa properti dokumen bawaan statistik. Panggil metode UpdateWordCount untuk menghitung ulang dan memperbarui properti berikut:

Buat Properti Kustom Baru yang Ditautkan ke Konten

Aspose.Words menyediakan metode AddLinkToContent untuk membuat properti dokumen kustom baru yang ditautkan ke konten. Properti ini mengembalikan objek properti yang baru dibuat atau null jika LinkSource tidak valid.

Contoh kode berikut menunjukkan cara mengonfigurasi tautan ke properti khusus:

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();

Dapatkan Variabel Dokumen

Anda bisa mendapatkan kumpulan variabel dokumen menggunakan properti Variables. Nama dan nilai variabel adalah string.

Contoh kode berikut menunjukkan cara menghitung variabel dokumen:

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;
}
}

Menghapus Informasi Pribadi dari Dokumen

Jika Anda ingin berbagi dokumen Word dengan orang lain, Anda mungkin ingin menghapus informasi pribadi seperti nama penulis dan perusahaan. Untuk melakukannya, gunakan properti RemovePersonalInformation untuk menyetel tanda yang menunjukkan bahwa Microsoft Word akan menghapus semua informasi pengguna dari komentar, revisi, dan properti dokumen setelah menyimpan dokumen.

Contoh kode berikut menunjukkan cara menghapus informasi pribadi:

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);