العمل مع الأقسام
في بعض الأحيان تريد مستندا لا يحتوي على نفس التنسيق عبر جميع الصفحات. على سبيل المثال، قد تحتاج إلى تعديل تنسيقات أرقام الصفحات، أو اختلاف حجم الصفحة واتجاهها، أو جعل صفحة المستند الأولى كصفحة غلاف بدون أي ترقيم. يمكنك تحقيق ذلك مع الأقسام.
الأقسام عبارة عن عقد مستوية تتحكم في الرؤوس والتذييلات والاتجاه والأعمدة والهوامش وتنسيق رقم الصفحة وغيرها.
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-Java.git. | |
/// <summary> | |
/// Insert section breaks before the specified paragraphs. | |
/// </summary> | |
private void insertSectionBreaks(ArrayList<Paragraph> topicStartParas) | |
{ | |
DocumentBuilder builder = new DocumentBuilder(mDoc); | |
for (Paragraph para : topicStartParas) | |
{ | |
Section section = para.getParentSection(); | |
// Insert section break if the paragraph is not at the beginning of a section already. | |
if (para != section.getBody().getFirstParagraph()) | |
{ | |
builder.moveTo(para.getFirstChild()); | |
builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE); | |
// 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.getBody().getLastParagraph().remove(); | |
} | |
} | |
} |
استخدم طريقة Remove لحذف فاصل مقطعي. إذا لم تكن بحاجة إلى إزالة فاصل قسم معين وحذف محتوى هذا القسم بدلا من ذلك، فيمكنك استخدام طريقة ClearContent.
يوضح مثال الكود التالي كيفية إزالة فواصل الأقسام:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git. | |
private void removeSectionBreaks(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.getSections().getCount() - 2; i >= 0; i--) | |
{ | |
// Copy the content of the current section to the beginning of the last section. | |
doc.getLastSection().prependContent(doc.getSections().get(i)); | |
// Remove the copied section. | |
doc.getSections().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-Java.git. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.write("Section 1"); | |
builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY); | |
builder.write("Primary header"); | |
builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY); | |
builder.write("Primary footer"); | |
Section section = doc.getFirstSection(); | |
// 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 (Node node : section) | |
{ | |
switch (node.getNodeType()) | |
{ | |
case NodeType.BODY: | |
{ | |
Body body = (Body)node; | |
System.out.println("Body:"); | |
System.out.println(MessageFormat.format("\t\"{0}\"", body.getText().trim())); | |
break; | |
} | |
case NodeType.HEADER_FOOTER: | |
{ | |
HeaderFooter headerFooter = (HeaderFooter)node; | |
System.out.println(MessageFormat.format("HeaderFooter type: {0}:", headerFooter.getHeaderFooterType())); | |
System.out.println(MessageFormat.format("\t\"{0}\"", headerFooter.getText().trim())); | |
break; | |
} | |
default: | |
{ | |
throw new Exception("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-Java.git. | |
Document doc = new Document(); | |
DocumentBuilder builder = new 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.getPageSetup().setLayoutMode(SectionLayoutMode.LINE_GRID); | |
builder.getPageSetup().setLinesPerPage(15); | |
builder.getParagraphFormat().setSnapToGrid(true); | |
for (int i = 0; i < 30; i++) | |
builder.write("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. "); | |
doc.save(getArtifactsDir() + "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-Java.git. | |
Document doc = new Document(); | |
// If we add a new section like this, it will not have a body, or any other child nodes. | |
doc.getSections().add(new Section(doc)); | |
// Run the "EnsureMinimum" method to add a body and a paragraph to this section to begin editing it. | |
doc.getLastSection().ensureMinimum(); | |
doc.getSections().get(0).getBody().getFirstParagraph().appendChild(new Run(doc, "Hello world!")); |
إلحاق المحتوى أو إلحاقه مسبقا
إذا كنت تريد رسم شكل ما أو إضافة نص أو صورة في بداية/نهاية القسم، فيمكنك استخدام طريقتي AppendContent و PrependContent لفئة Section.
يوضح مثال التعليمات البرمجية التالية كيفية إلحاق محتوى قسم موجود:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.write("Section 1"); | |
builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE); | |
builder.write("Section 2"); | |
builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE); | |
builder.write("Section 3"); | |
// This is the section that we will append and prepend to. | |
Section section = doc.getSections().get(2); | |
// Insert the contents of the first section to the beginning of the third section. | |
Section sectionToPrepend = doc.getSections().get(0); | |
section.prependContent(sectionToPrepend); | |
// Insert the contents of the second section to the end of the third section. | |
Section sectionToAppend = doc.getSections().get(1); | |
section.appendContent(sectionToAppend); |
استنساخ قسم
Aspose.Words يسمح لك بتكرار قسم عن طريق إنشاء نسخة كاملة منه باستخدام طريقة deepClone.
يوضح مثال الكود التالي كيفية استنساخ القسم الأول في المستند:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git. | |
Document doc = new Document(getMyDir() + "Document.docx"); | |
Section cloneSection = doc.getSections().get(0).deepClone(); |
نسخ الأقسام بين المستندات
في بعض الحالات، قد يكون لديك مستندات كبيرة بها العديد من الأقسام وتريد نسخ محتوى قسم من مستند إلى آخر.
Aspose.Words يسمح لك بنسخ الأقسام بين المستندات باستخدام طريقة ImportNode.
يوضح مثال الكود التالي كيفية نسخ الأقسام بين المستندات:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git. | |
Document srcDoc = new Document(getMyDir() + "Document.docx"); | |
Document dstDoc = new Document(); | |
Section sourceSection = srcDoc.getSections().get(0); | |
Section newSection = (Section) dstDoc.importNode(sourceSection, true); | |
dstDoc.getSections().add(newSection); | |
dstDoc.save(getArtifactsDir() + "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-Java.git. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.getPageSetup().setOrientation(Orientation.LANDSCAPE); | |
builder.getPageSetup().setLeftMargin(50.0); | |
builder.getPageSetup().setPaperSize(PaperSize.PAPER_10_X_14); | |
doc.save(getArtifactsDir() + "WorkingWithDocumentOptionsAndSettings.SetPageSetupAndSectionFormatting.docx"); |
يوضح مثال التعليمات البرمجية التالية كيفية تعديل خصائص الصفحة في جميع الأقسام:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.writeln("Section 1"); | |
doc.appendChild(new Section(doc)); | |
builder.writeln("Section 2"); | |
doc.appendChild(new Section(doc)); | |
builder.writeln("Section 3"); | |
doc.appendChild(new Section(doc)); | |
builder.writeln("Section 4"); | |
// 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 (Section section : doc.getSections()) | |
section.getPageSetup().setPaperSize(PaperSize.LETTER); | |
doc.save(getArtifactsDir() + "WorkingWithSection.ModifyPageSetupInAllSections.doc"); |