セクションの操作
場合によっては、すべてのページで同じ書式設定を持たないドキュメントが必要になることがあります。 たとえば、ページ番号の書式を変更したり、ページサイズと向きを変えたり、最初の文書ページを番号なしの表紙として使用したりする必要がある場合があ あなたはセクションでそれを達成することができます。
セクションは、ヘッダーとフッター、方向、列、余白、ページ番号の書式設定などを制御するレベルノードです。
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!")); |
コンテンツの追加または先頭への追加
図形を描画したり、セクションの先頭/末尾にテキストや画像を追加したりする場合は、SectionクラスのAppendContentメソッドとPrependContentメソッドを使用できます。
次のコード例は、既存のセクションのコンテンツを追加する方法を示しています:
// 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(); |
ドキュメント間のセクションのコピー
場合によっては、多数のセクションを含む大きなドキュメントがあり、セクションの内容をあるドキュメントから別のドキュメントにコピーする
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"); |
セクションヘッダーとフッターの操作
各セクションのヘッダーまたはフッターを表示するための基本的なルールは非常に簡単です:
- セクションに特定のタイプの独自のヘッダー/フッターがない場合は、前のセクションから取得されます。
- ページに表示されるヘッダー/フッターのタイプは、“異なる最初のページ"および"異なる奇数ページと偶数ページ"セクション設定によって制御されます。
次のコード例は、異なるヘッダーを持つ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"); |