การนำทางด้วยเคอร์เซอร์

ในขณะที่ทำงานกับเอกสาร แม้ว่าจะเป็นเอกสารสั้นหรือยาว คุณจะต้องเลื่อนดูเอกสารของคุณ การนำทางด้วยเคอร์เซอร์เสมือนแสดงถึงความสามารถในการนำทางระหว่างโหนดต่างๆ ในเอกสาร

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

บทความนี้จะอธิบายวิธีการเลื่อนไปมาในเอกสารและนำทางด้วยเคอร์เซอร์เสมือนไปยังส่วนต่างๆ ของเอกสาร

การตรวจจับตำแหน่งเคอร์เซอร์ปัจจุบัน

ก่อนที่จะเริ่มกระบวนการนำทางผ่านเอกสารของคุณ คุณจะต้องได้รับโหนดที่เลือกไว้ในปัจจุบัน คุณสามารถรับตำแหน่งที่แน่นอนของเคอร์เซอร์ที่โหนดที่เลือกได้โดยใช้คุณสมบัติ CurrentNode นอกจากนี้ แทนที่จะรับโหนดปัจจุบัน คุณสามารถรับย่อหน้าที่เลือกในปัจจุบันหรือส่วนที่เลือกในปัจจุบันได้โดยใช้คุณสมบัติ CurrentParagraph และ CurrentSection

การดำเนินการแทรกใดๆ ที่คุณดำเนินการโดยใช้ DocumentBuilder จะถูกแทรกก่อน CurrentNode เมื่อย่อหน้าปัจจุบันว่างเปล่าหรือเคอร์เซอร์อยู่ก่อนจุดสิ้นสุดของย่อหน้า CurrentNode จะส่งกลับค่าว่าง

วิธีการนำทางในเอกสาร

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

วิธีการหลักคือการสามารถย้ายตำแหน่งเคอร์เซอร์ไปยังโหนดเฉพาะในเอกสารของคุณได้ คุณสามารถทำได้โดยใช้วิธี MoveTo

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีย้าย DocumentBuilder ไปยังโหนดต่างๆ ในเอกสาร:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Start a bookmark and add content to it using a DocumentBuilder.
builder.StartBookmark("MyBookmark");
builder.Writeln("Bookmark contents.");
builder.EndBookmark("MyBookmark");
// The node that the DocumentBuilder is currently at is past the boundaries of the bookmark.
Assert.AreEqual(doc.Range.Bookmarks[0].BookmarkEnd, builder.CurrentParagraph.FirstChild);
// If we wish to revise the content of our bookmark with the DocumentBuilder, we can move back to it like this.
builder.MoveToBookmark("MyBookmark");
// Now we're located between the bookmark's BookmarkStart and BookmarkEnd nodes, so any text the builder adds will be within it.
Assert.AreEqual(doc.Range.Bookmarks[0].BookmarkStart, builder.CurrentParagraph.FirstChild);
// We can move the builder to an individual node,
// which in this case will be the first node of the first paragraph, like this.
builder.MoveTo(doc.FirstSection.Body.FirstParagraph.GetChildNodes(NodeType.Any, false)[0]);
Assert.AreEqual(NodeType.BookmarkStart, builder.CurrentNode.NodeType);
Assert.IsTrue(builder.IsAtStartOfParagraph);
// A shorter way of moving the very start/end of a document is with these methods.
builder.MoveToDocumentEnd();
Assert.IsTrue(builder.IsAtEndOfParagraph);
builder.MoveToDocumentStart();
Assert.IsTrue(builder.IsAtStartOfParagraph);

แต่นอกเหนือจากวิธี MoveTo พื้นฐานแล้ว ยังมีวิธีที่เฉพาะเจาะจงมากกว่าอีกด้วย

นำทางไปยังจุดเริ่มต้นหรือจุดสิ้นสุดของเอกสาร

คุณสามารถไปที่จุดเริ่มต้นหรือจุดสิ้นสุดของเอกสารโดยใช้วิธี MoveToDocumentStart และ MoveToDocumentEnd

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการย้ายตำแหน่งเคอร์เซอร์ไปที่จุดเริ่มต้นหรือจุดสิ้นสุดของเอกสาร:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Move the cursor position to the beginning of your document.
builder.MoveToDocumentStart();
Console.WriteLine("\nThis is the beginning of the document.");
// Move the cursor position to the end of your document.
builder.MoveToDocumentEnd();
Console.WriteLine("\nThis is the end of the document.");

นำทางด้วยบุ๊กมาร์ก

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

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Start a bookmark and add content to it using a DocumentBuilder.
builder.StartBookmark("MyBookmark");
builder.Writeln("Bookmark contents.");
builder.EndBookmark("MyBookmark");
// The node that the DocumentBuilder is currently at is past the boundaries of the bookmark.
Assert.AreEqual(doc.Range.Bookmarks[0].BookmarkEnd, builder.CurrentParagraph.FirstChild);
// If we wish to revise the content of our bookmark with the DocumentBuilder, we can move back to it like this.
builder.MoveToBookmark("MyBookmark");
// Now we're located between the bookmark's BookmarkStart and BookmarkEnd nodes, so any text the builder adds will be within it.
Assert.AreEqual(doc.Range.Bookmarks[0].BookmarkStart, builder.CurrentParagraph.FirstChild);
// We can move the builder to an individual node,
// which in this case will be the first node of the first paragraph, like this.
builder.MoveTo(doc.FirstSection.Body.FirstParagraph.GetChildNodes(NodeType.Any, false)[0]);

นำทางไปยังเซลล์ตาราง

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

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการย้ายตำแหน่งเคอร์เซอร์ไปยังเซลล์ตารางที่ระบุ:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document(MyDir + "Tables.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
// Move the builder to row 3, cell 4 of the first table.
builder.MoveToCell(0, 2, 3, 0);
builder.Write("\nCell contents added by DocumentBuilder");
Table table = (Table)doc.GetChild(NodeType.Table, 0, true);
Assert.AreEqual(table.Rows[2].Cells[3], builder.CurrentNode.ParentNode.ParentNode);
Assert.AreEqual("Cell contents added by DocumentBuilderCell 3 contents\a", table.Rows[2].Cells[3].GetText().Trim());

นำทางไปยังฟิลด์

คุณสามารถย้ายไปยังช่องเฉพาะในเอกสารของคุณได้โดยใช้วิธี MoveToField นอกจากนี้ คุณยังสามารถย้ายไปยังเขตข้อมูลผสานเฉพาะได้โดยใช้วิธี MoveToMergeField

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการย้ายเคอร์เซอร์ตัวสร้างเอกสารไปยังฟิลด์เฉพาะ:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert a field using the DocumentBuilder and add a run of text after it.
Field field = builder.InsertField("MERGEFIELD field");
builder.Write(" Text after the field.");
// The builder's cursor is currently at end of the document.
Assert.Null(builder.CurrentNode);
// We can move the builder to a field like this, placing the cursor at immediately after the field.
builder.MoveToField(field, true);
// Note that the cursor is at a place past the FieldEnd node of the field, meaning that we are not actually inside the field.
// If we wish to move the DocumentBuilder to inside a field,
// we will need to move it to a field's FieldStart or FieldSeparator node using the DocumentBuilder.MoveTo() method.
Assert.AreEqual(field.End, builder.CurrentNode.PreviousSibling);
builder.Write(" Text immediately after the field.");

นำทางไปยังส่วนหัวหรือส่วนท้าย

คุณสามารถย้ายไปยังจุดเริ่มต้นของส่วนหัวหรือส่วนท้ายได้โดยใช้วิธี MoveToHeaderFooter

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการย้ายเคอร์เซอร์ตัวสร้างเอกสารไปยังส่วนหัวหรือส่วนท้ายของเอกสาร:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Specify that we want headers and footers different for first, even and odd pages.
builder.PageSetup.DifferentFirstPageHeaderFooter = true;
builder.PageSetup.OddAndEvenPagesHeaderFooter = true;
// Create the headers.
builder.MoveToHeaderFooter(HeaderFooterType.HeaderFirst);
builder.Write("Header for the first page");
builder.MoveToHeaderFooter(HeaderFooterType.HeaderEven);
builder.Write("Header for even pages");
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
builder.Write("Header for all other pages");
// Create two pages in the document.
builder.MoveToSection(0);
builder.Writeln("Page1");
builder.InsertBreak(BreakType.PageBreak);
builder.Writeln("Page2");
doc.Save(ArtifactsDir + "AddContentUsingDocumentBuilder.MoveToHeadersFooters.docx");

นำทางไปยังส่วนหรือย่อหน้า

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

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการย้ายไปยังส่วนเฉพาะและย่อหน้าเฉพาะในเอกสาร:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
doc.AppendChild(new Section(doc));
// Move a DocumentBuilder to the second section and add text.
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToSection(1);
builder.Writeln("Text added to the 2nd section.");
// Create document with paragraphs.
doc = new Document(MyDir + "Paragraphs.docx");
ParagraphCollection paragraphs = doc.FirstSection.Body.Paragraphs;
Assert.AreEqual(22, paragraphs.Count);
// When we create a DocumentBuilder for a document, its cursor is at the very beginning of the document by default,
// and any content added by the DocumentBuilder will just be prepended to the document.
builder = new DocumentBuilder(doc);
Assert.AreEqual(0, paragraphs.IndexOf(builder.CurrentParagraph));
// You can move the cursor to any position in a paragraph.
builder.MoveToParagraph(2, 10);
Assert.AreEqual(2, paragraphs.IndexOf(builder.CurrentParagraph));
builder.Writeln("This is a new third paragraph. ");
Assert.AreEqual(3, paragraphs.IndexOf(builder.CurrentParagraph));