本文介绍使用Spire.Doc for Java删除Word目录的方法。
import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
public class RemoveTOC {
public static void main(String[]args){
//加载包含目录的Word文档
Document doc = new Document();
doc.loadFromFile("sample.docx");
//获取section
Section section = doc.getSections().get(0);
//遍历段落
for (int i = 0; i < section.getParagraphs().getCount(); i++)
{
Paragraph paragraph = section.getParagraphs().get(i);
if (paragraph.getStyleName().matches("TOC\\w+"))
{
section.getParagraphs().removeAt(i);//删除目录
i--;
}
}
//保存文档
doc.saveToFile("RemoveTOC.docx", FileFormat.Docx_2013);
doc.dispose();
}
}
目录删除效果前后: