การทำงานกับส่วนต่างๆ

บางครั้งคุณต้องการเอกสารที่ไม่มีการจัดรูปแบบเหมือนกันในทุกหน้า ตัวอย่างเช่น คุณอาจต้องแก้ไขรูปแบบหมายเลขหน้า มีขนาดหน้าและการวางแนวที่แตกต่างกัน หรือมีเอกสารหน้าแรกเป็นหน้าปกโดยไม่มีหมายเลขใดๆ คุณสามารถบรรลุเป้าหมายนั้นได้ด้วยส่วนต่างๆ

ส่วนต่างๆ คือโหนดระดับที่ควบคุมหัวกระดาษและท้ายกระดาษ การวางแนว คอลัมน์ ระยะขอบ การจัดรูปแบบหมายเลขหน้า และอื่นๆ

Aspose.Words ช่วยให้คุณจัดการส่วนต่างๆ แบ่งเอกสารออกเป็นส่วนๆ และทำการเปลี่ยนแปลงการจัดรูปแบบที่ใช้กับส่วนใดส่วนหนึ่งเท่านั้น Aspose.Words จัดเก็บข้อมูลเกี่ยวกับการจัดรูปแบบส่วนต่างๆ เช่น ส่วนหัวและส่วนท้าย การตั้งค่าหน้า และการตั้งค่าคอลัมน์ในตัวแบ่งส่วน

บทความนี้จะอธิบายวิธีการทำงานกับส่วนและตัวแบ่งส่วน

ตัวแบ่งส่วนและส่วนคืออะไร

ส่วนเอกสารจะแสดงด้วยคลาส Section และ SectionCollection ออบเจ็กต์ส่วนเป็นรายการย่อยของโหนด Document และสามารถเข้าถึงได้ผ่านคุณสมบัติ Sections คุณสามารถจัดการโหนดเหล่านั้นได้โดยใช้วิธีการบางอย่าง เช่น Remove, Add, IndexOf และอื่นๆ

ตัวแบ่งส่วนเป็นตัวเลือกที่แบ่งหน้าเอกสารออกเป็นส่วนต่างๆ ด้วยเค้าโครงที่ปรับแต่งได้

ประเภทของตัวแบ่งส่วน

Aspose.Words ช่วยให้คุณสามารถแยกและจัดรูปแบบเอกสารโดยใช้ตัวแบ่งส่วนต่างๆ ของการแจงนับ BreakType:

  • SectionBreak ต่อเนื่อง
  • SectionBreakNewColumn
  • SectionBreakNewPage
  • SectionBreakEvenPage
  • SectionBreakOddPage

คุณยังสามารถใช้การแจงนับ 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 ช่วยให้คุณได้รับตำแหน่งส่วนจาก SectionCollection โดยใช้คุณสมบัติ Item คุณสามารถใช้คุณสมบัติ 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:

  • ค่าเริ่มต้น
  • กริด *ไลน์กริด
  • 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");

ดูสิ่งนี้ด้วย