کار با بخش ها

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

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

Aspose.Words به شما امکان می‌دهد بخش‌ها را مدیریت کنید، یک سند را به بخش‌ها تقسیم کنید و تغییرات قالب‌بندی را انجام دهید که فقط برای یک بخش خاص اعمال می‌شود. Aspose.Words اطلاعات مربوط به قالب بندی بخش مانند سرصفحه ها و پاورقی ها، تنظیمات صفحه و تنظیمات ستون را در بخش شکست ذخیره می کند.

در این مقاله نحوه کار با بخش ها و بخش ها توضیح داده شده است.

Section و Section Break چیست؟

بخش های سند توسط کلاس های Section و SectionCollection نشان داده می شوند. اشیاء بخش فرزندان مستقیم گره Document هستند و از طریق ویژگی Sections قابل دسترسی هستند. شما می توانید آن گره ها را با استفاده از روش هایی مانند Remove، Add، IndexOf و غیره مدیریت کنید.

Section break گزینه ای است که صفحات سند را به بخش هایی با طرح بندی قابل تنظیم تقسیم می کند.

انواع شکستن بخش

Aspose.Words به شما امکان می دهد اسناد را با استفاده از بخش های مختلف شمارش BreakType تقسیم و قالب بندی کنید:

  • بخش Break Continuous
  • بخش BreakNewColumn
  • بخش BreakNewPage
  • SectionBreakEvenPage
  • بخش BreakOddPage

همچنین می‌توانید از شمارش SectionStart برای انتخاب نوع شکست استفاده کنید که فقط برای بخش اول مانند NewColumn، NewPage، EvenPage و OddPage اعمال می‌شود.

یک بخش را مدیریت کنید

از آنجایی که یک بخش یک گره مرکب معمولی است، کل دستکاری گره API را می توان برای دستکاری بخش ها استفاده کرد: افزودن، حذف و سایر عملیات بر روی بخش ها. در مقاله Aspose.Words Document Object Model (DOM) می توانید اطلاعات بیشتری در مورد گره ها بخوانید.

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

درج یا حذف بخش شکست

Aspose.Words به شما این امکان را می دهد که با استفاده از روش InsertBreak یک بخش شکست را در متن وارد کنید.

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
/// <summary>
/// Insert section breaks before the specified paragraphs.
/// </summary>
private void InsertSectionBreaks(List<Paragraph> topicStartParas)
{
DocumentBuilder builder = new DocumentBuilder(mDoc);
foreach (Paragraph para in topicStartParas)
{
Section section = para.ParentSection;
// Insert section break if the paragraph is not at the beginning of a section already.
if (para != section.Body.FirstParagraph)
{
builder.MoveTo(para.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.Body.LastParagraph.Remove();
}
}
}

از روش Remove برای حذف یک بخش استراحت استفاده کنید. اگر نیازی به حذف یک بخش استراحت خاص و حذف محتوای آن بخش ندارید، می توانید از روش ClearContent استفاده کنید.

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.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.Sections.Count - 2; i >= 0; i--)
{
// Copy the content of the current section to the beginning of the last section.
doc.LastSection.PrependContent(doc.Sections[i]);
// Remove the copied section.
doc.Sections[i].Remove();
}
}

یک بخش را جابجا کنید

اگر می خواهید بخشی را از یک موقعیت به موقعیت دیگر در سند خود منتقل کنید، باید شاخص آن بخش را دریافت کنید. Aspose.Words به شما امکان می دهد با استفاده از ویژگی Item یک موقعیت بخش را از SectionCollection دریافت کنید. می توانید از ویژگی Sections برای دریافت تمام بخش ها در سند خود استفاده کنید. اما اگر می خواهید فقط قسمت اول را دریافت کنید، می توانید از ویژگی FirstSection استفاده کنید.

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Write("Section 1");
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
builder.Write("Primary header");
builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
builder.Write("Primary footer");
Section section = doc.FirstSection;
// 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.
foreach (Node node in section)
{
switch (node.NodeType)
{
case NodeType.Body:
{
Body body = (Body)node;
Console.WriteLine("Body:");
Console.WriteLine($"\t\"{body.GetText().Trim()}\"");
break;
}
case NodeType.HeaderFooter:
{
HeaderFooter headerFooter = (HeaderFooter)node;
Console.WriteLine($"HeaderFooter type: {headerFooter.HeaderFooterType}:");
Console.WriteLine($"\t\"{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-.NET.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.PageSetup.LayoutMode = SectionLayoutMode.LineGrid;
builder.PageSetup.LinesPerPage = 15;
builder.ParagraphFormat.SnapToGrid = 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(ArtifactsDir + "WorkingWithDocumentOptionsAndSettings.LinesPerPage.docx");

یک بخش را ویرایش کنید

وقتی بخش جدیدی را به سند خود اضافه می کنید، بدنه یا پاراگرافی وجود نخواهد داشت که بتوانید آن را ویرایش کنید. Aspose.Words به شما این امکان را می دهد که با استفاده از روش EnsureMinimum تضمین کنید که یک بخش دارای بدنه ای با حداقل یک پاراگراف است - به طور خودکار یک گره Body (یا HeaderFooter) را به سند اضافه می کند و سپس یک پاراگراف را به آن اضافه می کند.

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.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.Sections.Add(new Section(doc));
// Run the "EnsureMinimum" method to add a body and a paragraph to this section to begin editing it.
doc.LastSection.EnsureMinimum();
doc.Sections[0].Body.FirstParagraph.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-.NET.git.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Write("Section 1");
builder.InsertBreak(BreakType.SectionBreakNewPage);
builder.Write("Section 2");
builder.InsertBreak(BreakType.SectionBreakNewPage);
builder.Write("Section 3");
Section section = doc.Sections[2];
// Insert the contents of the first section to the beginning of the third section.
Section sectionToPrepend = doc.Sections[0];
section.PrependContent(sectionToPrepend);
// Insert the contents of the second section to the end of the third section.
Section sectionToAppend = doc.Sections[1];
section.AppendContent(sectionToAppend);

یک بخش را شبیه سازی کنید

Aspose.Words به شما این امکان را می دهد که با ایجاد یک کپی کامل از آن با استفاده از روش Clone، یک بخش را کپی کنید.

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document doc = new Document(MyDir + "Document.docx");
Section cloneSection = doc.Sections[0].Clone();

بخش ها را بین اسناد کپی کنید

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

Aspose.Words به شما امکان می دهد با استفاده از روش ImportNode بخش هایی را بین اسناد کپی کنید.

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document srcDoc = new Document(MyDir + "Document.docx");
Document dstDoc = new Document();
Section sourceSection = srcDoc.Sections[0];
Section newSection = (Section)dstDoc.ImportNode(sourceSection, true);
dstDoc.Sections.Add(newSection);
dstDoc.Save(ArtifactsDir + "WorkingWithSection.CopySection.docx");
view raw copy-section.cs 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-.NET.git.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.PageSetup.Orientation = Orientation.Landscape;
builder.PageSetup.LeftMargin = 50;
builder.PageSetup.PaperSize = PaperSize.Paper10x14;
doc.Save(ArtifactsDir + "WorkingWithDocumentOptionsAndSettings.PageSetupAndSectionFormatting.docx");

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.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.
foreach (Section section in doc)
section.PageSetup.PaperSize = PaperSize.Letter;
doc.Save(ArtifactsDir + "WorkingWithSection.ModifyPageSetupInAllSections.doc");

همچنین ببینید