使用Java應用程序添加或刪除 PDF 中的附件
如何通過Java應用程序添加或刪除 PDF 中的附件
當我們在制作PDF文件或者PPT演示文稿的時候,為了讓自己的文件更全面詳細,就會在文件中添加附件。并且將相關文檔附加到 PDF 可以方便文檔的集中管理和傳輸。那么如何添加或刪除 PDF 中的附件呢?別擔心,我們可以通過編程方式輕松實現(xiàn)此操作。下面是我整理的具體步驟,并附上Java代碼供大家參考。
文檔級附件:PDF的文檔級附件不會顯示在頁面上,只能在PDF閱讀器的“附件”面板中查看。
注釋附件:文件將被添加到頁面的特定位置。注釋附件在頁面上顯示為回形針圖標;審閱者可以雙擊圖標打開文件。
- 在 Java 中向 PDF 添加附件
- 在 Java 中向 PDF 添加注釋附件
- 在 Java 中從 PDF 中刪除附件
- 在 Java 中從 PDF 中刪除注釋附件
代碼編譯環(huán)境:
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 添加附件
- 創(chuàng)建一個 PdfDocument 對象。
- 使用 PdfDocument.loadFromFile() 方法加載 PDF 文檔。
- 基于外部文件創(chuàng)建 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) {
//創(chuàng)建一個 PdfDocument 對象
PdfDocument doc = new PdfDocument();
//加載 PDF 文檔
doc.loadFromFile("什么是AI數(shù)字人.pdf");
//基于外部文件創(chuàng)建 PdfAttachment 對象
PdfAttachment attachment = new PdfAttachment("到場嘉賓名單.xlsx");
//將附件添加到 PDF
doc.getAttachments().add(attachment);
//保存文件
doc.saveToFile("添加附件.pdf");
}
}效果圖

在 Java 中向 PDF 添加注釋附件
以下是向 PDF 添加注釋附件的步驟。
- 創(chuàng)建一個 PdfDocument 對象。
- 使用 PdfDocument.loadFromFile() 方法加載 PDF 文檔。
- 使用 PdfDocument.getPages().get() 方法獲取特定頁面以添加注釋。
- 基于外部文件創(chuàng)建 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 {
//創(chuàng)建一個 PdfDocument 對象
PdfDocument doc = new PdfDocument();
//加載 PDF 文檔
doc.loadFromFile("什么是AI數(shù)字人1.pdf");
//獲取特定頁面
PdfPageBase page = doc.getPages().get(0);
//在 PDF 上繪制標簽
String label = "AI數(shù)字人詳情介紹見附件:";
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數(shù)字人詳情介紹.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");
}
//將文件轉換為字節(jié)數(shù)組
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 中刪除附件
詳細步驟如下。
- 創(chuàng)建一個 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) {
//創(chuàng)建一個 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 中刪除注釋附件
以下是詳細步驟。
- 創(chuàng)建一個 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) {
//創(chuàng)建一個 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();
//循環(huán)遍歷注釋
for (Object annotation: annotationCollection) {
//確定注釋是否為 PdfAttachmentAnnotationWidget 的實例
if (annotation instanceof PdfAttachmentAnnotationWidget){
//刪除注釋附件
annotationCollection.remove((PdfAnnotation) annotation);
}
}
}
//保存文件
doc.saveToFile("刪除注釋附件.pdf");
doc.close();
}
}到此這篇關于如何通過Java應用程序添加或刪除 PDF 中的附件的文章就介紹到這了,更多相關java添加或刪除 PDF 中的附件內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
springboot整合mybatis-plus基于注解實現(xiàn)一對一(一對多)查詢功能
這篇文章主要介紹了springboot整合mybatis-plus基于純注解實現(xiàn)一對一(一對多)查詢功能,因為本人采用的是spring-boot進行開發(fā),本身springboot就提倡采用不用配置自動配置的方式,所以真心希望mybatis(不是mybatis-plus)這點需要繼續(xù)努力2021-09-09
Spring Boot2開發(fā)之Spring Boot整合Shiro兩種詳細方法
這篇文章主要介紹了Spring Boot2開發(fā)之Spring Boot整合Shiro詳細方法,需要的朋友可以參考下2020-03-03
java JSON解析庫Alibaba Fastjson用法詳解
這篇文章主要介紹了java JSON解析庫Alibaba Fastjson用法,結合實例形式詳細分析了java JSON解析庫Alibaba Fastjson的基本功能、原理、用法及操作注意事項,需要的朋友可以參考下2020-04-04

