العمل مع الأقسام
في بعض الأحيان تريد مستندا لا يحتوي على نفس التنسيق عبر جميع الصفحات. على سبيل المثال، قد تحتاج إلى تعديل تنسيقات أرقام الصفحات، أو اختلاف حجم الصفحة واتجاهها، أو جعل صفحة المستند الأولى كصفحة غلاف بدون أي ترقيم. يمكنك تحقيق ذلك مع الأقسام.
الأقسام عبارة عن عقد مستوية تتحكم في الرؤوس والتذييلات والاتجاه والأعمدة والهوامش وتنسيق رقم الصفحة وغيرها.
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(); |
نسخ الأقسام بين المستندات
في بعض الحالات، قد يكون لديك مستندات كبيرة بها العديد من الأقسام وتريد نسخ محتوى قسم من مستند إلى آخر.
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"); |
العمل مع رأس القسم وتذييل الصفحة
القواعد الأساسية لعرض رأس أو تذييل لكل قسم بسيطة للغاية:
- إذا لم يكن للقسم رؤوس / تذييلات خاصة به من نوع معين، فسيتم أخذه من القسم السابق.
- يتم التحكم في نوع الرأس/التذييل المعروض على الصفحة من خلال إعدادات قسم “الصفحة الأولى المختلفة” و “الصفحات الفردية والزوجية المختلفة” – إذا تم تعطيلها، فسيتم تجاهل عناوين القسم الخاصة.
يوضح مثال الكود التالي كيفية إنشاء أقسام 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"); |