如何通过Java应用程序添加或删除 PDF 中的附件
2024-10-20 13:35:46
当我们在制作PDF文件或者PPT演示文稿的时候,为了让自己的文件更全面详细,就会在文件中添加附件。并且将相关文档附加到 PDF 可以方便文档的集中管理和传输。那么如何添加或删除 PDF 中的附件呢?别担心,我们可以通过编程方式轻松实现此操作。下面是我整理的具体步骤,并附上Java代码供大家参考。
文档级附件:PDF的文档级附件不会显示在页面上,只能在PDF阅读器的“附件”面板中查看。
注释附件:文件将被添加到页面的特定位置。注释附件在页面上显示为回形针图标;审阅者可以双击图标打开文件。
- 在 Java 中向 PDF 添加附件
- 在 Java 中向 PDF 添加注释附件
- 在 Java 中从 PDF 中删除附件
- 在 Java 中从 PDF 中删除注释附件
代码编译环境:
IntelliJ IDEA 2018(jdk 1.8.0)
PDF Jar包:Free Spire.PDF for Java 5.1.0
引入jar包
导入方法1:
手动引入。将Free Spire.PDF for Java下载到本地,解压,找到lib文件夹下的Spire.PDF.jar文件。在IDEA中打开如下界面,将本地路径中的jar文件引入Java程序:
导入方法2:如果您想通过 Maven安装,则可以在 pom.xml 文件中添加以下代码导入 JAR 文件。
<repositories>
<repository>
<id>com.e-iceblue</id>
<url>https://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.pdf.free</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>
在 Java 中向 PDF 添加附件
- 创建一个 PdfDocument 对象。
- 使用 PdfDocument.loadFromFile() 方法加载 PDF 文档。
- 基于外部文件创建 PdfAttachment 对象。
- 使用 PdfDocument.getAttachments().add() 方法将附件添加到 PDF。
- 使用 PdfDocument.saveToFile() 方法将文档保存到另一个 PDF 文件。
完整代码
Java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.attachments.PdfAttachment; public class AttachFilesToPdf { public static void main(String[] args) { //创建一个 PdfDocument 对象
PdfDocument doc = new PdfDocument(); //加载 PDF 文档
doc.loadFromFile("什么是AI数字人.pdf"); //基于外部文件创建 PdfAttachment 对象
PdfAttachment attachment = new PdfAttachment("到场嘉宾名单.xlsx"); //将附件添加到 PDF
doc.getAttachments().add(attachment); //保存文件
doc.saveToFile("添加附件.pdf");
}
}
效果图
在 Java 中向 PDF 添加注释附件
以下是向 PDF 添加注释附件的步骤。
- 创建一个 PdfDocument 对象。
- 使用 PdfDocument.loadFromFile() 方法加载 PDF 文档。
- 使用 PdfDocument.getPages().get() 方法获取特定页面以添加注释。
- 基于外部文件创建 PdfAttachmentAnnotation 对象。
- 使用 PdfPageBase.getAnnotationsWidget().add() 方法将注释附件添加到页面。
- 使用 PdfDocument.saveToFile() 方法将文档保存到另一个 PDF 文件。
完整代码
Java
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.annotations.*;
import com.spire.pdf.graphics.*;
import com.spire.pdf.PdfDocument; import java.awt.*;
import java.awt.geom.Dimension2D;
import java.awt.geom.Rectangle2D;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; public class AnnotationAttachment { public static void main(String[] args) throws IOException { //创建一个 PdfDocument 对象
PdfDocument doc = new PdfDocument(); //加载 PDF 文档
doc.loadFromFile("什么是AI数字人1.pdf"); //获取特定页面
PdfPageBase page = doc.getPages().get(0); //在 PDF 上绘制标签
String label = "AI数字人详情介绍见附件:";
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("宋体", Font.PLAIN, 13));
double x = 35;
double y = doc.getPages().get(0).getActualSize().getHeight() - 270;
page.getCanvas().drawString(label, font, PdfBrushes.getRed(), x, y); //附加文件作为注释
String filePath = "AI数字人详情介绍.pptx";
byte[] data = toByteArray(filePath);
Dimension2D size = font.measureString(label);
Rectangle2D bound = new Rectangle2D.Float((float) (x + size.getWidth() + 5), (float) y, 10, 15);
PdfAttachmentAnnotation annotation = new PdfAttachmentAnnotation(bound, filePath, data);
annotation.setColor(new PdfRGBColor(new Color(0, 128, 128)));
annotation.setFlags(PdfAnnotationFlags.Default);
annotation.setIcon(PdfAttachmentIcon.Graph);
annotation.setText("单击此处打开文件");
page.getAnnotationsWidget().add(annotation); //保存文件
doc.saveToFile("添加注释附件.pdf");
}
//将文件转换为字节数组
public static byte[] toByteArray(String filePath) throws IOException { File file = new File(filePath);
long fileSize = file.length();
if (fileSize > Integer.MAX_VALUE) {
System.out.println("文件过大...");
return null;
}
FileInputStream fi = new FileInputStream(file);
byte[] buffer = new byte[(int) fileSize];
int offset = 0;
int numRead = 0;
while (offset < buffer.length
&& (numRead = fi.read(buffer, offset, buffer.length - offset)) >= 0) {
offset += numRead;
} if (offset != buffer.length) {
throw new IOException("无法完全读取文件 "
+ file.getName());
}
fi.close();
return buffer;
}
}
效果图
在 Java 中从 PDF 中删除附件
详细步骤如下。
- 创建一个 PdfDocument 对象。
- 使用 PdfDocument.loadFromFile() 方法加载 PDF 文档。
- 使用 PdfDocument.getAttachments() 方法从文档中获取附件集合。
- 使用 PdfAttachmentCollection.removeAt() 方法删除特定附件。 要一次删除所有附件,可以使用 PdfAttachmentCollection.clear() 方法。
- 使用 PdfDocument.saveToFile() 方法将文档保存到另一个 PDF 文件。
完整代码
Java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.attachments.PdfAttachmentCollection; public class RemoveAttachments { public static void main(String[] args) { //创建一个 PdfDocument 对象
PdfDocument doc = new PdfDocument(); //加载 PDF 文档
doc.loadFromFile("添加附件.pdf"); //获取附件集合,不包含注释附件
PdfAttachmentCollection attachments = doc.getAttachments(); //删除所有附件
attachments.clear(); //删除指定附件
//attachments.removeAt(0); //保存文件
doc.saveToFile("删除附件.pdf");
doc.close();
}
}
在 Java 中从 PDF 中删除注释附件
以下是详细步骤。
- 创建一个 PdfDocument 对象。
- 使用 PdfDocument.loadFromFile() 方法加载 PDF 文档。
- 遍历文档中的页面,并使用 PdfPageBase.getAnnotationsWidget() 方法从特定页面获取注释集合。
- 确定注释是否为 PdfAttachmentAnnotationWidget 的实例。如果是,请使用 PdfAnnotationCollection.remove() 方法删除注释附件。
- 使用 PdfDocument.saveToFile() 方法将文档保存到另一个 PDF 文件。
完整代码
Java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.annotations.PdfAnnotation;
import com.spire.pdf.annotations.PdfAnnotationCollection;
import com.spire.pdf.annotations.PdfAttachmentAnnotationWidget; public class RemoveAnnotationAttachments { public static void main(String[] args) { //创建一个 PdfDocument 对象
PdfDocument doc = new PdfDocument(); //加载 PDF 文档
doc.loadFromFile("添加注释附件.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("删除注释附件.pdf");
doc.close();
}
}
—本文完—
最新文章
- 【C#】组件发布:MessageTip,轻快型消息提示窗
- 【转】RHadoop实践系列之二:RHadoop安装与使用
- Cocoapods的使用教程
- (传输层)UDP协议
- Moo University - Financial Aid
- java提高篇---LinkedList
- JAVA标签的使用跳出循环
- POJ 1947 Rebuilding Roads (树dp + 背包思想)
- Android app version code and name
- DUBBO安装配置注意事项
- Composer生成PHP依赖包
- github上forck一个分支之后,如何和主分支同步
- Hbuilder常用功能汇总
- Python基础之字符编码
- [SDOI2008]Cave 洞穴勘测
- 【安富莱专题教程第2期】uC/Probe简易使用说明,含MDK和IAR,支持F103,F407和F429开发板
- 阿里分布式事务解决方案-GTS
- 第五天 py if使用
- Django 的ORM 数据操作
- Android getWidth和getMeasuredWidth
热门文章
- [苹果APP上架]ios App Store上架详细教程-一条龙顺滑上架-适合小白
- vulnhub靶场之NOOB: 1
- salesforce零基础学习(一百二十)快去迁移你的代码中的 Alert / Confirm 以及 Prompt吧
- Codeforces Round #812 (Div. 2) E(并查集)
- 数组还是HashSet?
- Java学习之Filter与Listener
- Spring Cloud GateWay基于nacos如何去做灰度发布
- 【论文解读】NIPS 2021-HSWA: Hierarchical Semantic-Visual Adaption for Zero-Shot Learning.(基于层次适应的零样本学习)
- C++日期和时间编程总结
- 彻底理解Python中的闭包和装饰器(下)