İmleçle Gezinme

Bir belgeyle çalışırken, ister kısa ister uzun olsun, belgenizin içinde gezinmeniz gerekecektir. Sanal imleçle gezinme, bir belgedeki farklı düğümler arasında gezinme yeteneğini temsil eder.

Kısa bir belgede, belge içinde hareket etmek basittir; çünkü ekleme noktasını istediğiniz yere bulmak için klavyenin ok tuşlarını kullanarak veya fareyi tıklatarak bile ekleme noktasını taşıyabilirsiniz. Ancak çok sayfalı büyük bir belgeye sahip olduğunuzda bu temel teknikler yetersiz kalacaktır.

Bu makalede, bir belgede nasıl hareket edileceği ve sanal imleçle belgenin farklı bölümlerine nasıl gidileceği açıklanmaktadır.

Mevcut İmleç Konumunu Algılama

Belgenizde gezinme işlemine başlamadan önce, o anda seçili olan düğümü almanız gerekecektir. CurrentNode özelliğini kullanarak imlecin seçilen düğümdeki tam konumunu alabilirsiniz. Ayrıca CurrentParagraph ve CurrentSection özelliklerini kullanarak geçerli düğümü almak yerine, seçili paragrafı veya seçili bölümü alabilirsiniz.

DocumentBuilder‘i kullanarak gerçekleştirdiğiniz tüm ekleme işlemleri CurrentNode‘den önce eklenecektir. Geçerli paragraf boş olduğunda veya imleç paragrafın sonunun hemen öncesine getirildiğinde CurrentNode null değerini döndürür.

Belgede Gezinme Yöntemleri

Metni düzenlerken belgenizde nasıl gezineceğinizi ve tam olarak nereye taşınacağınızı bilmek önemlidir. Aspose.Words, bir belgede dolaşmanıza ve farklı bölümlerine ve parçalarına gitmenize olanak tanır; bu, Microsoft Word’deki Gezinme Bölmesi’nin, kaydırma yapmadan bir Word belgesindeki bir sayfaya veya başlığa gitme 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‘in 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-.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);

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-.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.");

Yer İşaretleriyle Gezin

Bulmak istediğiniz bir yeri işaretleyip tekrar kolayca oraya gidebilirsiniz. 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 gidebilirsiniz.

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-.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]);

Tablo Hücrelerine Git

MoveToCell yöntemini kullanarak bir tablo hücresine geçebilirsiniz. Bu yöntem, imlecinizi belirli bir tablodaki herhangi bir hücreye yönlendirmenizi sağlayacaktır. Ek olarak, MoveToCell yönteminde imleci bir hücrede herhangi bir konuma veya belirtilen karaktere taşımak için bir dizin belirleyebilirsiniz.

Aşağıdaki kod örneği, imleç konumunun belirtilen 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-.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());

Bir Alana Git

MoveToField yöntemini kullanarak belgenizdeki belirli bir alana gidebilirsiniz. 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-.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.");

Bir Üst Bilgiye veya Alt Bilgiye Gitme

MoveToHeaderFooter yöntemini kullanarak üstbilgi veya altbilginin başına gidebilirsiniz

Aşağıdaki kod örneği, belge oluşturucu imlecinin belge üstbilgisine veya altbilgisine nasıl taşınacağını gösterir:

// 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);
// Specify that we want headers and footers different for first, even and odd pages.
builder.PageSetup.DifferentFirstPageHeaderFooter = true;
builder.PageSetup.OddAndEvenPagesHeaderFooter = true;
// Create the headers.
builder.MoveToHeaderFooter(HeaderFooterType.HeaderFirst);
builder.Write("Header for the first page");
builder.MoveToHeaderFooter(HeaderFooterType.HeaderEven);
builder.Write("Header for even pages");
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
builder.Write("Header for all other pages");
// Create two pages in the document.
builder.MoveToSection(0);
builder.Writeln("Page1");
builder.InsertBreak(BreakType.PageBreak);
builder.Writeln("Page2");
doc.Save(ArtifactsDir + "AddContentUsingDocumentBuilder.MoveToHeadersFooters.docx");

Bir Bölüme veya Paragrafa Git

MoveToParagraph veya MoveToSection yöntemlerini kullanarak belirli bir bölüme veya paragrafa geçebilirsiniz. Ek olarak, MoveToParagraph yöntemi içinde imleci paragrafta herhangi bir konuma veya belirli bir karaktere taşımak için bir dizin belirleyebilirsiniz.

Aşağıdaki kod örneği, bir belgedeki belirli bir bölüme ve belirli bir paragrafa nasıl gidileceğini gösterir:

// 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));