Kursör ile Gezinme

Bir belge üzerinde çalışırken, kısa veya uzun olsun, belgenin içinden geçmeniz gerekecektir. Sanal imleç ile gezinme farklı noktalar arasında bir belgeyi gezme yeteneğini temsil eder.

Kısa bir belge içinde, bir belgedeki gezinme basitçe imleci klavyenin ok tuşlarını veya imleci tıklayarak istediğiniz yerde konumlandırarak bile hareket ettirebilir. Ancak çok sayıda sayfaya sahip büyük bir belgeniz varsa bu temel teknikler yetersiz kalacaktır.

Bu makale bir belgeyi nasıl gezileceğini ve sanal imleç kullanarak farklı kısımlarına nasıl yönlendirileceğini açıklamaktadır.

Geçerli Fare Konumunu Algılama

Dokümanınızdan geçmeye başlamadan önce seçili düğümü almanız gerekecek. Seçili bir düğüme göre tam fare imleci konumunu almak için CurrentNode özelliğini kullanabilirsiniz. Buna ek olarak, geçerli düğüm yerine, geçerli paragrafı veya şu anda seçilen bölümü almak için CurrentParagraph ve CurrentSection özelliklerini kullanabilirsiniz.

Bu DocumentBuilder kullanarak gerçekleştirdiğiniz herhangi bir ekleme işlemi, CurrentNode ‘den önce eklenecektir. Geçerli paragraf boşsa veya imleç paragrafın sonunda doğru konumlandırılmışsa, CurrentNode null döndürür.

Bir Dokümanda Navigasyon Yöntemleri

Metin düzenlerken, belgenizi nasıl gezineceğinizi ve tam olarak ona nasıl geçeceğinizi bilmek önemlidir. Aspose.Words sizi bir belge içinde hareket etmenizi sağlar ve farklı bölümlerine ve parçalarına doğru gezdirir – bu Microsoft Word’deki Gezinti Paneli’nin işlevselliğine benzer, bir Word belgesinde sayfa veya başlığa doğru kaydırmadan gitmenizi sağlar.

Ana yöntem, imleç pozisyonunu belgenizdeki belirli bir düğüme taşıyabilme yeteneğidir, bunu yapmak için MoveTo yöntemini kullanabilirsiniz.

Aşağıdaki kod örneği bir belgedeki farklı düğümleri kullanarak DocumentBuilder‘i nasıl taşıyacağını gösterir:

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

Ama temel MoveTo yönteminin yanı sıra daha spesifik olanlar da var.

Bir Belgeye Başlangıç veya Sonunu Gezin

Belgenizin başına veya sonuna MoveToDocumentStart ve MoveToDocumentEnd yöntemlerini kullanarak gidebilirsiniz.

Aşağıdaki kod örneği, bir belgenin başlangıcına veya sonuna imleci nasıl taşıyacağınızı göstermektedir:

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

Kitap Çubuğu ile Gezin

Bulmak istediğiniz bir yeri işaretleyebilir ve kolayca oraya geri gidebilirsiniz. Belgeye istediğiniz kadar yer imi ekleyebilir, sonra benzersiz adlarla işaretleyerek onları gezinebilirsiniz. Bir yer imine MoveToBookmark yöntemiyle ulaşabilirsiniz.

Aşağıdaki kod örnekleri bir imlezin konumunu nasıl bir yer işaretine taşıdığını gösterir:

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

Tablo Hücrelerine Navigasyon Yapın

Bir tablo hücresine geçmek için MoveToCell yöntemini kullanabilirsiniz. Bu yöntem sizi belirli bir tablodaki herhangi bir hücreye yönlendirmek için imleci kullanmanıza olanak tanıyacaktır. Buna ek olarak, belirtilen bir dizine hareket etmek için imleci herhangi bir konuma veya belirtilen bir karaktere taşımak için MoveToCell yöntemini belirtebilirsiniz.

Aşağıdaki kod örneği bir imlecin belirtilen tablo hücresine hareketini nasıl gösterir:

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

Bir Alan’a Git

Belge içinde belirli bir alana geçmek için MoveToField yöntemini kullanabilirsiniz. Buna ek olarak, belirli bir birleştirme alanına girmek için MoveToMergeField yöntemini kullanabilirsiniz.

Aşağıdaki kod örneği, belge oluşturucu imlecinin nasıl belirli bir alana taşınacağını göstermektedir:

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

Bir Başlık veya Altbilgiye Geçin

Başlık veya alt bilgi hareket MoveToHeaderFooter yöntemini kullanarak bir başlık veya alt bilgiye geçebilirsiniz

Aşağıdaki kod örneği, belge oluşturucu imlecini bir belge başlığına veya alt bilgisine nasıl taşıyacağını göstermektedir:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
String dataDir = Utils.getDataDir(DocumentBuilderHeadersAndFooters.class);
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Specify that we want headers and footers different for first, even and odd pages.
builder.getPageSetup().setDifferentFirstPageHeaderFooter(true);
builder.getPageSetup().setOddAndEvenPagesHeaderFooter(true);
// Create the headers.
builder.moveToHeaderFooter(HeaderFooterType.HEADER_FIRST);
builder.write("Header for the first page");
builder.moveToHeaderFooter(HeaderFooterType.HEADER_EVEN);
builder.write("Header for even pages");
builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
builder.write("Header for all other pages");
// Create two pages in the document.
builder.moveToSection(0);
builder.writeln("Page1");
builder.insertBreak(BreakType.PAGE_BREAK);
builder.writeln("Page2");
doc.save(dataDir + "DocumentBuilder.HeadersAndFooters.docx");

Bir Bölüme veya Paragrafı Gezin

Belirli bir bölüme veya paragraflara geçebilirsiniz MoveToParagraph veya MoveToSection yöntemlerini kullanarak. Ayrıca, MoveToParagraph yöntemini kullanarak imleci belirli bir konuma veya paragraflardaki belirli bir karaktere taşımak için bir dizin belirtebilirsiniz.

Aşağıdaki kod örneğinde bir belgedeki belirli bir bölüme ve paragraflara nasıl geçileceğini gösterilmektedir:

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