Điều hướng bằng con trỏ
Trong khi làm việc với một tài liệu, ngay cả khi nó ngắn hay dài, bạn sẽ cần điều hướng qua tài liệu của mình. Điều hướng bằng con trỏ ảo thể hiện khả năng điều hướng giữa các nút khác nhau trong tài liệu.
Trong một tài liệu ngắn, việc di chuyển trong tài liệu rất đơn giản vì bạn có thể di chuyển điểm chèn thậm chí bằng cách sử dụng các phím mũi tên của bàn phím hoặc bằng cách nhấp chuột để định vị điểm chèn ở bất cứ đâu bạn muốn. Nhưng một khi bạn có một tài liệu lớn có nhiều trang thì những kỹ thuật cơ bản này sẽ không đủ.
Bài viết này giải thích cách di chuyển trong tài liệu và điều hướng bằng con trỏ ảo đến các phần khác nhau của tài liệu.
Phát hiện vị trí con trỏ hiện tại
Trước khi bắt đầu quá trình điều hướng qua tài liệu của bạn, bạn sẽ cần lấy nút hiện được chọn. Bạn có thể lấy vị trí chính xác của con trỏ tại nút đã chọn bằng cách sử dụng thuộc tính CurrentNode. Ngoài ra, thay vì lấy nút hiện tại, bạn có thể lấy đoạn hiện được chọn hoặc phần hiện được chọn bằng cách sử dụng thuộc tính CurrentParagraph và CurrentSection.
Mọi thao tác chèn bạn thực hiện bằng DocumentBuilder sẽ được chèn trước CurrentNode. Khi đoạn văn hiện tại trống hoặc con trỏ được đặt ngay trước cuối đoạn văn, CurrentNode sẽ trả về giá trị rỗng.
Phương pháp điều hướng trong tài liệu
Khi bạn chỉnh sửa văn bản, điều quan trọng là phải biết cách điều hướng tài liệu và vị trí chính xác để di chuyển trong đó. Aspose.Words cho phép bạn di chuyển trong tài liệu và điều hướng đến các phần và phần khác nhau của nó - điều này tương tự như chức năng của Ngăn Điều hướng trong Microsoft Word để đi đến một trang hoặc tiêu đề trong tài liệu Word mà không cần cuộn.
Phương pháp chính là có thể di chuyển vị trí con trỏ đến một nút cụ thể trong tài liệu của bạn, bạn có thể đạt được điều này bằng cách sử dụng phương pháp MoveTo.
Ví dụ về mã sau đây cho thấy cách di chuyển DocumentBuilder sang các nút khác nhau trong tài liệu:
// 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); |
Nhưng bên cạnh phương pháp MoveTo cơ bản, còn có những phương pháp cụ thể hơn.
Điều hướng đến phần đầu hoặc phần cuối của tài liệu
Bạn có thể đi đến đầu hoặc cuối tài liệu của mình bằng phương pháp MoveToDocumentStart và MoveToDocumentEnd.
Ví dụ mã sau đây cho thấy cách di chuyển vị trí con trỏ đến đầu hoặc cuối tài liệu:
// 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."); |
Điều hướng bằng dấu trang
Bạn có thể đánh dấu một địa điểm mà bạn muốn tìm và di chuyển đến đó một cách dễ dàng. Bạn có thể chèn bao nhiêu dấu trang vào tài liệu của mình tùy thích, sau đó điều hướng qua chúng bằng cách xác định các dấu trang có tên duy nhất. Bạn có thể di chuyển đến dấu trang bằng cách sử dụng phương pháp MoveToBookmark.
Các ví dụ mã sau đây cho thấy cách di chuyển vị trí con trỏ tới dấu trang:
// 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]); |
Điều hướng đến các ô trong bảng
Bạn có thể di chuyển đến một ô trong bảng bằng cách sử dụng phương pháp MoveToCell. Phương pháp này sẽ cho phép bạn điều hướng con trỏ vào bất kỳ ô nào trong một bảng cụ thể. Ngoài ra, bạn có thể chỉ định một chỉ mục để di chuyển con trỏ đến bất kỳ vị trí hoặc ký tự nào được chỉ định trong một ô trong phương thức MoveToCell.
Ví dụ mã sau đây cho thấy cách di chuyển vị trí con trỏ đến một ô trong bảng được chỉ định:
// 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()); |
Điều hướng đến một trường
Bạn có thể di chuyển đến một trường cụ thể trong tài liệu của mình bằng cách sử dụng phương pháp MoveToField. Ngoài ra, bạn có thể di chuyển đến trường hợp nhất cụ thể bằng cách sử dụng phương pháp MoveToMergeField.
Ví dụ mã sau đây cho biết cách di chuyển con trỏ của trình tạo tài liệu đến một trường cụ thể:
// 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."); |
Điều hướng đến Đầu trang hoặc Chân trang
Bạn có thể di chuyển đến đầu đầu trang hoặc chân trang bằng cách sử dụng phương pháp MoveToHeaderFooter
Ví dụ về mã sau đây cho biết cách di chuyển con trỏ của trình tạo tài liệu đến đầu trang hoặc chân trang của tài liệu:
Điều hướng đến một Phần hoặc Đoạn
Bạn có thể di chuyển đến một phần hoặc đoạn cụ thể bằng cách sử dụng phương pháp MoveToParagraph hoặc MoveToSection. Ngoài ra, bạn có thể chỉ định một chỉ mục để di chuyển con trỏ đến bất kỳ vị trí nào hoặc một ký tự được chỉ định trong một đoạn văn trong phương thức MoveToParagraph.
Ví dụ về mã sau đây cho biết cách di chuyển đến một phần cụ thể và một đoạn văn cụ thể trong tài liệu:
// 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)); |