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

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

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

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).

ในทางกลับกันคุณยังสามารถใช้DocumentBuilderAPIเพื่อทำงานกับส่วนต่างๆได้ ในบทความนี้เราจะมุ่งเน้นไปที่วิธีนี้โดยเฉพาะอย่างยิ่งของการทำงานกับส่วน.

แทรกหรือลบตัวแบ่งส่วน

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");

ดูเพิ่มเติม