1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Aspose.Words for .NET使用章节教程(2):如何处理文档分段——Aspose.Words中的分段

Aspose.Words for .NET使用章节教程(2):如何处理文档分段——Aspose.Words中的分段

时间:2019-11-30 05:59:12

相关推荐

Aspose.Words for .NET使用章节教程(2):如何处理文档分段——Aspose.Words中的分段

Aspose.Words For .Net是一种高级Word文档处理API,用于执行各种文档管理和操作任务。API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word。此外,API支持所有流行的Word处理文件格式,并允许将Word文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。

【下载Aspose.Words for .NET最新试用版】

接下来我们将进入“如何使用Aspose.Words以编程方式处理文档分段”的介绍。在生成文档时,使用section非常有用。您可以组合文档,根据从多个模板文档复制的多个部分构建输出文档,或者根据某些应用程序逻辑删除不需要的部分,从而有效地将公共模板文档过滤到特定场景。

Aspose.Words中的部分

文档的各节由Section和SectionCollection类表示。Section对象是Document节点的直接子节点,可以通过Document.Sections属性访问。

▲获得一分段

每个分段由一个Section对象表示,该对象可以通过索引从Document.Sections集合中获取。默认页边距、页眉/页脚距离和列间距取决于模拟MS Word行为的当前区域。例如,现在英语(美国)和英语(英国)的所有页边距都是1英寸。左,右,上边距为2.5厘米; 德国底部边距为2厘米。如果没有为提及参数设置显式值,则新默认值用于新文档和加载文档。

下面的代码示例显示了如何访问指定索引处的节:

//指向documents目录的路径。stringdataDir=RunExamples.GetDataDir_WorkingWithSections();Documentdoc=newDocument(dataDir+"Document.doc");Sectionsection=doc.Sections[0];section.PageSetup.LeftMargin=90;//3.17cmsection.PageSetup.RightMargin=90;//3.17cmsection.PageSetup.TopMargin=72;//2.54cmsection.PageSetup.BottomMargin=72;//2.54cmsection.PageSetup.HeaderDistance=35.4;//1.25cmsection.PageSetup.FooterDistance=35.4;//1.25cmsection.PageSetup.TextColumns.Spacing=35.4;//1.25cm

▲添加一个分段

Document对象提供了可以使用Document.Sections访问的节集合。这将返回包含文档部分的SectionCollection对象。然后,您可以使用此对象上的SectionCollection.Add方法将一个节添加到文档的末尾。下面的代码示例显示了如何将一个部分添加到文档的末尾:

Documentdoc=newDocument(dataDir);SectionsectionToAdd=newSection(doc);doc.Sections.Add(sectionToAdd);

▲删除一个分段

以与上面讨论的相同方式,使用Document.Sections检索文档的部分。然后,可以使用SectionCollection.Remove删除指定的节或SectionCollection.RemoveAt以删除指定索引处的节。 下面的代码示例显示了如何删除指定索引处的节:

Documentdoc=newDocument(dataDir);doc.Sections.RemoveAt(0);

下面的代码示例展示了如何从文档中删除所有部分:

Documentdoc=newDocument(dataDir);doc.Sections.Clear();

▲添加分段内容

如果要复制和插入除了节分隔符和节属性之外的节的主要文本,请使用Section.PrependContent或Section.AppendContent为要复制的内容传递Section对象。如果没有创建新的分段,页眉和页脚不会被复制。前一种方法在该部分的开头插入内容的副本,而后者在该部分的末尾插入内容的副本。下面的代码示例显示了如何附加现有部分的内容:

//文档目录的路径。stringdataDir=RunExamples.GetDataDir_WorkingWithSections();Documentdoc=newDocument(dataDir+"Section.AppendContent.doc");//Thisisthesectionthatwewillappendandprependto.Sectionsection=doc.Sections[2];//复制第1部分的内容并将其插入指定部分的开头。SectionsectionToPrepend=doc.Sections[0];section.PrependContent(sectionToPrepend);//复制第二部分的内容并将其插入指定部分的末尾。SectionsectionToAppend=doc.Sections[1];section.AppendContent(sectionToAppend);

▲删除分段内容

要删除节的主要文本,请使用Section.ClearContent。要删除节中的页眉和页脚,请调用Section.ClearHeadersFooters。下面的示例显示了如何删除节的主要内容:

//文档目录的路径。stringdataDir=RunExamples.GetDataDir_WorkingWithSections();Documentdoc=newDocument(dataDir+"Document.doc");Sectionsection=doc.Sections[0];section.ClearContent();

▲克隆一分段

使用Section.Clone方法创建特定节的副本。下面的示例显示了如何创建特定部分的副本:

//文档目录的路径。stringdataDir=RunExamples.GetDataDir_WorkingWithSections();Documentdoc=newDocument(dataDir+"Document.doc");SectioncloneSection=doc.Sections[0].Clone();

▲在文档之间复制分段

将一个文档完全或部分复制到另一个文档是一项非常流行的任务 这是实现这一点的“模式”。在插入来自其他文档的任何节点之前,必须使用Document.ImportNode方法导入该节点。该Document.ImportNode方法使原始节点的副本,并更新所有的内部文档特定的属性,如清单和样式,使他们的目标文档中有效。 下面的示例显示了如何在文档之间复制分段:

//文档目录的路径。stringdataDir=RunExamples.GetDataDir_WorkingWithSections();DocumentsrcDoc=newDocument(dataDir+"Document.doc");DocumentdstDoc=newDocument();SectionsourceSection=srcDoc.Sections[0];SectionnewSection=(Section)dstDoc.ImportNode(sourceSection,true);dstDoc.Sections.Add(newSection);dataDir=dataDir+"Document.Copy_out.doc";dstDoc.Save(dataDir);

欢迎下载|体验更多Aspose文档管理产品或 加入Aspose技术交流群(761297826

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。