
在Java程序中引入jar包,如下图:
02在IDEA项目文件夹下存入需要用于测试的Word文档。
03在程序中键入如下代码内容:
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();
}
}
执行程序,生成文档,如下图,
特别提示文件路径可自定义。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)