使用部分

有时您想要的文档在所有页面上没有相同的格式。 例如,您可能需要修改页码格式,具有不同的页面大小和方向,或者将第一个文档页面作为封面页,而无需任何编号。 你可以用部分来实现这一点。

节是控制页眉和页脚、方向、列、边距、页码格式等的级别节点。

Aspose.Words允许您管理部分,将文档划分为部分,并进行仅适用于特定部分的格式更改。 Aspose.Words存储有关节格式的信息,如页眉和页脚、页面设置和节间隔中的列设置。

本文介绍了如何使用节和节间隔。

什么节和节休息是

文档部分由SectionSectionCollection类表示。 节对象是Document节点的直接子节点,可以通过Sections属性访问。 您可以使用一些方法来管理这些节点,例如Remove, Add, IndexOf, 和其他人。

Section break是将文档页面划分为具有可自定义布局的部分的选项。

分段中断的类型

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-Java.git.
/// <summary>
/// Insert section breaks before the specified paragraphs.
/// </summary>
private void insertSectionBreaks(ArrayList<Paragraph> topicStartParas)
{
DocumentBuilder builder = new DocumentBuilder(mDoc);
for (Paragraph para : topicStartParas)
{
Section section = para.getParentSection();
// Insert section break if the paragraph is not at the beginning of a section already.
if (para != section.getBody().getFirstParagraph())
{
builder.moveTo(para.getFirstChild());
builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);
// 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.getBody().getLastParagraph().remove();
}
}
}

使用Remove方法删除分段中断。 如果您不需要删除特定的节中断,而是删除该节的内容,则可以使用ClearContent方法。

下面的代码示例演示如何删除分隔符:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.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.getSections().getCount() - 2; i >= 0; i--)
{
// Copy the content of the current section to the beginning of the last section.
doc.getLastSection().prependContent(doc.getSections().get(i));
// Remove the copied section.
doc.getSections().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-Java.git.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.write("Section 1");
builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
builder.write("Primary header");
builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);
builder.write("Primary footer");
Section section = doc.getFirstSection();
// 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 (Node node : section)
{
switch (node.getNodeType())
{
case NodeType.BODY:
{
Body body = (Body)node;
System.out.println("Body:");
System.out.println(MessageFormat.format("\t\"{0}\"", body.getText().trim()));
break;
}
case NodeType.HEADER_FOOTER:
{
HeaderFooter headerFooter = (HeaderFooter)node;
System.out.println(MessageFormat.format("HeaderFooter type: {0}:", headerFooter.getHeaderFooterType()));
System.out.println(MessageFormat.format("\t\"{0}\"", headerFooter.getText().trim()));
break;
}
default:
{
throw new Exception("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-Java.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.getPageSetup().setLayoutMode(SectionLayoutMode.LINE_GRID);
builder.getPageSetup().setLinesPerPage(15);
builder.getParagraphFormat().setSnapToGrid(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(getArtifactsDir() + "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-Java.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.getSections().add(new Section(doc));
// Run the "EnsureMinimum" method to add a body and a paragraph to this section to begin editing it.
doc.getLastSection().ensureMinimum();
doc.getSections().get(0).getBody().getFirstParagraph().appendChild(new Run(doc, "Hello world!"));

追加或追加内容

如果要在部分的开头/结尾绘制一些形状或添加文本或图像,可以使用Section类的AppendContentPrependContent方法。

下面的代码示例演示如何附加现有部分的内容:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.write("Section 1");
builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);
builder.write("Section 2");
builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);
builder.write("Section 3");
// This is the section that we will append and prepend to.
Section section = doc.getSections().get(2);
// Insert the contents of the first section to the beginning of the third section.
Section sectionToPrepend = doc.getSections().get(0);
section.prependContent(sectionToPrepend);
// Insert the contents of the second section to the end of the third section.
Section sectionToAppend = doc.getSections().get(1);
section.appendContent(sectionToAppend);

克隆一个部分

Aspose.Words允许您通过使用deepClone方法创建节的完整副本来复制节。

下面的代码示例演示如何克隆文档中的第一个部分:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git.
Document doc = new Document(getMyDir() + "Document.docx");
Section cloneSection = doc.getSections().get(0).deepClone();

在文档之间复制部分

在某些情况下,您可能有许多节的大型文档,并且您希望将节的内容从一个文档复制到另一个文档。

Aspose.Words允许您使用ImportNode方法在文档之间复制部分。

下面的代码示例演示如何在文档之间复制节:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git.
Document srcDoc = new Document(getMyDir() + "Document.docx");
Document dstDoc = new Document();
Section sourceSection = srcDoc.getSections().get(0);
Section newSection = (Section) dstDoc.importNode(sourceSection, true);
dstDoc.getSections().add(newSection);
dstDoc.save(getArtifactsDir() + "WorkingWithSection.CopySection.docx");

使用节页眉和页脚

为每个部分显示页眉或页脚的基本规则非常简单:

  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-Java.git.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.getPageSetup().setOrientation(Orientation.LANDSCAPE);
builder.getPageSetup().setLeftMargin(50.0);
builder.getPageSetup().setPaperSize(PaperSize.PAPER_10_X_14);
doc.save(getArtifactsDir() + "WorkingWithDocumentOptionsAndSettings.SetPageSetupAndSectionFormatting.docx");

下面的代码示例演示如何修改所有部分中的页属性:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.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.
for (Section section : doc.getSections())
section.getPageSetup().setPaperSize(PaperSize.LETTER);
doc.save(getArtifactsDir() + "WorkingWithSection.ModifyPageSetupInAllSections.doc");

请参阅