ناوبری با مکان نما

هنگام کار با یک سند، حتی اگر کوتاه یا طولانی باشد، باید در سند خود پیمایش کنید. ناوبری با مکان نما مجازی نشان دهنده توانایی حرکت بین گره های مختلف در یک سند است.

در یک سند کوتاه، حرکت در یک سند ساده است زیرا می‌توانید نقطه درج را حتی با استفاده از کلیدهای جهت‌نمای صفحه‌کلید یا با کلیک کردن روی ماوس برای قرار دادن نقطه درج در هر کجا که می‌خواهید، جابه‌جا کنید. اما هنگامی که یک سند بزرگ دارید که صفحات زیادی دارد، این تکنیک های اساسی ناکافی خواهند بود.

این مقاله نحوه حرکت در یک سند و حرکت با مکان نما مجازی به قسمت های مختلف آن را توضیح می دهد.

تشخیص موقعیت مکان نما فعلی

قبل از شروع فرآیند پیمایش در سند خود، باید گره ای را که در حال حاضر انتخاب شده است دریافت کنید. با استفاده از ویژگی CurrentNode می توانید موقعیت دقیق مکان نما را در یک گره انتخاب شده بدست آورید. علاوه بر این، به جای دریافت گره فعلی، می توانید پاراگراف انتخاب شده فعلی یا بخش انتخاب شده فعلی را با استفاده از ویژگی های CurrentParagraph و CurrentSection دریافت کنید.

هر عملیات درج که با استفاده از DocumentBuilder انجام می دهید قبل از CurrentNode درج می شود. هنگامی که پاراگراف فعلی خالی است یا مکان نما درست قبل از پایان پاراگراف قرار می گیرد، CurrentNode null را برمی گرداند.

روش‌های پیمایش در یک سند

هنگامی که متن را ویرایش می کنید، مهم است که بدانید چگونه سند خود را پیمایش کنید و دقیقاً کجا در آن حرکت کنید. Aspose.Words به شما امکان می‌دهد در یک سند حرکت کنید و به بخش‌ها و بخش‌های مختلف آن پیمایش کنید - این شبیه به عملکرد صفحه ناوبری در Microsoft Word برای رفتن به یک صفحه یا عنوان در یک سند Word بدون پیمایش است.

روش اصلی این است که بتوانید موقعیت مکان نما را به یک گره خاص در سند خود منتقل کنید، می توانید با استفاده از روش MoveTo به این هدف برسید.

مثال کد زیر نحوه انتقال DocumentBuilder را به گره های مختلف در یک سند نشان می دهد:

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

اما در کنار روش اصلی MoveTo، روش های خاص تری نیز وجود دارد.

به ابتدا یا انتهای یک سند بروید

می توانید با استفاده از روش های MoveToDocumentStart و MoveToDocumentEnd به ابتدا یا انتهای سند خود بروید.

مثال کد زیر نحوه انتقال مکان نما به ابتدا یا انتهای یک سند را نشان می دهد:

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

با نشانک‌ها پیمایش کنید

می‌توانید مکانی را که می‌خواهید پیدا کنید علامت‌گذاری کنید و دوباره به آن جا بروید. می‌توانید هر تعداد نشانک را که می‌خواهید در سند خود وارد کنید، و سپس با شناسایی نشانک‌هایی با نام‌های منحصربه‌فرد، در میان آنها پیمایش کنید. با استفاده از روش MoveToBookmark می توانید به نشانک منتقل شوید.

مثال های کد زیر نحوه انتقال مکان نما به نشانک را نشان می دهد:

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

به سلول های جدول بروید

با استفاده از روش MoveToCell می توانید به سلول جدول بروید. این روش به شما امکان می دهد مکان نما خود را در هر سلولی در یک جدول خاص هدایت کنید. علاوه بر این، می توانید یک شاخص برای حرکت مکان نما به هر موقعیت یا کاراکتر مشخص شده در یک سلول در روش MoveToCell تعیین کنید.

مثال کد زیر نحوه انتقال مکان نما به سلول جدول مشخص را نشان می دهد:

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

به یک فیلد بروید

با استفاده از روش MoveToField می توانید به یک فیلد خاص در سند خود بروید. علاوه بر این، می توانید با استفاده از روش MoveToMergeField به یک فیلد ادغام خاص بروید.

مثال کد زیر نحوه انتقال مکان‌نمای سازنده سند را به یک فیلد خاص نشان می‌دهد:

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

به سربرگ یا پاورقی بروید

با استفاده از روش MoveToHeaderFooter می توانید به ابتدای سرصفحه یا پاورقی بروید

مثال کد زیر نحوه انتقال مکان‌نمای سازنده سند به سرصفحه یا پاورقی سند را نشان می‌دهد:

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

به بخش یا پاراگراف بروید

با استفاده از روش‌های MoveToParagraph یا MoveToSection می‌توانید به بخش یا پاراگراف خاصی بروید. علاوه بر این، می توانید یک شاخص برای حرکت مکان نما به هر موقعیت یا یک کاراکتر مشخص در یک پاراگراف در روش MoveToParagraph تعیین کنید.

مثال کد زیر نحوه انتقال به یک بخش خاص و یک پاراگراف خاص در یک سند را نشان می دهد:

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