Navigasie Met Wyser
Terwyl jy met’n dokument werk, selfs al is dit’n kort of lang een, sal jy deur jou dokument moet navigeer. Navigasie met’n virtuele wyser verteenwoordig die vermoë om tussen verskillende nodusse in’n dokument te navigeer.
Binne’n kort dokument is dit eenvoudig om in’n dokument rond te beweeg, aangesien u die invoegpunt kan skuif, selfs deur die pyltjie-sleutels van die sleutelbord te gebruik of deur op die muis te klik om die invoegpunt te vind waar u wil. Maar sodra jy’n groot dokument het wat baie bladsye het, sal hierdie basiese tegnieke onvoldoende wees.
Hierdie artikel verduidelik hoe om in’n dokument rond te beweeg en met’n virtuele wyser na verskillende dele daarvan te navigeer.
Bespeur Huidige Wyser Posisie
Voordat u die proses van navigasie deur u dokument begin, moet u die nodus kry wat tans gekies is. Jy kan die presiese posisie van die wyser by’n geselekteerde knoop kry deur die CurrentNode eienskap te gebruik. Daarbenewens, in plaas van om die huidige nodus te kry, kan jy die huidiglik geselekteerde paragraaf of die huidiglik geselekteerde afdeling kry deur die CurrentParagraph en CurrentSection eienskappe te gebruik.
Enige insetselbewerkings wat u met die DocumentBuilder uitvoer, sal voor die CurrentNode ingevoeg word. Wanneer die huidige paragraaf leeg is of die wyser net voor die einde van die paragraaf geplaas word, gee die CurrentNode nullptr terug.
Navigasie Metodes in’n Dokument
Wanneer jy teks redigeer, is dit belangrik om te weet hoe om jou dokument te navigeer en waar presies om daarin te beweeg. Aspose.Words laat jou toe om in’n dokument rond te beweeg en na sy verskillende afdelings en dele te navigeer – dit is soortgelyk aan die funksionaliteit van die Navigasiepaneel in Microsoft Word om na’n bladsy of opskrif in’n Word-dokument te gaan sonder om te blaai.
Die belangrikste metode is om die wyserposisie na’n spesifieke knoop in jou dokument te kan skuif, jy kan dit bereik deur die MoveTo metode te gebruik.
Die volgende kode voorbeeld toon hoe om die DocumentBuilder te skuif na verskillende nodes in’n dokument:
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()); |
Maar behalwe die basiese MoveTo metode, is daar meer spesifieke.
Navigeer Na Begin of Einde Van’n Dokument
Jy kan na die begin of einde van jou dokument gaan deur die MoveToDocumentStart en MoveToDocumentEnd metodes te gebruik.
Die volgende kode voorbeeld toon hoe om die wyser posisie te skuif na die begin of die einde van’n dokument:
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."); |
Navigeer Met Boekmerke
Jy kan’n plek merk wat jy wil vind en maklik weer daarheen beweeg. Jy kan soveel boekmerke in jou dokument invoeg as wat jy wil, en dan daardeur navigeer deur die boekmerke met unieke name te identifiseer. U kan na’n boekmerk beweeg deur die MoveToBookmark - metode te gebruik.
Die volgende kode voorbeelde toon hoe om’n wyser posisie te skuif na’n boekmerk:
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()); |
Navigeer Na Tabel Selle
Jy kan na’n tabel sel beweeg deur die MoveToCell metode te gebruik. Hierdie metode sal jou in staat stel om jou wyser in enige sel in’n spesifieke tabel te navigeer. Daarbenewens kan u’n indeks spesifiseer om die wyser na enige posisie of gespesifiseerde karakter in’n sel binne die MoveToCell - metode te skuif.
Die volgende kode voorbeeld toon hoe om’n wyser posisie te skuif na’n gespesifiseerde tabel sel:
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"); |
Navigeer na’n Veld
U kan na’n spesifieke veld in u dokument beweeg deur die MoveToField - metode te gebruik. Daarbenewens kan jy na’n spesifieke samesmeltingsveld beweeg deur die MoveToMergeField - metode te gebruik.
Die volgende kode voorbeeld toon hoe om die dokument bouer wyser te skuif na’n spesifieke veld:
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."); |
Navigeer na’n Koptekst of Voetskrif
U kan na die begin van’n koptekst of voetskrif beweeg deur die MoveToHeaderFooter - metode te gebruik.
Die volgende kode voorbeeld toon hoe om dokument bouer wyser te skuif na’n dokument kop of voetskrif:
Navigeer na’n Afdeling of Paragraaf
Jy kan na’n spesifieke afdeling of paragraaf beweeg deur die MoveToParagraph of MoveToSection metodes te gebruik. Daarbenewens kan u’n indeks spesifiseer om die wyser na enige posisie of’n gespesifiseerde karakter in’n paragraaf binne die MoveToParagraph - metode te skuif.
Die volgende kode voorbeeld toon hoe om te beweeg na’n spesifieke afdeling en’n spesifieke paragraaf in’n dokument:
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); |