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

บุ๊กมาร์กจะระบุตำแหน่งหรือส่วนต่างๆ ในเอกสาร Microsoft Word ที่คุณตั้งชื่อและระบุเพื่อใช้อ้างอิงในอนาคต ตัวอย่างเช่น คุณอาจใช้บุ๊กมาร์กเพื่อระบุข้อความที่คุณต้องการแก้ไขในภายหลัง แทนที่จะเลื่อนดูเอกสารเพื่อค้นหาข้อความ คุณสามารถไปที่เอกสารนั้นได้โดยใช้กล่องโต้ตอบบุ๊กมาร์ก

การดำเนินการที่สามารถทำได้โดยใช้บุ๊กมาร์กโดยใช้ Aspose.Words จะเหมือนกับการดำเนินการที่คุณสามารถทำได้โดยใช้ Microsoft Word คุณสามารถแทรกบุ๊กมาร์กใหม่ ลบ ย้ายไปยังบุ๊กมาร์ก รับหรือตั้งชื่อบุ๊กมาร์ก รับหรือตั้งค่าข้อความที่อยู่ในนั้น

แทรกบุ๊กมาร์ก

ใช้ StartBookmark และ EndBookmark เพื่อสร้างบุ๊กมาร์กโดยทำเครื่องหมายจุดเริ่มต้นและจุดสิ้นสุดตามลำดับ อย่าลืมส่งชื่อบุ๊กมาร์กเดียวกันให้กับทั้งสองวิธี บุ๊กมาร์กในเอกสารสามารถทับซ้อนกันและขยายช่วงใดก็ได้ บุ๊กมาร์กที่มีรูปแบบไม่ถูกต้องหรือบุ๊กมาร์กที่มีชื่อซ้ำกันจะถูกละเว้นเมื่อบันทึกเอกสาร

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการสร้างบุ๊กมาร์กใหม่:

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

รับบุ๊กมาร์ก

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

แสดงซ่อนเนื้อหาบุ๊กมาร์ก

บุ๊กมาร์กทั้งหมด (รวมถึงเนื้อหาที่บุ๊กมาร์ก) สามารถห่อหุ้มไว้ในส่วน True ของช่อง IF ได้โดยใช้ Aspose.Words อาจเป็นในลักษณะที่ฟิลด์ IF มี Merge Field ที่ซ้อนกันในนิพจน์ (ด้านซ้ายของตัวดำเนินการ) และขึ้นอยู่กับค่าของ Merge Field ฟิลด์ 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");
}