使用部分
有时您想要的文档在所有页面上没有相同的格式。 例如,您可能需要修改页码格式,具有不同的页面大小和方向,或者将第一个文档页面作为封面页,而无需任何编号。 你可以用部分来实现这一点。
节是控制页眉和页脚、方向、列、边距、页码格式等的级别节点。
Aspose.Words允许您管理部分,将文档划分为部分,并进行仅适用于特定部分的格式更改。 Aspose.Words存储有关节格式的信息,如页眉和页脚、页面设置和节间隔中的列设置。
本文介绍了如何使用节和节间隔。
什么节和节休息是
文档部分由Section和SectionCollection类表示。 节对象是Document节点的直接子节点,可以通过Sections属性访问。 您可以使用一些方法来管理这些节点,例如Remove, Add, IndexOf, 和其他人。
Section break是将文档页面划分为具有可自定义布局的部分的选项。
分段中断的类型
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"); |