如何通过Java应用程序添加或删除 PDF 中的附件

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

当我们在制作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​

1.引入jar包

导入方法1:

手动引入。将Free Spire.PDF for Java下载到本地,解压,找到lib文件夹下的Spire.PDF.jar文件。在IDEA中打开如下界面,将本地路径中的jar文件引入Java程序:

如何通过Java应用程序添加或删除 PDF 中的附件 _PDF

导入方法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 中的附件 _添加附件_02

在 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 中的附件 _PDF_03

在 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();
}
}

—本文完—

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: Java