섹션 작업

때로는 모든 페이지에 동일한 서식이 없는 문서를 원할 수도 있습니다. 예를 들어 페이지 번호 형식을 수정하거나 페이지 크기 및 방향이 다르거나 첫 번째 문서 페이지를 번호 매기기 없이 표지로 사용해야 할 수 있습니다. 당신은 섹션으로 그것을 달성 할 수 있습니다.

섹션은 머리글 및 바닥글,방향,열,여백,페이지 번호 서식 등을 제어하는 수준 노드입니다.

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

또한 참조