İmleç ile Gezinme
Bir belgeyle çalışırken, kısa veya uzun olsa bile, belgenizde gezinmeniz gerekir. Sanal imleçle gezinme, bir belgedeki farklı düğümler arasında gezinme yeteneğini temsil eder.
Kısa bir belgede, ekleme noktasını klavyenin ok tuşlarını kullanarak veya ekleme noktasını istediğiniz yere bulmak için fareyi tıklatarak bile taşıyabildiğiniz için belgede dolaşmak basittir. Ancak çok sayıda sayfası olan büyük bir belgeye sahip olduğunuzda, bu temel teknikler yetersiz kalacaktır.
Bu makalede, bir belgede nasıl gezinileceği ve sanal bir imleçle belgenin farklı bölümlerine nasıl gidileceği açıklanmaktadır.
Geçerli İmleç Konumunu Algılama
Belgenizde gezinme işlemine başlamadan önce, o anda seçili olan düğümü almanız gerekir. CurrentNode özelliğini kullanarak imlecin seçili bir düğümdeki tam konumunu alabilirsiniz. Ayrıca, geçerli düğümü almak yerine, CurrentParagraph ve CurrentSection özelliklerini kullanarak seçili paragrafı veya seçili bölümü alabilirsiniz.
DocumentBuilder ‘i kullanarak gerçekleştirdiğiniz tüm ekleme işlemleri CurrentNode‘dan önce eklenir. Geçerli paragraf boş olduğunda veya imleç paragrafın sonundan hemen önce konumlandırıldığında, CurrentNode nullptr değerini döndürür.
Belgede Gezinme Yöntemleri
Metni düzenlerken, belgenizde nasıl gezineceğinizi ve belgede tam olarak nereye taşınacağınızı bilmek önemlidir. Aspose.Words bir belgede dolaşmanıza ve farklı bölümlerine ve bölümlerine gitmenize olanak tanır - bu, bir Word belgesindeki bir sayfaya veya başlığa kaydırmadan gitmek için Microsoft Word içindeki Gezinti Bölmesinin işlevine benzer.
Ana yöntem, imleç konumunu belgenizdeki belirli bir düğüme taşıyabilmektir, bunu MoveTo yöntemini kullanarak başarabilirsiniz.
Aşağıdaki kod örneği, DocumentBuilder‘ın bir belgedeki farklı düğümlere nasıl taşınacağını gösterir:
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()); |
Ancak temel MoveTo yönteminin yanı sıra daha spesifik olanlar da var.
Belgenin Başına veya Sonuna Gitme
MoveToDocumentStart ve MoveToDocumentEnd yöntemlerini kullanarak belgenizin başına veya sonuna gidebilirsiniz.
Aşağıdaki kod örneği, imleç konumunun bir belgenin başına veya sonuna nasıl taşınacağını gösterir:
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."); |
Yer imleriyle Gezin
Bulmak istediğiniz bir yeri işaretleyebilir ve kolayca tekrar oraya taşıyabilirsiniz. Belgenize istediğiniz kadar yer imi ekleyebilir ve ardından yer imlerini benzersiz adlarla tanımlayarak bunlar arasında gezinebilirsiniz. MoveToBookmark yöntemini kullanarak bir yer imine geçebilirsiniz.
Aşağıdaki kod örnekleri, imleç konumunun yer imine nasıl taşınacağını gösterir:
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()); |
Tablo Hücrelerine Git
MoveToCell yöntemini kullanarak bir tablo hücresine taşıyabilirsiniz. Bu yöntem, imlecinizi belirli bir tablodaki herhangi bir hücreye yönlendirmenizi sağlar. Ayrıca, imleci MoveToCell yöntemi içindeki bir hücrede herhangi bir konuma veya belirtilen karaktere taşımak için bir dizin belirtebilirsiniz.
Aşağıdaki kod örneği, imleç konumunun belirli bir tablo hücresine nasıl taşınacağını gösterir:
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"); |
Bir Alana Git
MoveToField yöntemini kullanarak belgenizdeki belirli bir alana geçebilirsiniz. Ayrıca, MoveToMergeField yöntemini kullanarak belirli bir birleştirme alanına geçebilirsiniz.
Aşağıdaki kod örneği, belge oluşturucu imlecinin belirli bir alana nasıl taşınacağını gösterir:
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."); |
Üstbilgi veya Altbilgiye Gitme
MoveToHeaderFooter yöntemini kullanarak bir üstbilgi veya altbilginin başına geçebilirsiniz.
Aşağıdaki kod örneği, belge oluşturucu imlecinin belge üstbilgisine veya altbilgisine nasıl taşınacağını gösterir:
Bir Bölüme veya Paragrafa Gitme
MoveToParagraph veya MoveToSection yöntemlerini kullanarak belirli bir bölüme veya paragrafa geçebilirsiniz. Ayrıca, imleci MoveToParagraph yöntemi içindeki bir paragrafta herhangi bir konuma veya belirtilen bir karaktere taşımak için bir dizin belirtebilirsiniz.
Aşağıdaki kod örneği, bir belgedeki belirli bir bölüme ve belirli bir paragrafa nasıl geçileceğini gösterir:
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); |