کار با بوک مارک ها

نشانه ها در یک سند Microsoft Word مکان ها یا قطعات را که نام می برید و برای مرجع آینده شناسایی می کنید، شناسایی می کنند. برای مثال، ممکن است از یک نشانه برای شناسایی متن استفاده کنید که می خواهید بعدا آن را بازبینی کنید. به جای پیمایش از طریق سند برای پیدا کردن متن، می توانید با استفاده از کادر محاوره ای Bookmark به آن بروید.

اعمالی که می توان با نشانک ها با استفاده از Aspose.Words انجام داد همان اعمالی است که می توانید با استفاده از Microsoft Word انجام دهید. شما می توانید یک نشانه جدید را وارد کنید، حذف کنید، به یک نشانه بروید، یک نام نشانه را دریافت یا تنظیم کنید، متن را در آن قرار دهید یا تنظیم کنید. با Aspose.Words، شما همچنین می توانید از نشانه ها در گزارش ها یا اسناد برای قرار دادن برخی از داده ها در نشانه ها یا اعمال قالب بندی ویژه به محتوای آن استفاده کنید. همچنین می توانید از نشانه ها برای بازیابی متن از یک مکان خاص در سند خود استفاده کنید.

یک نشانک وارد کنید

از StartBookmark و EndBookmark برای ایجاد یک علامت گذاری با علامت گذاری شروع و پایان آن استفاده کنید. فراموش نکنید که همان نام نشانه را به هر دو روش منتقل کنید. نشانه های کتاب در یک سند می توانند همپوشانی داشته باشند و هر محدوده ای را پوشش دهند. نشانه ها یا نشانه های بد شکل با نام های تکراری هنگام ذخیره سند نادیده گرفته می شوند.

مثال کد زیر نشان می دهد که چگونه یک نشانه جدید ایجاد کنید:

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

به دست آوردن نشانک

گاهی اوقات لازم است که مجموعه ای از نشانه ها را برای تکرار از طریق نشانه ها یا برای اهداف دیگر بدست آورید. از ویژگی Node.Range که توسط هر گره سند که یک شی Range را نشان می دهد که بخشی از سند موجود در این گره را نشان می دهد، استفاده کنید. از این شی برای بازیابی BookmarkCollection استفاده کنید و سپس از شاخص کننده مجموعه برای دریافت یک نشانه خاص استفاده کنید.

مثال کد زیر نشان می دهد که چگونه از مجموعه نشانه ها نشانه ها را بدست آوریم:

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

مثال کد زیر نشان می دهد که چگونه یک نام و متن نشانه گذاری را بدست آورید یا تنظیم کنید:

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

مثال کد زیر نشان می دهد که چگونه یک جدول را نشانه گذاری کنید:

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

اگر نام یک نشانک را به نامی که قبلا در سند وجود دارد تغییر دهید، هیچ خطایی ایجاد نمی شود و تنها اولین نشانک هنگام ذخیره سند ذخیره می شود.

حرکت به نشانه گذاری

اگر شما نیاز به وارد کردن محتوای غنی (نه فقط متن ساده) به یک نشانه دارید، باید از MoveToBookmark برای حرکت دادن نشانگر به نشانه استفاده کنید و سپس از روش ها و خواص DocumentBuilder’s برای وارد کردن محتوا استفاده کنید.

نمایش مخفی کردن محتوای نشانک

کل نشانه (including the bookmarked content) را می توان در قسمت واقعی فیلد IF با استفاده از Aspose.Words خلاصه کرد. این می تواند به گونه ای باشد که فیلد IF شامل یک فیلد ادغام در عبارت (Left of Operator) باشد و بسته به ارزش فیلد ادغام، فیلد IF محتوای نشانه گذاری را در سند ورد نشان می دهد یا پنهان می کند.

مثال کد زیر نشان می دهد که چگونه نشانه ها را نشان می دهد/ پنهان می کند:

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