ブックマークの操作

ブックマークは、将来の参照のために名前を付けて特定した場所またはフラグメントを Microsoft Word ドキュメント内で識別します。たとえば、ブックマークを使用して、後で修正するテキストを特定できます。ドキュメントをスクロールしてテキストを見つける代わりに、[ブックマーク] ダイアログ ボックスを使用してテキストに移動できます。

Aspose.Words を使用してブックマークで実行できるアクションは、Microsoft Word を使用して実行できるアクションと同じです。新しいブックマークの挿入、削除、ブックマークへの移動、ブックマーク名の取得または設定、ブックマークに囲まれたテキストの取得または設定を行うことができます。

ブックマークを挿入する

StartBookmarkEndBookmark を使用して、それぞれ開始と終了をマークしてブックマークを作成します。同じブックマーク名を両方のメソッドに渡すことを忘れないでください。文書内のブックマークは、任意の範囲に重ねたり、またがったりすることができます。形式が正しくないブックマークや重複した名前のブックマークは、ドキュメントの保存時に無視されます。

次のコード例は、新しいブックマークを作成する方法を示しています。

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithBookmarks();
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.StartBookmark("My Bookmark");
builder.Writeln("Text inside a bookmark.");
builder.StartBookmark("Nested Bookmark");
builder.Writeln("Text inside a NestedBookmark.");
builder.EndBookmark("Nested Bookmark");
builder.Writeln("Text after Nested Bookmark.");
builder.EndBookmark("My Bookmark");
PdfSaveOptions options = new PdfSaveOptions();
options.OutlineOptions.BookmarksOutlineLevels.Add("My Bookmark", 1);
options.OutlineOptions.BookmarksOutlineLevels.Add("Nested Bookmark", 2);
dataDir = dataDir + "Create.Bookmark_out.pdf";
doc.Save(dataDir, options);

ブックマークを取得する

場合によっては、ブックマークを反復処理するため、またはその他の目的でブックマーク コレクションを取得する必要があります。このノードに含まれるドキュメントの部分を表す Range オブジェクトを返すドキュメント ノードによって公開される Node.Range プロパティを使用します。このオブジェクトを使用して BookmarkCollection を取得し、コレクション インデクサーを使用して特定のブックマークを取得します。

次のコード例は、ブックマーク コレクションからブックマークを取得する方法を示しています。

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithBookmarks();
Document doc = new Document(dataDir + "Bookmarks.doc");
// By index.
Bookmark bookmark1 = doc.Range.Bookmarks[0];
// By name.
Bookmark bookmark2 = doc.Range.Bookmarks["Bookmark2"];

次のコード例は、ブックマーク名とテキストを取得または設定する方法を示しています。

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithBookmarks();
Document doc = new Document(dataDir + "Bookmark.doc");
// Use the indexer of the Bookmarks collection to obtain the desired bookmark.
Bookmark bookmark = doc.Range.Bookmarks["MyBookmark"];
// Get the name and text of the bookmark.
string name = bookmark.Name;
string text = bookmark.Text;
// Set the name and text of the bookmark.
bookmark.Name = "RenamedBookmark";
bookmark.Text = "This is a new bookmarked text.";

次のコード例は、テーブルをブックマークする方法を示しています。

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// Create empty document
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Table table = builder.StartTable();
// Insert a cell
builder.InsertCell();
// Start bookmark here after calling InsertCell
builder.StartBookmark("MyBookmark");
builder.Write("This is row 1 cell 1");
// Insert a cell
builder.InsertCell();
builder.Write("This is row 1 cell 2");
builder.EndRow();
// Insert a cell
builder.InsertCell();
builder.Writeln("This is row 2 cell 1");
// Insert a cell
builder.InsertCell();
builder.Writeln("This is row 2 cell 2");
builder.EndRow();
builder.EndTable();
// End of bookmark
builder.EndBookmark("MyBookmark");
dataDir = dataDir + "Bookmark.Table_out.doc";
doc.Save(dataDir);

ブックマークの名前をドキュメント内に既に存在する名前に変更しても、エラーは生成されず、ドキュメントを保存するときに最初のブックマークのみが保存されます。

ドキュメント内の一部のブックマークはフォーム フィールドに割り当てられていることに注意してください。このようなブックマークに移動してそこにテキストを挿入すると、そのテキストがフォーム フィールド コードに挿入されます。これによってフォーム フィールドは無効になりませんが、挿入されたテキストはフィールド コードの一部になるため表示されなくなります。

次のコード例は、ブックマークされたテーブルの列にアクセスする方法を示しています。

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// Create empty document
Document doc = new Document(dataDir + "Bookmark.Table_out.doc");
foreach (Bookmark bookmark in doc.Range.Bookmarks)
{
Console.WriteLine("Bookmark: {0}{1}", bookmark.Name, bookmark.IsColumn ? " (Column)" : "");
if (bookmark.IsColumn)
{
Row row = bookmark.BookmarkStart.GetAncestor(NodeType.Row) as Row;
if (row != null && bookmark.FirstColumn < row.Cells.Count)
Console.WriteLine(row.Cells[bookmark.FirstColumn].GetText().TrimEnd(ControlChar.CellChar));
}
}

ブックマークに移動

リッチ コンテンツ (プレーン テキストだけでなく) をブックマークに挿入する必要がある場合は、MoveToBookmark を使用してカーソルをブックマークに移動し、DocumentBuilder’s メソッドとプロパティを使用してコンテンツを挿入する必要があります。

表示 ブックマークの内容を非表示にする

ブックマーク全体 (ブックマークされたコンテンツを含む) は、Aspose.Words を使用して IF フィールドの True 部分内にカプセル化できます。 IF フィールドの式 (演算子の左側) にネストされた差し込みフィールドが含まれており、差し込みフィールドの値に応じて、IF フィールドで Word 文書のブックマークの内容が表示または非表示になります。

次のコード例は、ブックマークを表示/非表示にする方法を示しています。

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithBookmarks();
Document doc = new Document(dataDir + "Bookmarks.doc");
ShowHideBookmarkedContent(doc, "Bookmark2", false);
doc.Save(dataDir + "Updated_Document.doc");
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
public static void ShowHideBookmarkedContent(Document doc, String bookmarkName, bool showHide)
{
DocumentBuilder builder = new DocumentBuilder(doc);
Bookmark bm = doc.Range.Bookmarks[bookmarkName];
builder.MoveToDocumentEnd();
// {IF "{MERGEFIELD bookmark}" = "true" "" ""}
Field field = builder.InsertField("IF \"", null);
builder.MoveTo(field.Start.NextSibling);
builder.InsertField("MERGEFIELD " + bookmarkName + "", null);
builder.Write("\" = \"true\" ");
builder.Write("\"");
builder.Write("\"");
builder.Write(" \"\"");
Node currentNode = field.Start;
bool flag = true;
while (currentNode != null && flag)
{
if (currentNode.NodeType == NodeType.Run)
if (currentNode.ToString(SaveFormat.Text).Trim().Equals("\""))
flag = false;
Node nextNode = currentNode.NextSibling;
bm.BookmarkStart.ParentNode.InsertBefore(currentNode, bm.BookmarkStart);
currentNode = nextNode;
}
Node endNode = bm.BookmarkEnd;
flag = true;
while (currentNode != null && flag)
{
if (currentNode.NodeType == NodeType.FieldEnd)
flag = false;
Node nextNode = currentNode.NextSibling;
bm.BookmarkEnd.ParentNode.InsertAfter(currentNode, endNode);
endNode = currentNode;
currentNode = nextNode;
}
doc.MailMerge.Execute(new String[] { bookmarkName }, new Object[] { showHide });
//MailMerge can be avoided by using the following
//builder.MoveToMergeField(bookmarkName);
//builder.Write(showHide ? "true" : "false");
}