Nawigacja z kursorem
Podczas pracy z dokumentem, nawet jeśli jest on krótki lub długi, musisz poruszać się po dokumencie. Nawigacja za pomocą wirtualnego kursora reprezentuje możliwość poruszania się między różnymi węzłami w dokumencie.
W krótkim dokumencie poruszanie się w dokumencie jest proste, ponieważ możesz przesunąć punkt wstawiania nawet za pomocą klawiszy strzałek na klawiaturze lub klikając myszką, aby zlokalizować punkt wstawiania w dowolnym miejscu. Ale gdy masz duży dokument, który ma wiele stron, te podstawowe techniki będą niewystarczające.
W tym artykule wyjaśniono, jak poruszać się w dokumencie i nawigować za pomocą wirtualnego kursora do różnych jego części.
Wykrywanie Aktualnej Pozycji Kursora
Przed rozpoczęciem procesu poruszania się po dokumencie musisz uzyskać aktualnie wybrany węzeł. Dokładne położenie kursora w wybranym węźle można uzyskać za pomocą właściwości CurrentNode. Ponadto zamiast pobierać bieżący węzeł, możesz uzyskać aktualnie wybrany akapit lub aktualnie wybraną sekcję, używając właściwości CurrentParagraph i CurrentSection.
Wszelkie operacje wstawiania wykonywane przy użyciu DocumentBuilder zostaną wstawione przed CurrentNode. Gdy bieżący akapit jest pusty lub kursor znajduje się tuż przed końcem akapitu, CurrentNode zwraca nullptr.
Metody nawigacji w dokumencie
Podczas edycji tekstu ważne jest, aby wiedzieć, jak poruszać się po dokumencie i gdzie dokładnie się w nim poruszać. Aspose.Words umożliwia poruszanie się w dokumencie i przechodzenie do jego różnych sekcji i części – jest to podobne do funkcji okienka nawigacji w Microsoft Word, aby przejść do strony lub nagłówka w dokumencie Word bez przewijania.
Główną metodą jest przesunięcie pozycji kursora do określonego węzła w dokumencie, można to osiągnąć za pomocą metody MoveTo.
Poniższy przykład kodu pokazuje, jak przenieść DocumentBuilder do różnych węzłów w dokumencie:
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
auto doc = System::MakeObject<Document>(); | |
auto builder = System::MakeObject<DocumentBuilder>(doc); | |
// Start a bookmark and add content to it using a DocumentBuilder. | |
builder->StartBookmark(u"MyBookmark"); | |
builder->Writeln(u"Bookmark contents."); | |
builder->EndBookmark(u"MyBookmark"); | |
// The node that the DocumentBuilder is currently at is past the boundaries of the bookmark. | |
ASSERT_EQ(doc->get_Range()->get_Bookmarks()->idx_get(0)->get_BookmarkEnd(), builder->get_CurrentParagraph()->get_FirstChild()); | |
// If we wish to revise the content of our bookmark with the DocumentBuilder, we can move back to it like this. | |
builder->MoveToBookmark(u"MyBookmark"); | |
// Now we're located between the bookmark's BookmarkStart and BookmarkEnd nodes, so any text the builder adds will be within it. | |
ASSERT_EQ(doc->get_Range()->get_Bookmarks()->idx_get(0)->get_BookmarkStart(), builder->get_CurrentParagraph()->get_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->get_FirstSection()->get_Body()->get_FirstParagraph()->GetChildNodes(NodeType::Any, false)->idx_get(0)); | |
ASSERT_EQ(NodeType::BookmarkStart, builder->get_CurrentNode()->get_NodeType()); | |
ASSERT_TRUE(builder->get_IsAtStartOfParagraph()); | |
// A shorter way of moving the very start/end of a document is with these methods. | |
builder->MoveToDocumentEnd(); | |
ASSERT_TRUE(builder->get_IsAtEndOfParagraph()); | |
builder->MoveToDocumentStart(); | |
ASSERT_TRUE(builder->get_IsAtStartOfParagraph()); |
Ale oprócz podstawowej metody MoveTo istnieją bardziej szczegółowe.
Przejdź do początku lub końca dokumentu
Możesz przejść do początku lub końca dokumentu za pomocą metod MoveToDocumentStart i MoveToDocumentEnd.
Poniższy przykład kodu pokazuje, jak przesunąć pozycję kursora na początek lub koniec dokumentu:
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
auto doc = System::MakeObject<Document>(inputDataDir + u"Document.docx"); | |
auto builder = System::MakeObject<DocumentBuilder>(doc); | |
// Move the cursor position to the beginning of your document. | |
builder->MoveToDocumentStart(); | |
builder->Writeln(u"This is the beginning of the document."); | |
// Move the cursor position to the end of your document. | |
builder->MoveToDocumentEnd(); | |
builder->Writeln(u"This is the end of the document."); |
Nawiguj Za Pomocą Zakładek
Możesz zaznaczyć miejsce, które chcesz znaleźć i łatwo się do niego przenieść. Możesz wstawić do dokumentu dowolną liczbę zakładek, a następnie poruszać się po nich, identyfikując zakładki z unikalnymi nazwami. Możesz przejść do zakładki za pomocą metody MoveToBookmark.
Poniższe przykłady kodu pokazują, jak przenieść pozycję kursora do zakładki:
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
auto doc = System::MakeObject<Document>(); | |
auto builder = System::MakeObject<DocumentBuilder>(doc); | |
// Start a bookmark and add content to it using a DocumentBuilder. | |
builder->StartBookmark(u"MyBookmark"); | |
builder->Writeln(u"Bookmark contents."); | |
builder->EndBookmark(u"MyBookmark"); | |
// If we wish to revise the content of our bookmark with the DocumentBuilder, we can move back to it like this. | |
builder->MoveToBookmark(u"MyBookmark"); | |
// Now we're located between the bookmark's BookmarkStart and BookmarkEnd nodes, so any text the builder adds will be within it. | |
ASSERT_EQ(doc->get_Range()->get_Bookmarks()->idx_get(0)->get_BookmarkStart(), builder->get_CurrentParagraph()->get_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->get_FirstSection()->get_Body()->get_FirstParagraph()->GetChildNodes(NodeType::Any, false)->idx_get(0)); | |
ASSERT_EQ(NodeType::BookmarkStart, builder->get_CurrentNode()->get_NodeType()); |
Przejdź do komórek tabeli
Możesz przejść do komórki tabeli za pomocą metody MoveToCell. Ta metoda umożliwi nawigację kursorem do dowolnej komórki w określonej tabeli. Ponadto można określić indeks, aby przesunąć kursor do dowolnej pozycji lub określonego znaku w komórce w metodzie MoveToCell.
Poniższy przykład kodu pokazuje, jak przenieść pozycję kursora do określonej komórki tabeli:
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
auto doc = System::MakeObject<Document>(inputDataDir + u"Tables.docx"); | |
auto builder = System::MakeObject<DocumentBuilder>(doc); | |
// Move the builder to row 3, cell 4 of the first table. | |
builder->MoveToCell(0, 2, 3, 0); | |
builder->Write(u"\nCell contents added by DocumentBuilder"); | |
auto table = System::DynamicCast<Tables::Table>(doc->GetChild(NodeType::Table, 0, true)); | |
ASSERT_EQ(table->get_Rows()->idx_get(2)->get_Cells()->idx_get(3), builder->get_CurrentNode()->get_ParentNode()->get_ParentNode()); | |
ASSERT_EQ(table->get_Rows()->idx_get(2)->get_Cells()->idx_get(2)->GetText().Trim(), u"Cell contents added by DocumentBuilderCell 3 contents\a"); |
Przejdź do pola
Możesz przejść do określonego pola w dokumencie za pomocą metody MoveToField. Ponadto możesz przejść do określonego pola scalania za pomocą metody MoveToMergeField.
Poniższy przykład kodu pokazuje, jak przenieść kursor kreatora dokumentów do określonego pola:
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
auto doc = System::MakeObject<Document>(); | |
auto builder = System::MakeObject<DocumentBuilder>(doc); | |
// Insert a field using the DocumentBuilder and add a run of text after it. | |
auto field = builder->InsertField(u"MERGEFIELD field"); | |
builder->Write(u" Text after the field."); | |
// The builder's cursor is currently at end of the document. | |
ASSERT_EQ(builder->get_CurrentNode(), nullptr); | |
// 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_EQ(field->get_End(), builder->get_CurrentNode()->get_PreviousSibling()); | |
builder->Write(u" Text immediately after the field."); |
Przejdź do nagłówka lub stopki
Możesz przejść na początek nagłówka lub stopki, używając metody MoveToHeaderFooter.
Poniższy przykład kodu pokazuje, jak przenieść kursor kreatora dokumentów do nagłówka lub stopki dokumentu:
Przejdź do sekcji lub akapitu
Możesz przejść do określonej sekcji lub akapitu, używając metod MoveToParagraph LUB MoveToSection. Ponadto można określić indeks, aby przesunąć kursor do dowolnej pozycji lub określonego znaku w akapicie w metodzie MoveToParagraph.
Poniższy przykład kodu pokazuje, jak przejść do określonej sekcji i określonego akapitu w dokumencie:
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
// Create a blank document and append a section to it, giving it two sections. | |
auto doc = System::MakeObject<Document>(); | |
doc->AppendChild(System::MakeObject<Section>(doc)); | |
// Move a DocumentBuilder to the second section and add text. | |
auto builder = System::MakeObject<DocumentBuilder>(doc); | |
builder->MoveToSection(1); | |
builder->Writeln(u"Text added to the 2nd section."); | |
// Create document with paragraphs. | |
auto doc = System::MakeObject<Document>(inputDataDir + u"Paragraphs.docx"); | |
auto paragraphs = doc->get_FirstSection()->get_Body()->get_Paragraphs(); | |
ASSERT_EQ(paragraphs->get_Count(), 22); | |
// 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. | |
auto builder = System::MakeObject<DocumentBuilder>(doc); | |
ASSERT_EQ(paragraphs->IndexOf(builder->get_CurrentParagraph()), 0); | |
// You can move the cursor to any position in a paragraph. | |
builder->MoveToParagraph(0, 14); | |
ASSERT_EQ(paragraphs->IndexOf(builder->get_CurrentParagraph()), 2); | |
builder->Writeln(u"This is a new third paragraph. "); | |
ASSERT_EQ(paragraphs->IndexOf(builder->get_CurrentParagraph()), 3); |