Bekerja dengan Bookmark

Penanda mengidentifikasi dalam dokumen Microsoft Word lokasi atau fragmen yang Anda beri nama dan identifikasi untuk referensi di masa mendatang. Misalnya, Anda dapat menggunakan penanda untuk mengidentifikasi teks yang ingin Anda revisi nanti. Alih-alih menggulir dokumen untuk menemukan teks, Anda dapat membukanya dengan menggunakan kotak dialog Bookmark.

Tindakan yang dapat dilakukan dengan penanda menggunakan Aspose.Words sama dengan tindakan yang dapat Anda lakukan menggunakan Microsoft Word. Anda dapat menyisipkan bookmark baru, menghapus, memindahkan ke bookmark, mendapatkan atau menyetel nama bookmark, mendapatkan atau menyetel teks yang terlampir di dalamnya. Dengan Aspose.Words, Anda juga dapat menggunakan penanda di laporan atau dokumen untuk menyisipkan beberapa data ke dalam penanda atau menerapkan pemformatan khusus pada kontennya. Anda juga dapat menggunakan penanda untuk mengambil teks dari lokasi tertentu di dokumen Anda.

Sisipkan Penanda Buku

Gunakan StartBookmark dan EndBookmark untuk membuat bookmark dengan menandai awal dan akhir masing-masing. Jangan lupa untuk meneruskan nama bookmark yang sama ke kedua metode. Penanda dalam dokumen dapat tumpang tindih dan menjangkau rentang apa pun. Bookmark atau bookmark yang terbentuk dengan buruk dengan nama duplikat akan diabaikan saat dokumen disimpan.

Contoh kode berikut menunjukkan cara membuat bookmark baru:

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 outputDataDir = GetOutputDataDir_WorkingWithBookmarks();
System::SharedPtr<Document> doc = System::MakeObject<Document>();
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);
builder->StartBookmark(u"My Bookmark");
builder->Writeln(u"Text inside a bookmark.");
builder->StartBookmark(u"Nested Bookmark");
builder->Writeln(u"Text inside a NestedBookmark.");
builder->EndBookmark(u"Nested Bookmark");
builder->Writeln(u"Text after Nested Bookmark.");
builder->EndBookmark(u"My Bookmark");
System::SharedPtr<PdfSaveOptions> options = System::MakeObject<PdfSaveOptions>();
options->get_OutlineOptions()->get_BookmarksOutlineLevels()->Add(u"My Bookmark", 1);
options->get_OutlineOptions()->get_BookmarksOutlineLevels()->Add(u"Nested Bookmark", 2);
System::String outputPath = outputDataDir + u"CreateBookmark.pdf";
doc->Save(outputPath, options);

Dapatkan Bookmark

Kadang-kadang perlu untuk mendapatkan koleksi bookmark untuk beralih melalui bookmark atau untuk tujuan lain. Gunakan properti Node.Range yang diekspos oleh simpul dokumen apa pun yang mengembalikan objek Range yang mewakili bagian dokumen yang terdapat dalam simpul ini. Gunakan objek ini untuk mengambil BookmarkCollection lalu gunakan pengindeks koleksi untuk mendapatkan penanda tertentu.

Contoh kode berikut menunjukkan cara mendapatkan bookmark dari koleksi bookmark:

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_WorkingWithBookmarks();
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"Bookmarks.doc");
// By index.
System::SharedPtr<Bookmark> bookmark1 = doc->get_Range()->get_Bookmarks()->idx_get(0);
// By name.
System::SharedPtr<Bookmark> bookmark2 = doc->get_Range()->get_Bookmarks()->idx_get(u"Bookmark2");

Contoh kode berikut menunjukkan cara mendapatkan atau menyetel nama dan teks bookmark:

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_WorkingWithBookmarks();
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"Bookmark.doc");
// Use the indexer of the Bookmarks collection to obtain the desired bookmark.
System::SharedPtr<Bookmark> bookmark = doc->get_Range()->get_Bookmarks()->idx_get(u"MyBookmark");
// Get the name and text of the bookmark.
System::String name = bookmark->get_Name();
System::String text = bookmark->get_Text();
// Set the name and text of the bookmark.
bookmark->set_Name(u"RenamedBookmark");
bookmark->set_Text(u"This is a new bookmarked text.");

Contoh kode berikut menunjukkan cara menandai tabel:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// Create empty document
System::SharedPtr<Document> doc = System::MakeObject<Document>();
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);
System::SharedPtr<Table> table = builder->StartTable();
// Insert a cell
builder->InsertCell();
// Start bookmark here after calling InsertCell
builder->StartBookmark(u"MyBookmark");
builder->Write(u"This is row 1 cell 1");
// Insert a cell
builder->InsertCell();
builder->Write(u"This is row 1 cell 2");
builder->EndRow();
// Insert a cell
builder->InsertCell();
builder->Writeln(u"This is row 2 cell 1");
// Insert a cell
builder->InsertCell();
builder->Writeln(u"This is row 2 cell 2");
builder->EndRow();
builder->EndTable();
// End of bookmark
builder->EndBookmark(u"MyBookmark");
System::String outputPath = outputDataDir + u"BookmarkTable.doc";
doc->Save(outputPath);

Jika Anda mengubah nama penanda menjadi nama yang sudah ada di dokumen, tidak akan ada kesalahan dan hanya penanda pertama yang akan disimpan saat Anda menyimpan dokumen.

Pindah ke Bookmark

Jika Anda perlu menyisipkan konten kaya (bukan hanya teks biasa) ke dalam bookmark, Anda harus menggunakan MoveToBookmark untuk memindahkan kursor ke bookmark, lalu menggunakan metode dan properti DocumentBuilder’s untuk menyisipkan konten.

Tampilkan Sembunyikan Konten Bookmark

Seluruh Penanda (including the bookmarked content) dapat dienkapsulasi dalam bagian Sebenarnya dari bidang IF menggunakan Aspose.Words. Bisa sedemikian rupa sehingga bidang IF berisi Bidang Penggabungan bersarang di ekspresi (Left of Operator) dan bergantung pada nilai Bidang Penggabungan, bidang IF menampilkan atau menyembunyikan konten Bookmark di Dokumen Word.

Contoh kode berikut menunjukkan cara menampilkan / menyembunyikan bookmark:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
typedef System::SharedPtr<System::Object> TObjectPtr;
System::String bookmarkName = u"Bookmark2";
// The path to the documents directory.
System::String inputDataDir = GetInputDataDir_WorkingWithBookmarks();
System::String outputDataDir = GetOutputDataDir_WorkingWithBookmarks();
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"Bookmarks.doc");
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);
System::SharedPtr<Bookmark> bm = doc->get_Range()->get_Bookmarks()->idx_get(bookmarkName);
builder->MoveToDocumentEnd();
// {IF "{MERGEFIELD bookmark}" = "true" "" ""}
System::SharedPtr<Field> field = builder->InsertField(u"IF \"", nullptr);
builder->MoveTo(field->get_FieldStart()->get_NextSibling());
builder->InsertField(System::String(u"MERGEFIELD ") + bookmarkName + u"", nullptr);
builder->Write(u"\" = \"true\" ");
builder->Write(u"\"");
builder->Write(u"\"");
builder->Write(u" \"\"");
System::SharedPtr<Node> currentNode = field->get_FieldStart();
bool flag = true;
while (currentNode != nullptr && flag)
{
if (currentNode->get_NodeType() == Aspose::Words::NodeType::Run)
{
if (System::ObjectExt::Equals(currentNode->ToString(Aspose::Words::SaveFormat::Text).Trim(), u"\""))
{
flag = false;
}
}
System::SharedPtr<Node> nextNode = currentNode->get_NextSibling();
bm->get_BookmarkStart()->get_ParentNode()->InsertBefore(currentNode, bm->get_BookmarkStart());
currentNode = nextNode;
}
System::SharedPtr<Node> endNode = bm->get_BookmarkEnd();
flag = true;
while (currentNode != nullptr && flag)
{
if (currentNode->get_NodeType() == Aspose::Words::NodeType::FieldEnd)
{
flag = false;
}
System::SharedPtr<Node> nextNode = currentNode->get_NextSibling();
bm->get_BookmarkEnd()->get_ParentNode()->InsertAfter(currentNode, endNode);
endNode = currentNode;
currentNode = nextNode;
}
doc->get_MailMerge()->Execute(System::MakeArray<System::String>({bookmarkName}), System::MakeArray<TObjectPtr>({System::ObjectExt::Box<bool>(false)}));
//MailMerge can be avoided by using the following
//builder.MoveToMergeField(bookmarkName);
//builder.Write(showHide ? "true" : "false");
doc->Save(outputDataDir + u"Updated_Document_out.doc");