북마크 작업

북마크는 Microsoft Word 나중에 참조할 수 있도록 이름을 지정하고 식별하는 위치 또는 조각을 문서화합니다. 예를 들어 책갈피를 사용하여 나중에 수정할 텍스트를 식별할 수 있습니다. 문서를 스크롤하여 텍스트를 찾는 대신 책갈피 대화 상자를 사용하여 해당 텍스트로 이동할 수 있습니다.

다음을 사용하여 북마크로 수행 할 수있는 작업 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");