文档半岛外围网上直营>>Aspose中文文档>>从文档中删除分节符
从文档中删除分节符
Aspose.Words是一种高级Word文档处理API,用于执行各种文档管理和操作任务。API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word。
Aspose API支持流行文件格式处理,并允许将各类文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。
使用Aspose.Words
在Aspose.Words中,使用ControlChar类的SectionBreak字段来查找所有分节符。
以下代码示例演示如何从文档中删除分页符:
Document doc = new Document(MyDir + "Remove section breaks.docx");
// Loop through all sections starting from the section that precedes the last one
for (int i = doc.Sections.Count - 2; i >= 0; i--)
{
// Copy the content of the current section to the beginning of the last section.
doc.LastSection.PrependContent(doc.Sections[i]);
// Remove the copied section.
doc.Sections[i].Remove();
}
点击复制
使用 Open XML SDK
需要添加的命名空间:
uusing System.Collections.Generic; using System.IO; using System.Linq; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Wordprocessing; using NUnit.Framework;
点击复制
以下代码示例演示如何从文档中删除分页符:
public void RemoveSectionBreaksFeature()
{
using (WordprocessingDocument myDoc = WordprocessingDocument.Open(MyDir + "Remove section breaks.docx", true))
{
MainDocumentPart mainPart = myDoc.MainDocumentPart;
List<ParagraphProperties> paraProps = mainPart.Document.Descendants<ParagraphProperties>()
.Where(IsSectionProps).ToList();
foreach (ParagraphProperties pPr in paraProps)
pPr.RemoveChild(pPr.GetFirstChild<SectionProperties>());
using (Stream stream = File.Create(ArtifactsDir + "Remove section breaks - OpenXML.docx"))
{
mainPart.Document.Save(stream);
}
}
}
private static bool IsSectionProps(ParagraphProperties pPr)
{
SectionProperties sectPr = pPr.GetFirstChild<SectionProperties>();
if (sectPr == null)
return false;
return true;
}
点击复制

QQ交谈
在线咨询

渝公网安备
50010702500608号

客服热线