การทำงานกับบุ๊กมาร์ก

บุ๊กมาร์กระบุใน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.");

ขอรับที่คั่นหน้า

บางครั้งก็เป็นสิ่งจำเป็นเพื่อขอรับคอลเลกชันที่คั่นหน้าซ้ำผ่านที่คั่นหน้าหรือเพื่อวัตถุประสง ใช้คุณสมบัติNode.getRangeที่แสดงโดยโหนดเอกสารใดๆที่ส่งกลับวัตถุRangeที่แสดงส่วนของเอกสารที่อยู่ในโหนด ใช้วัตถุนี้เพื่อดึงข้อมูล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)สามารถถูกห่อหุ้มภายในส่วนที่แท้จริงของฟิลด์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-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");
}