本文将介绍如何使用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();
}
}
}
提取结果: