ناوبری با مکان نما
هنگام کار با یک سند، حتی اگر کوتاه یا طولانی باشد، باید از طریق سند خود حرکت کنید. ناوبری با یک مکان نما مجازی نشان دهنده توانایی حرکت بین گره های مختلف در یک سند است.
در یک سند کوتاه، حرکت در یک سند ساده است زیرا شما می توانید نقطه ورودی را حتی با استفاده از کلید های تیر صفحه کلید یا با کلیک بر روی ماوس برای پیدا کردن نقطه ورودی در هر کجا که می خواهید حرکت دهید. اما هنگامی که یک سند بزرگ دارید که صفحات زیادی دارد، این تکنیک های اساسی کافی نخواهد بود.
این مقاله توضیح می دهد که چگونه در یک سند حرکت کنید و با یک مکان نما مجازی به بخش های مختلف آن حرکت کنید.
تشخیص موقعیت مکان نما فعلی
قبل از شروع روند پیمایش از طریق سند خود، باید گره ای را که در حال حاضر انتخاب شده است دریافت کنید. شما می توانید موقعیت دقیق نشانگر را در یک گره انتخاب شده با استفاده از ویژگی CurrentNode بدست آورید. علاوه بر این، به جای گرفتن گره فعلی، می توانید پاراگراف انتخاب شده یا بخش انتخاب شده را با استفاده از ویژگی های CurrentParagraph و CurrentSection دریافت کنید.
هر عملیاتی که با استفاده از DocumentBuilder انجام می دهید قبل از CurrentNode وارد می شود. وقتی پاراگراف فعلی خالی است یا نشانگر درست قبل از پایان پاراگراف قرار گرفته است، CurrentNode null را باز می گرداند.
روش های پیمایش در یک سند
هنگامی که متن را ویرایش می کنید، مهم است که بدانید چگونه سند خود را هدایت کنید و دقیقا کجا در آن حرکت کنید. Aspose.Words به شما اجازه می دهد تا در یک سند حرکت کنید و به بخش ها و بخش های مختلف آن بروید – این شبیه به عملکرد صفحه ناوبری در Microsoft Word برای رفتن به یک صفحه یا عنوان در یک سند ورد بدون حرکت است.
روش اصلی این است که بتوانید موقعیت مکان نما را به یک گره خاص در سند خود منتقل کنید، شما می توانید این کار را با استفاده از روش MoveTo انجام دهید.
مثال کد زیر نشان می دهد که چگونه DocumentBuilder را به گره های مختلف در یک سند منتقل کنیم:
// 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()); |
اما علاوه بر روش اساسی MoveTo، روش های خاص تری نیز وجود دارد.
به سمت شروع یا پایان یک سند حرکت کنید
شما می توانید با استفاده از روش های MoveToDocumentStart و MoveToDocumentEnd به ابتدای یا پایان سند خود بروید.
مثال کد زیر نشان می دهد که چگونه موقعیت مکان نما را به ابتدا یا پایان یک سند منتقل کنیم:
// 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."); |
با نشانک ها حرکت کنید
می توانید مکانی را که می خواهید پیدا کنید علامت گذاری کنید و به راحتی دوباره به آن بروید. شما می توانید به عنوان بسیاری از نشانه ها را به سند خود را به عنوان شما می خواهید وارد کنید، و سپس از طریق آنها با شناسایی نشانه ها با نام های منحصر به فرد حرکت کنید. شما می توانید با استفاده از روش MoveToBookmark به یک نشانه حرکت کنید.
نمونه های کد زیر نشان می دهد که چگونه یک موقعیت نشانگر را به یک نشانه گذاری منتقل کنیم:
// 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()); |
به سلول های جدول بروید
شما می توانید با استفاده از روش MoveToCell به یک سلول جدول حرکت کنید. این روش شما را قادر می سازد تا نشانگر خود را به هر سلول در یک جدول خاص هدایت کنید. علاوه بر این، شما می توانید یک شاخص را برای حرکت نشانگر به هر موقعیت یا کاراکتر مشخص شده در یک سلول در روش MoveToCell مشخص کنید.
مثال کد زیر نشان می دهد که چگونه یک موقعیت نشانگر را به یک سلول جدول مشخص منتقل کنیم:
// 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()); |
به یک فیلد بروید
شما می توانید با استفاده از روش MoveToField به یک فیلد خاص در سند خود بروید. علاوه بر این، شما می توانید با استفاده از روش MoveToMergeField به یک فیلد ادغام خاص بروید.
مثال کد زیر نشان می دهد که چگونه نشانگر سازنده سند را به یک فیلد خاص منتقل کنیم:
// 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."); |
به یک سر یا پای صفحه بروید
با استفاده از روش MoveToHeaderFooter می توانید به ابتدای هدر یا پاورقی بروید.
مثال کد زیر نشان می دهد که چگونه نشانگر سازنده سند را به یک سرصفحه یا پای صفحه سند منتقل کنیم:
به بخش یا پاراگراف بروید
شما می توانید با استفاده از روش های MoveToParagraph یا MoveToSection به یک بخش یا پاراگراف خاص بروید. علاوه بر این، شما می توانید یک شاخص را برای حرکت نشانگر به هر موقعیت یا یک کاراکتر مشخص شده در یک پاراگراف در روش MoveToParagraph مشخص کنید.
مثال کد زیر نشان می دهد که چگونه به یک بخش خاص و یک پاراگراف خاص در یک سند حرکت کنید:
// 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())); |