ניווט עם הסמן
תוך כדי עבודה עם מסמך, גם אם הוא קצר או ארוך, יהיה עליך לנווט במסמך שלך. ניווט עם סמן וירטואלי מייצג את היכולת לנווט בין צמתים שונים במסמך.
בתוך מסמך קצר, התנועה במסמך היא פשוטה מכיוון שאתה יכול להזיז את נקודת ההכנסה גם באמצעות מקשי החצים של המקלדת או על ידי לחיצה על העכבר כדי לאתר את נקודת ההכנסה בכל מקום שתרצה. אבל ברגע שיש לך מסמך גדול שיש בו דפים רבים, הטכניקות הבסיסיות הללו לא יספיקו.
מאמר זה מסביר כיצד לנוע במסמך ולנווט עם סמן וירטואלי לחלקים שונים שלו.
זיהוי הנוכחי סמן עמדה
לפני שתתחיל בתהליך הניווט במסמך שלך, יהיה עליך לקבל את הצומת שנבחר כעת. אתה יכול לקבל את המיקום המדויק של הסמן בצומת שנבחר באמצעות המאפיין CurrentNode. בנוסף, במקום לקבל את הצומת הנוכחי, אתה יכול לקבל את הפסקה שנבחרה כעת או את הקטע שנבחר כעת באמצעות המאפיינים CurrentParagraph ו CurrentSection.
כל פעולות הוספה שתבצע באמצעות DocumentBuilder יוכנסו לפני CurrentNode. כאשר הפסקה הנוכחית ריקה או שהסמן ממוקם ממש לפני סוף הפסקה, CurrentNode מחזיר אפס.
שיטות ניווט במסמך
כאשר אתה עורך טקסט, חשוב לדעת כיצד לנווט במסמך שלך ולאן בדיוק לנוע בו. Aspose.Words מאפשר לך לנוע במסמך ולנווט לחלקים ולחלקים השונים שלו-זה דומה לפונקציונליות של חלונית הניווט ב Microsoft Word כדי לעבור לדף או כותרת במסמך Word מבלי לגלול.
השיטה העיקרית היא להיות מסוגל להעביר את מיקום הסמן לצומת ספציפי במסמך שלך, אתה יכול להשיג זאת באמצעות שיטת MoveTo.
דוגמת הקוד הבאה מראה כיצד להעביר את DocumentBuilder לצמתים שונים במסמך:
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()); |
אבל מלבד השיטה הבסיסית MoveTo, יש יותר ספציפיים.
נווט אל התחלה או סוף של מסמך
אתה יכול ללכת להתחלה או לסוף המסמך שלך באמצעות שיטות MoveToDocumentStart ו MoveToDocumentEnd.
דוגמת הקוד הבאה מראה כיצד להעביר את מיקום הסמן לתחילת או לסוף של מסמך:
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."); |
נווט עם סימניות
אתה יכול לסמן מקום שאתה רוצה למצוא ולעבור אליו שוב בקלות. אתה יכול להכניס כמה סימניות למסמך שלך ואז לנווט בהן על ידי זיהוי הסימניות עם שמות ייחודיים. ניתן לעבור לסימנייה באמצעות שיטת MoveToBookmark.
דוגמאות הקוד הבאות מראות כיצד להעביר מיקום סמן לסימניה:
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()); |
נווט אל תאי טבלה
ניתן לעבור לתא טבלה באמצעות שיטת MoveToCell. שיטה זו תאפשר לך לנווט את הסמן לכל תא בטבלה ספציפית. בנוסף, ניתן לציין אינדקס כדי להזיז את הסמן לכל מיקום או תו שצוין בתא בתוך השיטה MoveToCell.
דוגמת הקוד הבאה מראה כיצד להעביר מיקום סמן לתא טבלה שצוין:
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"); |
נווט אל שדה
ניתן לעבור לשדה מסוים במסמך באמצעות השיטה MoveToField. בנוסף, ניתן לעבור לשדה מיזוג ספציפי באמצעות שיטת MoveToMergeField.
דוגמת הקוד הבאה מראה כיצד להעביר את סמן בונה המסמכים לשדה ספציפי:
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."); |
נווט אל כותרת עליונה או תחתונה
ניתן לעבור לתחילת כותרת עליונה או תחתונה באמצעות שיטת MoveToHeaderFooter.
דוגמת הקוד הבאה מראה כיצד להעביר את סמן בונה המסמכים לכותרת עליונה או תחתונה של מסמך:
נווט אל קטע או פסקה
ניתן לעבור לקטע או פסקה ספציפיים באמצעות השיטות MoveToParagraph או MoveToSection. בנוסף, ניתן לציין אינדקס כדי להזיז את הסמן לכל מיקום או תו שצוין בפסקה בשיטה MoveToParagraph.
דוגמת הקוד הבאה מראה כיצד לעבור לקטע ספציפי ולפסקה ספציפית במסמך:
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); |