spire.pdf中有pdfattachment类和pdfattachmentannotation类分别对应pdf中的附件和注释附件。本文介绍如何使用spire.pdf for java获取pdf文档中的附件和注释附件并进行删除。
删除附件
import com.spire.pdf.attachments.pdfattachmentcollection;
public class deleteattachments {
public static void main(string[] args) {
//加载pdf文档
pdfdocument doc = new pdfdocument();
doc.loadfromfile("c:\\users\\administrator\\desktop\\attachments.pdf");
//获取附件集合(不包含注释附件)
pdfattachmentcollection attachments = doc.getattachments();
//删除所有附件
attachments.clear();
//删除指定附件
//attachments.removeat(0);
//保存文档
doc.savetofile("output/deleteattachments.pdf");
doc.close();
}
}
删除注释附件
import com.spire.pdf.annotations.pdfannotation;
import com.spire.pdf.annotations.pdfannotationcollection;
import com.spire.pdf.annotations.pdfattachmentannotationwidget;
public class deleteannotationattachments {
public static void main(string[] args) {
//加载pdf文档
pdfdocument doc = new pdfdocument();
doc.loadfromfile("c:\\users\\administrator\\desktop\\attachments.pdf");
//遍历文档中的页
for (int i = 0; i < doc.getpages().getcount(); i ) {
//获取注释集合
pdfannotationcollection annotationcollection = doc.getpages().get(i).getannotationswidget();
//遍历注释集合
for (object annotation: annotationcollection) {
//判断注释是否为pdfattachmentannotationwidget类型
if (annotation instanceof pdfattachmentannotationwidget){
//删除注释附件
annotationcollection.remove((pdfannotation) annotation);
}
}
}
//保存文档
doc.savetofile("output/deleteannotationattachments.pdf");
doc.close();
}
}