Điều hướng với con trỏ
Trong khi đang làm việc với một tài liệu, ngay cả khi nó là ngắn hoặc dài, bạn sẽ cần phải điều hướng qua tài liệu của mình. Điều hướng với con trỏ ảo đại diện cho khả năng điều hướng giữa các nút khác nhau trong một tài liệu.
Trong một tài liệu ngắn gọn, việc di chuyển trong một tài liệu đơn giản như bạn có thể di chuyển điểm chèn ngay cả bằng cách sử dụng bàn phím các phím mũi tên hoặc bằng cách nhấp chuột để xác định điểm chèn bất cứ nơi nào bạn muốn. Nhưng một khi bạn đã có tài liệu lớn với nhiều trang, những kỹ thuật cơ bản này sẽ là không đủ.
Bài viết này giải thích cách di chuyển trong một tài liệu và điều hướng với con trỏ ảo đến các phần khác nhau của nó.
Tìm 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 nhận được nút mà hiện đang được chọn. Bạn có thể lấy vị trí chính xác của con trỏ tại một nút được chọn bằng cách sử dụng thuộc tính CurrentNode. Ngoài ra, thay vì nhận lấy nút hiện tại, bạn có thể nhận lấy đoạn văn đang được chọn hoặc phần đang được chọn bằng cách sử dụng thuộc tính CurrentParagraph và CurrentSection.
Bất kỳ các thao tác chèn nào bạn thực hiện bằng cách sử dụ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 thì CurrentNode trả về giá trị null.
Phương pháp điều hướng trong một tài liệu
Khi chỉnh sửa văn bản, điều quan trọng là biết cách di chuyển trên tài liệu của bạn và chính xác nơi để di chuyển trong đó. Aspose.Words cho phép bạn di chuyển xung quanh một tài liệu và điều hướng đến các phần và phần khác nhau - điều này tương tự như chức năng của thanh điều hướng ở Microsoft Word để đi đến trang hoặc tiêu đề trong một tài liệu Word mà không cần cuộn.
Phương pháp chính là có khả năng 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.
Mã ví dụ sau cho thấy cách di chuyển DocumentBuilder đến các node khác nhau trong một tài liệu:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
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.assertEquals(doc.getRange().getBookmarks().get(0).getBookmarkEnd(), builder.getCurrentParagraph().getFirstChild()); | |
// 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.assertEquals(doc.getRange().getBookmarks().get(0).getBookmarkStart(), builder.getCurrentParagraph().getFirstChild()); | |
// 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.getFirstSection().getBody().getFirstParagraph().getChildNodes(NodeType.ANY, false).get(0)); | |
Assert.assertEquals(NodeType.BOOKMARK_START, builder.getCurrentNode().getNodeType()); | |
Assert.assertTrue(builder.isAtStartOfParagraph()); | |
// A shorter way of moving the very start/end of a document is with these methods. | |
builder.moveToDocumentEnd(); | |
Assert.assertTrue(builder.isAtEndOfParagraph()); | |
builder.moveToDocumentStart(); | |
Assert.assertTrue(builder.isAtStartOfParagraph()); |
Nhưng ngoài phương pháp cơ bản MoveTo, còn có những phương pháp cụ thể hơn.
Điều hướng đến Đầu hoặc Cuối của một Tài liệu
Bạn có thể đi đến đầu hoặc cuối tài liệu của bạn bằng cách sử dụng các phương pháp MoveToDocumentStart và MoveToDocumentEnd.
Ví dụ sau cho thấy cách di chuyển vị trí con trỏ đến đầu hoặc cuối của một tài liệu":
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
String dataDir = Utils.getDataDir(DocumentBuilderMoveToDocumentStartEnd.class); | |
Document doc = new Document(dataDir + "Document.docx"); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
// Move the cursor position to the beginning of your document. | |
builder.moveToDocumentStart(); | |
builder.writeln("This is the beginning of the document."); | |
// Move the cursor position to the end of your document. | |
builder.moveToDocumentEnd(); | |
builder.writeln("This is the end of the document."); |
Điều hướng với Bookmark
Bạn có thể đánh dấu một vị trí mà bạn muốn tìm và dễ dàng di chuyển đến đó lại. Bạn có thể thêm nhiều dấu đánh dấu vào tài liệu của bạn như bạn muốn và sau đó điều hướng thông qua chúng bằng cách xác định các dấu đánh dấu với tên độc đáo. Bạn có thể di chuyển đến một dấu trang bằng cách sử dụng MoveToBookmark phương pháp.
Các ví dụ mã sau cho thấy cách di chuyển vị trí con trỏ đến một con dấu":
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
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"); | |
// 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.assertEquals(doc.getRange().getBookmarks().get(0).getBookmarkStart(), builder.getCurrentParagraph().getFirstChild()); | |
// 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.getFirstSection().getBody().getFirstParagraph().getChildNodes(NodeType.ANY, false).get(0)); | |
Assert.assertEquals(NodeType.BOOKMARK_START, builder.getCurrentNode().getNodeType()); |
Điều hướng đến Table Cells
Bạn có thể di chuyển đến một tế bào 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 trong một ô trong phương thức MoveToCell.
Mã ví dụ sau cho thấy cách di chuyển vị trí con trỏ đến một tế bào bảng được chỉ định:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
String dataDir = Utils.getDataDir(DocumentBuilderMoveToTableCell.class); | |
Document doc = new Document(dataDir + "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.assertEquals(table.getRows().get(2).getCells().get(3), builder.getCurrentNode().getParentNode().getParentNode()); | |
Assert.assertEquals("Cell contents added by DocumentBuilderCell 3 contents\u0007", table.getRows().get(2).getCells().get(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 bạn bằng cách sử dụng phương thức MoveToField. Bên cạnh đó bạn có thể di chuyển đến một trường hợp cụ thể bằng cách sử dụng MoveToMergeField phương pháp.
Ví dụ sau cho thấy cách di chuyển con trỏ bộ xây dựng 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-Java | |
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.assertNull(builder.getCurrentNode()); | |
// 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.assertEquals(field.getEnd(), builder.getCurrentNode().getPreviousSibling()); | |
builder.write(" Text immediately after the field."); |
Điều hướng đến Header hoặc Footer
Bạn có thể di chuyển đến đầu của tiêu đề hoặc chân trang bằng cách sử dụng phương pháp MoveToHeaderFooter này
Mã ví dụ sau cho thấy cách di chuyển con trỏ của trình tạo tài liệu đến tiêu đề hoặc chân trang tài liệu:
Điều hướng đến một Phần hoặc Đoạn văn
Bạn có thể di chuyển đến một phần cụ thể hoặc đoạn bằng cách sử dụng các phương pháp MoveToParagraph hay MoveToSection. Ngoài ra, bạn có thể chỉ định một số để di chuyển con trỏ đến bất kỳ vị trí nào hoặc một ký tự cụ thể trong một đoạn văn bản trong phương thức MoveToParagraph này.
Mã ví dụ sau cho thấy 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-Java | |
String dataDir = Utils.getDataDir(DocumentBuilderMoveToSectionParagraph.class); | |
// Create a blank document and append a section to it, giving it two sections. | |
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(dataDir + "Paragraphs.docx"); | |
ParagraphCollection paragraphs = doc.getFirstSection().getBody().getParagraphs(); | |
Assert.assertEquals(22, paragraphs.getCount()); | |
// 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.assertEquals(0, paragraphs.indexOf(builder.getCurrentParagraph())); | |
// You can move the cursor to any position in a paragraph. | |
builder.moveToParagraph(0, 14); | |
Assert.assertEquals(2, paragraphs.indexOf(builder.getCurrentParagraph())); | |
builder.writeln("This is a new third paragraph. "); | |
Assert.assertEquals(3, paragraphs.indexOf(builder.getCurrentParagraph())); |