这里介绍通过Java实现添加或删除PDF中的附件的完整攻略:
1. 添加附件
1.1 引入相关库
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.pdfbox.cos.COSDictionary;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.cos.COSStream;
import org.apache.pdfbox.io.IOUtils;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.common.PDStream;
import org.apache.pdfbox.pdmodel.interactive.action.PDActionFactory;
import org.apache.pdfbox.pdmodel.interactive.action.PDActionJavaScript;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationFileAttachment;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination.PDPageFitWidthDestination;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination.PDPageXYZDestination;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineItem;
1.2 添加附件的具体实现
public class AddAttachmentInPDF {
public static void main(String[] args) throws IOException {
//定义输入和输出文件
File pdfFile = new File("example.pdf");
File attachFile = new File("attach.pdf");
//载入PDF文件
try (PDDocument document = PDFParser.load(pdfFile)) {
//创建文件附件
PDAnnotationFileAttachment fileAttachment = new PDAnnotationFileAttachment();
//设置文件名和描述
fileAttachment.setFile("Attachment Name", IOUtils.toByteArray(new FileInputStream(attachFile)));
fileAttachment.setContents("Attachment Description");
//设置位置和大小
fileAttachment.setRectangle(new PDRectangle(50, 50, 100, 100));
//添加到页面
document.getPage(0).getAnnotations().add(fileAttachment);
//创建外部文件附件对象
PDOutlineItem annotation = new PDOutlineItem();
annotation.setTitle("Open Document");
//添加行为和目的地
PDActionJavaScript js = new PDActionJavaScript("this.exportDataObject({cName: 'Attachment Name', nLaunch: 2});");
PDActionFactory.createAction(js);
PDPageXYZDestination dest = new PDPageXYZDestination();
dest.setPage(document.getPage(0));
dest.setZoom(1);
dest.setTop(new Float(0));
dest.setLeft(new Float(0));
PDPageFitWidthDestination fit = new PDPageFitWidthDestination();
fit.setPage(document.getPage(0));
fit.setTop(new Float(0));
//设置附件属性
fileAttachment.setName("Attachment Name");
fileAttachment.setPage(document.getPage(0));
fileAttachment.setParent(annotation);
fileAttachment.setModifiedDate(new Date());
fileAttachment.setAction(js);
fileAttachment.setDestination(dest);
fileAttachment.setFit(fit);
//保存为新文件
document.save(new File("example_out.pdf"));
}
}
}
1.3 示例
下面是一个添加附件的示例代码:
public class AddAttachmentInPDF {
public static void main(String[] args) throws IOException {
//定义输入和输出文件
File pdfFile = new File("example.pdf");
File attachFile = new File("attach.pdf");
//载入PDF文件
try (PDDocument document = PDFParser.load(pdfFile)) {
//创建文件附件
PDAnnotationFileAttachment fileAttachment = new PDAnnotationFileAttachment();
//设置文件名和描述
fileAttachment.setFile("Attachment Name", IOUtils.toByteArray(new FileInputStream(attachFile)));
fileAttachment.setContents("Attachment Description");
//设置位置和大小
fileAttachment.setRectangle(new PDRectangle(50, 50, 100, 100));
//添加到页面
document.getPage(0).getAnnotations().add(fileAttachment);
//保存为新文件
document.save(new File("example_out.pdf"));
}
}
}
2. 删除附件
2.1 引入相关库
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.apache.pdfbox.cos.COSDictionary;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.cos.COSStream;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.common.PDStream;
import org.apache.pdfbox.pdmodel.interactive.action.PDActionFactory;
import org.apache.pdfbox.pdmodel.interactive.action.PDActionJavaScript;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationFileAttachment;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination.PDPageFitWidthDestination;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination.PDPageXYZDestination;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineItem;
2.2 删除附件的具体实现
public class DeleteAttachmentInPDF {
public static void main(String[] args) throws IOException {
//定义输入和输出文件
File pdfFile = new File("example.pdf");
//载入PDF文件
try (PDDocument document = PDFParser.load(pdfFile)) {
//获取第一页上的所有注释
List<PDAnnotation> annotations = document.getPage(0).getAnnotations();
//遍历注释
for (PDAnnotation annotation : annotations) {
if (annotation instanceof PDAnnotationFileAttachment) {
//获取附件字典
COSDictionary fileAttachmentDict = ((PDAnnotationFileAttachment) annotation).getCOSObject();
//获取附件字典中的Filespec中的Stream字典
COSDictionary fileSpecDict = fileAttachmentDict.getCOSDictionary(COSName.FS);
if (fileSpecDict != null) {
COSDictionary fileStreamDict = fileSpecDict.getCOSDictionary(COSName.EF);
//删除字典中的Stream字典
if (fileStreamDict != null) {
COSStream cosStream = (COSStream) PDStream.createFromCOS(fileStreamDict);
document.getDocument().getObjects().remove(cosStream.getCOSObject());
}
}
//删除注释
document.getPage(0).getAnnotations().remove(annotation);
}
}
//保存为新文件
document.save(new File("example_out.pdf"));
}
}
}
2.3 示例
下面是一个删除附件的示例代码:
public class DeleteAttachmentInPDF {
public static void main(String[] args) throws IOException {
//定义输入和输出文件
File pdfFile = new File("example.pdf");
//载入PDF文件
try (PDDocument document = PDFParser.load(pdfFile)) {
//获取第一页上的所有注释
List<PDAnnotation> annotations = document.getPage(0).getAnnotations();
//遍历注释
for (PDAnnotation annotation : annotations) {
if (annotation instanceof PDAnnotationFileAttachment) {
//获取附件字典
COSDictionary fileAttachmentDict = ((PDAnnotationFileAttachment) annotation).getCOSObject();
//获取附件字典中的Filespec中的Stream字典
COSDictionary fileSpecDict = fileAttachmentDict.getCOSDictionary(COSName.FS);
if (fileSpecDict != null) {
COSDictionary fileStreamDict = fileSpecDict.getCOSDictionary(COSName.EF);
//删除字典中的Stream字典
if (fileStreamDict != null) {
COSStream cosStream = (COSStream) PDStream.createFromCOS(fileStreamDict);
document.getDocument().getObjects().remove(cosStream.getCOSObject());
}
}
//删除注释
document.getPage(0).getAnnotations().remove(annotation);
}
}
//保存为新文件
document.save(new File("example_out.pdf"));
}
}
}
以上就是通过Java实现添加或删除PDF中的附件的完整攻略。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:通过Java实现添加或删除PDF中的附件 - Python技术站