本文将介绍如何使用spire.pdf for java提取pdf文档中的附件并保存至本地。
原pdf文档:
import com.spire.pdf.pdfdocument;
import com.spire.pdf.attachments.pdfattachmentcollection;
import java.io.*;
public class extractattachments {
public static void main(string[] args) throws ioexception {
//创建pdfdocument实例
pdfdocument pdf = new pdfdocument();
//加载pdf文档
pdf.loadfromfile("addattachments.pdf");
//获取pdf文档的附件集合
pdfattachmentcollection attachments = pdf.getattachments();
//遍历附件集合,提取集合中的每一个附件并保存至本地
for (int i = 0; i < attachments.getcount(); i ) {
file file = new file("attachments/" attachments.get(i).getfilename());
outputstream output = new fileoutputstream(file);
bufferedoutputstream bufferedoutput = new bufferedoutputstream(output);
bufferedoutput.write(attachments.get(i).getdata());
bufferedoutput.close();
}
}
}
提取结果: