ブックマークの操作
ブックマークは、Microsoft Wordドキュメント内で、将来の参照のために名前を付けて識別する場所またはフラグメントを識別します。 たとえば、ブックマークを使用して、後で修正するテキストを識別することができます。 ドキュメントをスクロールしてテキストを見つける代わりに、ブックマークダイアログボックスを使用してドキュメントに移動できます。
Aspose.Wordsを使用すると、レポートまたはドキュメントのブックマークを使用して、ブックマークにデータを挿入したり、その内容に特別な書式を適用したりで ブックマークを使用して、文書内の特定の場所からテキストを取得することもできます。
Aspose.Wordsを使用してブックマークで実行できるアクションは、Microsoft Wordを使用して実行できるアクションと同じです。 新しいブックマークの挿入、削除、ブックマークへの移動、ブックマーク名の取得または設定、ブックマークに囲まれたテキストの取得または設定ができます。
ブックマークを挿入する
startBookmarkとendBookmarkを使用して、それぞれ開始と終了をマークしてブックマークを作成します。 両方のメソッドに同じブックマーク名を渡すことを忘れないでください。 文書内のブックマークは、任意の範囲に重複してまたがることができます。 不適切に形成されたブックマークまたは重複した名前のブックマークは、文書を保存するときに無視されます。
次のコード例は、新しいブックマークを作成する方法を示しています:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(CreateBookmark.class); | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.startBookmark("MyBookmark"); | |
builder.writeln("Text inside a bookmark."); | |
builder.endBookmark("MyBookmark"); | |
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.getOutlineOptions().setDefaultBookmarksOutlineLevel(1); | |
options.getOutlineOptions().setDefaultBookmarksOutlineLevel(2); | |
doc.save(dataDir + "output.pdf", options); | |
System.out.println("\nBookmark created successfully."); |
ブックマークを取得する
ブックマークを反復処理するために、または他の目的のためにブックマークコレクションを取得する必要がある場合があります。 このノードに含まれる文書の部分を表すRangeオブジェクトを返す任意の文書ノードによって公開されるNode.getRangeプロパティを使用します。 このオブジェクトを使用してBookmarkCollectionを取得し、コレクションインデクサを使用して特定のブックマークを取得します。
次のコード例は、ブックマークコレクションからブックマークを取得する方法を示しています:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(AccessBookmarks.class); | |
Document doc = new Document(dataDir + "Bookmark.doc"); | |
Bookmark bookmark1 = doc.getRange().getBookmarks().get(0); | |
Bookmark bookmark = doc.getRange().getBookmarks().get("MyBookmark"); | |
doc.save(dataDir + "output.doc"); | |
System.out.println("\nTable bookmarked successfully.\nFile saved at " + dataDir); | |
ブックマーク名とテキストを取得または設定する方法を次のコード例に示します:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(BookmarkNameAndText.class); | |
Document doc = new Document(dataDir + "Bookmark.doc"); | |
// Use the indexer of the Bookmarks collection to obtain the desired bookmark. | |
Bookmark bookmark = doc.getRange().getBookmarks().get("MyBookmark"); | |
// Get the name and text of the bookmark. | |
String name = bookmark.getName(); | |
String text = bookmark.getText(); | |
// Set the name and text of the bookmark. | |
bookmark.setName("RenamedBookmark"); | |
bookmark.setText("This is a new bookmarked text."); | |
System.out.println("\nBookmark name and text set successfully."); | |
次のコード例は、テーブルをブックマークする方法を示しています:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
//Create empty document | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
// We call this method to start building the table. | |
builder.startTable(); | |
builder.insertCell(); | |
// Start bookmark here after calling InsertCell | |
builder.startBookmark("MyBookmark"); | |
builder.write("Row 1, Cell 1 Content."); | |
// Build the second cell | |
builder.insertCell(); | |
builder.write("Row 1, Cell 2 Content."); | |
// Call the following method to end the row and start a new row. | |
builder.endRow(); | |
// Build the first cell of the second row. | |
builder.insertCell(); | |
builder.write("Row 2, Cell 1 Content"); | |
// Build the second cell. | |
builder.insertCell(); | |
builder.write("Row 2, Cell 2 Content."); | |
builder.endRow(); | |
// Signal that we have finished building the table. | |
builder.endTable(); | |
//End of bookmark | |
builder.endBookmark("MyBookmark"); | |
doc.save(dataDir + "output.doc"); | |
System.out.println("\nTable bookmarked successfully.\nFile saved at " + dataDir); |
ブックマークの名前を文書に既に存在する名前に変更した場合、エラーは生成されず、文書を保存するときに最初のブックマークのみが保存されます。
ドキュメント内のブックマークの中には、フォームフィールドに割り当てられているものがあります。 そのようなブックマークに移動してテキストを挿入すると、テキストがフォームフィールドコードに挿入されます。 これによりフォームフィールドが無効になることはありませんが、挿入されたテキストはフィールドコードの一部になるため表示されません。
次のコード例は、ブックマークされたテーブルの列にアクセスする方法を示しています:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// Create empty document | |
Document doc = new Document(dataDir + "Bookmark.Table_out.doc"); | |
for (Bookmark bookmark : doc.getRange().getBookmarks()) | |
{ | |
System.out.printf("Bookmark: {0}{1}", bookmark.getName(), bookmark.isColumn() ? " (Column)" : ""); | |
if (bookmark.isColumn()) | |
{ | |
Row row = (Row) bookmark.getBookmarkStart().getAncestor(NodeType.ROW); | |
if (row != null && bookmark.getFirstColumn() < row.getCells().getCount()) | |
System.out.print(row.getCells().get(bookmark.getFirstColumn()).getText()); | |
} | |
} |
ブックマークに移動する
(プレーンテキストだけでなく)リッチコンテンツをブックマークに挿入する必要がある場合は、moveToBookmarkを使用してカーソルをブックマークに移動し、DocumentBuilderメソッドと
ブックマークのコンテンツを非表示にする
ブックマーク全体(including the bookmarked content)は、Aspose.Wordsを使用してIF
フィールドの真の部分にカプセル化できます。 IF
フィールドに式(Left of Operator)にネストされた差し込み項目が含まれ、差し込み項目の値に応じて、IF
フィールドにWord文書のブックマークの内容が表示または非表示にな
次のコード例は、ブックマークを表示/非表示にする方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(ShowHideBookmarks.class); | |
Document doc = new Document(dataDir + "Bookmark.doc"); | |
showHideBookmarkedContent(doc,"MyBookmark",false); | |
doc.save(dataDir + "Updated_Document.docx"); | |
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
public static void showHideBookmarkedContent(Document doc, String bookmarkName, boolean showHide) throws Exception { | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Bookmark bm = doc.getRange().getBookmarks().get(bookmarkName); | |
builder.moveToDocumentEnd(); | |
// {IF "{MERGEFIELD bookmark}" = "true" "" ""} | |
Field field = builder.insertField("IF \"", null); | |
builder.moveTo(field.getStart().getNextSibling().getNextSibling()); | |
builder.insertField("MERGEFIELD " + bookmarkName + "", null); | |
builder.write("\" = \"true\" "); | |
builder.write("\""); | |
builder.write("\""); | |
builder.write(" \"\""); | |
Node currentNode = field.getStart(); | |
boolean flag = true; | |
while (currentNode != null && flag) { | |
if (currentNode.getNodeType() == NodeType.RUN) | |
if (currentNode.toString(SaveFormat.TEXT).trim().equals("\"")) | |
flag = false; | |
Node nextNode = currentNode.getNextSibling(); | |
bm.getBookmarkStart().getParentNode().insertBefore(currentNode, bm.getBookmarkStart()); | |
currentNode = nextNode; | |
} | |
Node endNode = bm.getBookmarkEnd(); | |
flag = true; | |
while (currentNode != null && flag) { | |
if (currentNode.getNodeType() == NodeType.FIELD_END) | |
flag = false; | |
Node nextNode = currentNode.getNextSibling(); | |
bm.getBookmarkEnd().getParentNode().insertAfter(currentNode, endNode); | |
endNode = currentNode; | |
currentNode = nextNode; | |
} | |
doc.getMailMerge().execute(new String[]{bookmarkName}, new Object[]{showHide}); | |
//In case, you do not want to use MailMerge then you may use the following lines of code. | |
//builder.moveToMergeField(bookmarkName); | |
//builder.write(showHide ? "true" : "false"); | |
} |