עבודה עם סעיפים

לפעמים אתה רוצה מסמך שאין לו אותו עיצוב בכל הדפים. לדוגמה, ייתכן שיהיה עליך לשנות פורמטים של מספר עמוד, להיות בעל גודל וכיוון עמוד שונים, או שיהיה לך את דף המסמך הראשון כעמוד שער ללא כל מספור. אתה יכול להשיג את זה עם קטעים.

חלקים הם צמתים ברמה השולטים בכותרות עליונות ותחתונות, כיוון, עמודות, שוליים, עיצוב מספר עמוד ואחרים.

Aspose.Words מאפשר לך לנהל קטעים, לחלק מסמך למקטעים ולבצע שינויים בעיצוב החלים רק על קטע ספציפי. Aspose.Words מאחסן מידע על עיצוב מקטעים כגון כותרות עליונות ותחתונות, הגדרת עמוד והגדרות עמודות במעבר המקטע.

מאמר זה מסביר כיצד לעבוד עם קטעים והפסקות קטעים.

מה קטע וקטע הפסקה

קטעי המסמך מיוצגים על ידי הכיתות Section ו - SectionCollection. אובייקטים של סעיף הם ילדים מיידיים של הצומת Document וניתן לגשת אליהם דרך המאפיין Sections. אתה יכול לנהל את הצמתים האלה באמצעות כמה שיטות כגון Remove, Add, IndexOf, ואחרים.

מעבר מקטע הוא אפשרות המחלקת דפי מסמכים למקטעים עם פריסות הניתנות להתאמה אישית.

סוגי מעבר מקטע

Aspose.Words מאפשר לך לפצל ולעצב מסמכים באמצעות מעברי קטעים שונים של הספירה BreakType:

  • SectionBreakContinuous
  • SectionBreakNewColumn
  • SectionBreakNewPage
  • SectionBreakEvenPage
  • SectionBreakOddPage

אתה יכול גם להשתמש בספירה SectionStart כדי לבחור סוג הפסקה שחל רק על החלק הראשון כגון NewColumn, NewPage, EvenPage, ו - OddPage.

ניהול קטע

מאחר שחלק הוא צומת מורכב רגיל, ניתן להשתמש בכל מניפולציה של הצומת API כדי לתמרן חלקים: להוסיף, להסיר ופעולות אחרות בחלקים. אתה יכול לקרוא עוד על צמתים במאמר Aspose.Words מודל אובייקט מסמך (DOM).

מצד שני, אתה יכול גם להשתמש ב DocumentBuilder API לעבוד עם קטעים. במאמר זה, נתמקד בדרך מסוימת זו של עבודה עם חלקים.

הוספה או הסרה של מעבר מקטע

Aspose.Words מאפשר לך להוסיף קטע מעבר לטקסט בשיטת InsertBreak.

דוגמת הקוד הבאה מראה כיצד להוסיף פריצת מקטע למסמך:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
void WordToHtmlConverter::InsertSectionBreaks(System::SharedPtr<System::Collections::Generic::List<System::SharedPtr<Paragraph>>> topicStartParas)
{
auto builder = System::MakeObject<DocumentBuilder>(mDoc);
for (const auto& para : topicStartParas)
{
System::SharedPtr<Section> section = para->get_ParentSection();
// Insert section break if the paragraph is not at the beginning of a section already.
if (para != section->get_Body()->get_FirstParagraph())
{
builder->MoveTo(para->get_FirstChild());
builder->InsertBreak(BreakType::SectionBreakNewPage);
// This is the paragraph that was inserted at the end of the now old section.
// We don't really need the extra paragraph, we just needed the section.
section->get_Body()->get_LastParagraph()->Remove();
}
}
}

השתמש בשיטת Remove כדי למחוק מעבר מקטע. אם אינך צריך להסיר מעבר מקטע ספציפי ובמקום זאת למחוק את התוכן של אותו קטע, תוכל להשתמש בשיטת ClearContent.

דוגמת הקוד הבאה מראה כיצד להסיר מעברי קטעים:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
void RemoveSectionBreaks(SharedPtr<Document> doc)
{
// Loop through all sections starting from the section that precedes the last one and moving to the first section.
for (int i = doc->get_Sections()->get_Count() - 2; i >= 0; i--)
{
// Copy the content of the current section to the beginning of the last section.
doc->get_LastSection()->PrependContent(doc->get_Sections()->idx_get(i));
// Remove the copied section.
doc->get_Sections()->idx_get(i)->Remove();
}
}

העבר קטע

אם ברצונך להעביר קטע ממיקום אחד למשנהו במסמך שלך, עליך לקבל את האינדקס של אותו קטע. Aspose.Words מאפשר לך לקבל מיקום מקטע מ SectionCollection. אתה יכול להשתמש במאפיין Sections כדי לקבל את כל הסעיפים במסמך שלך. אבל אם אתה רוצה לקבל רק את החלק הראשון, אתה יכול להשתמש במאפיין FirstSection.

דוגמת הקוד הבאה מראה כיצד לגשת לחלק הראשון ולחזור דרך הילדים של צומת מורכב:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->Write(u"Section 1");
builder->MoveToHeaderFooter(HeaderFooterType::HeaderPrimary);
builder->Write(u"Primary header");
builder->MoveToHeaderFooter(HeaderFooterType::FooterPrimary);
builder->Write(u"Primary footer");
// A Section is a composite node and can contain child nodes,
// but only if those child nodes are of a "Body" or "HeaderFooter" node type.
for (const auto& srcSection : System::IterateOver(doc->get_Sections()->LINQ_OfType<SharedPtr<Section>>()))
{
for (const auto& srcNode : System::IterateOver(srcSection->get_Body()))
{
switch (srcNode->get_NodeType())
{
case NodeType::Body:
{
auto body = System::ExplicitCast<Body>(srcNode);
std::cout << "Body:" << std::endl;
std::cout << std::endl;
std::cout << "\t\"" << body->GetText().Trim() << "\"" << std::endl;
break;
}
case NodeType::HeaderFooter:
{
auto headerFooter = System::ExplicitCast<HeaderFooter>(srcNode);
auto headerFooterType = headerFooter->get_HeaderFooterType();
std::cout << "HeaderFooter type: " << static_cast<std::underlying_type<HeaderFooterType>::type>(headerFooterType) << std::endl;
std::cout << "\t\"" << headerFooter->GetText().Trim() << "\"" << std::endl;
break;
}
default:
{
throw System::Exception(u"Unexpected node type in a section.");
}
}
}
}

ציין פריסת מקטע

לפעמים אתה רוצה שהמסמך שלך ייראה טוב יותר על ידי יצירת פריסות יצירתיות עבור קטעי מסמכים שונים. אם ברצונך לציין את סוג רשת המקטעים הנוכחית, תוכל לבחור מצב פריסת מקטעים באמצעות ספירת SectionLayoutMode:

  • ברירת מחדל
  • רשת
  • LineGrid
  • SnapToChars

דוגמת הקוד הבאה מראה כיצד להגביל את מספר השורות שעשויות להיות בכל עמוד:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
// Enable pitching, and then use it to set the number of lines per page in this section.
// A large enough font size will push some lines down onto the next page to avoid overlapping characters.
builder->get_PageSetup()->set_LayoutMode(SectionLayoutMode::LineGrid);
builder->get_PageSetup()->set_LinesPerPage(15);
builder->get_ParagraphFormat()->set_SnapToGrid(true);
for (int i = 0; i < 30; i++)
builder->Write(u"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ");
doc->Save(ArtifactsDir + u"WorkingWithDocumentOptionsAndSettings.LinesPerPage.docx");

עריכת קטע

כשאתה מוסיף קטע חדש למסמך שלך, לא יהיה גוף או פסקה שתוכל לערוך. Aspose.Words מאפשר לך להבטיח שקטע מכיל גוף עם פיסקה אחת לפחות בשיטת EnsureMinimum - הוא יוסיף אוטומטית צומת גוף (או HeaderFooter) למסמך ואז יוסיף לו פסקה.

דוגמת הקוד הבאה מראה כיצד להכין צומת קטע חדש באמצעות EnsureMinimum:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
auto doc = MakeObject<Document>();
// If we add a new section like this, it will not have a body, or any other child nodes.
doc->get_Sections()->Add(MakeObject<Section>(doc));
// Run the "EnsureMinimum" method to add a body and a paragraph to this section to begin editing it.
doc->get_LastSection()->EnsureMinimum();
doc->get_Sections()->idx_get(0)->get_Body()->get_FirstParagraph()->AppendChild(MakeObject<Run>(doc, u"Hello world!"));

הוסף או הוסף תוכן מראש

אם ברצונך לצייר צורה כלשהי או להוסיף טקסט או תמונה בתחילת/סוף קטע, תוכל להשתמש בשיטות AppendContent ו - PrependContent של הכיתה Section.

דוגמת הקוד הבאה מראה כיצד להוסיף תוכן של קטע קיים:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->Writeln(u"Hello1");
doc->AppendChild(MakeObject<Section>(doc));
builder->Writeln(u"Hello22");
doc->AppendChild(MakeObject<Section>(doc));
builder->Writeln(u"Hello3");
doc->AppendChild(MakeObject<Section>(doc));
builder->Writeln(u"Hello45");
// This is the section that we will append and prepend to.
SharedPtr<Section> section = doc->get_Sections()->idx_get(2);
// This copies the content of the 1st section and inserts it at the beginning of the specified section.
SharedPtr<Section> sectionToPrepend = doc->get_Sections()->idx_get(0);
section->PrependContent(sectionToPrepend);
// This copies the content of the 2nd section and inserts it at the end of the specified section.
SharedPtr<Section> sectionToAppend = doc->get_Sections()->idx_get(1);
section->AppendContent(sectionToAppend);

שיבוט קטע

Aspose.Words מאפשר לך לשכפל קטע על ידי יצירת עותק מלא שלו בשיטת Clone.

דוגמת הקוד הבאה מראה כיצד לשכפל את החלק הראשון במסמך שלך:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
auto doc = MakeObject<Document>(MyDir + u"Document.docx");
SharedPtr<Section> cloneSection = doc->get_Sections()->idx_get(0)->Clone();
view raw clone-section.h hosted with ❤ by GitHub

העתק קטעים בין מסמכים

במקרים מסוימים, ייתכן שיש לך מסמכים גדולים עם חלקים רבים ואתה רוצה להעתיק את התוכן של קטע ממסמך אחד למשנהו.

Aspose.Words מאפשר לך להעתיק קטעים בין מסמכים בשיטת ImportNode.

דוגמת הקוד הבאה מראה כיצד להעתיק קטעים בין מסמכים:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
auto srcDoc = MakeObject<Document>(MyDir + u"Document.docx");
auto dstDoc = MakeObject<Document>();
SharedPtr<Section> sourceSection = srcDoc->get_Sections()->idx_get(0);
auto newSection = System::ExplicitCast<Section>(dstDoc->ImportNode(sourceSection, true));
dstDoc->get_Sections()->Add(newSection);
dstDoc->Save(ArtifactsDir + u"WorkingWithSection.CopySection.docx");
view raw copy-section.h hosted with ❤ by GitHub

עבודה עם כותרת עליונה ותחתונה

הכללים הבסיסיים להצגת כותרת עליונה או תחתונה עבור כל קטע הם פשוטים למדי:

  1. אם לקטע אין כותרות עליונות / תחתונות משלו מסוג מסוים, הוא נלקח מהקטע הקודם.
  2. סוג הכותרת העליונה / התחתונה המוצגת בדף נשלט על ידי הגדרות הקטע" עמוד ראשון שונה “ו” דפים מוזרים ואחידים שונים " – אם הם מושבתים, מתעלמים מהכותרות של הקטע עצמו.

דוגמת הקוד הבאה מראה כיצד ליצור 2 קטעים עם כותרות שונות:

אם ברצונך להסיר את הטקסט של כותרות עליונות ותחתונות מבלי להסיר HeaderFooter אובייקטים במסמך, תוכל להשתמש בשיטה ClearHeadersFooters. בנוסף, תוכל להשתמש בשיטת DeleteHeaderFooterShapes כדי להסיר את כל הצורות מכותרות עליונות ותחתונות במסמך שלך.

דוגמת הקוד הבאה מראה כיצד לנקות תוכן מכל הכותרות העליונות והתחתונות בקטע:

דוגמת הקוד הבאה כיצד להסיר את כל הצורות מכל הכותרות התחתונות בסעיף:

התאמה אישית של מאפייני דף במקטע

לפני הדפסת דף או מסמך ייתכן שתרצה להתאים אישית ולשנות את הגודל והפריסה של דף בודד או של המסמך כולו. עם הגדרת הדף, באפשרותך לשנות את ההגדרות של דפי מסמכים כגון שוליים, כיוון וגודל להדפסת דפים ראשונים שונים או דפים מוזרים.

Aspose.Words מאפשר לך להתאים אישית את מאפייני הדף והקטע באמצעות הכיתה PageSetup.

דוגמת הקוד הבאה מראה כיצד להגדיר מאפיינים כגון גודל עמוד וכיוון עבור החלק הנוכחי:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->get_PageSetup()->set_Orientation(Orientation::Landscape);
builder->get_PageSetup()->set_LeftMargin(50);
builder->get_PageSetup()->set_PaperSize(PaperSize::Paper10x14);
doc->Save(ArtifactsDir + u"WorkingWithDocumentOptionsAndSettings.PageSetupAndSectionFormatting.docx");

דוגמת הקוד הבאה מראה כיצד לשנות את מאפייני הדף בכל הסעיפים:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->Writeln(u"Hello1");
doc->AppendChild(MakeObject<Section>(doc));
builder->Writeln(u"Hello22");
doc->AppendChild(MakeObject<Section>(doc));
builder->Writeln(u"Hello3");
doc->AppendChild(MakeObject<Section>(doc));
builder->Writeln(u"Hello45");
// It is important to understand that a document can contain many sections,
// and each section has its page setup. In this case, we want to modify them all.
for (const auto& section : System::IterateOver<Section>(doc))
{
section->get_PageSetup()->set_PaperSize(PaperSize::Letter);
}
doc->Save(ArtifactsDir + u"WorkingWithSection.ModifyPageSetupInAllSections.doc");

ראה גם