本文介绍使用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();
}
}
目录删除效果前后: